1 Replies - 3887 Views - Last Post: 20 August 2017 - 09:24 AM Rate Topic: -----

#1 terrysworkstations   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 23-May 17

Basic steps of intersection of bounding sphere

Posted 31 July 2017 - 04:55 PM

I just need to know the basic steps in how to detect collision with a bounding sphere. I want to not be able to go through a object when I get close. I have the following code below that I will use.

IvBoundingSphere::Set( const IvVector3* points, unsigned int numPoints )
    ASSERT( points );
    // compute minimal and maximal bounds
    IvVector3 min(points[0]), max(points[0]);
    unsigned int i;
    for ( i = 1; i < numPoints; ++i )
        if (points[i].x < min.x)
            min.x = points[i].x;
        else if (points[i].x > max.x )
            max.x = points[i].x;
        if (points[i].y < min.y)
            min.y = points[i].y;
        else if (points[i].y > max.y )
            max.y = points[i].y;
        if (points[i].z < min.z)
            min.z = points[i].z;
        else if (points[i].z > max.z )
            max.z = points[i].z;
    // compute center and radius
    mCenter = 0.5f*(min + max);
    float maxDistance = ::DistanceSquared( mCenter, points[0] );
    for ( i = 1; i < numPoints; ++i )
        float dist = ::DistanceSquared( mCenter, points[i] );
        if (dist > maxDistance)
            maxDistance = dist;
    mRadius = ::IvSqrt( maxDistance );

Also, do I run this code on all my vertices of the object that I want the sphere on?

Is This A Good Question/Topic? 0
  • +

Replies To: Basic steps of intersection of bounding sphere

#2 BBeck   User is offline

  • Here to help.
  • member icon

Reputation: 792
  • View blog
  • Posts: 1,886
  • Joined: 24-April 12

Re: Basic steps of intersection of bounding sphere

Posted 20 August 2017 - 09:24 AM

The idea of a bounding sphere is that each object (not the individual vertices) has a sphere that surrounds it. So the sphere can be sized for that object. One way of mathematically determining the sphere size and position would be to analyze all the vertices and find the two vertices that are furthest apart from one another. Then find the mid-point between them. That would give you the center position of the sphere and the diameter of the sphere for a perfectly sized sphere. That's probably never done. Most people probably just make a "ball-park" estimate of the sphere size and place it roughly at the center of the object. Ideally, you might have a level editor like program that would allow you to visually attach the sphere to the object.

If it were me, I would maybe create my model in Blender and create a separate cube in the model to represent the sphere. Then I would have my model loading code pull that named cube out and create the sphere based on the position and diameter of the cube. Or... you can just kinda guess on where to place it and how big to size it. I'm probably even more likely to do that.

Anyway, sphere collisions are the most efficient and the most simple because they are so straight forward. If the two spheres represent the boundaries of two objects, then either the sphere's over-lap (collision) or they don't. Get the radius of each sphere and add them together. Then get the distance between the center-point of both spheres. Which ever is bigger is your answer. If the spheres overlap, then the distance between them must be shorter than the sum of their radii (er... radiusesus, radium, raidi... half widths). If the distance between them is greater than the sum of their half-diameters then the spheres cannot be overlapping. So, mathematically, the only difficulty at all is getting the distance between two points.

Once you get this between two spheres, you can start assigning more spheres to a model. A sphere doesn't really match the shape of most models. But if you have a model of a humanoid, for example, you could assign spheres to every joint such as the upper arm, lower arm, and hand. This could also tell you where the model is hit if you are shooting at it and the bullet's sphere can be determined as to which body part it hits. You could take this to the extreme and create a sphere for every vertex of the model, but today's models have maybe as many as 10,000 vertices. And the vertices technically are infinitesimally small. Not to mention that it's really the triangles between the vertices where the collision should happen. So really the spheres should be assigned to the triangles of the model, not the vertices. All of that's probably over-kill. Just having spheres for each major joint/body-part would probably be pretty accurate. And there are other methods of collision detection once you start needing to know specifically which triangle the object collided with.
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1