Advertisement

Bounding Sphere for collision Detection

Started by July 31, 2017 09:35 PM
3 comments, last by terrysworkstations 7 years, 4 months ago

I want to know the steps so I can create a bounding sphere on a object so when I get close, it will collide and will not intersect the object. I think my main question is..Will I get the distance of the radius and the center of my object to detect this? Second, do I run this function below on all the vertices of the object? I just need the steps.

 

The code snippet I got is:


void
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.x < min.x)
            min.x = points.x;
        else if (points.x > max.x )
            max.x = points.x;
        if (points.y < min.y)
            min.y = points.y;
        else if (points.y > max.y )
            max.y = points.y;
        if (points.z < min.z)
            min.z = points.z;
        else if (points.z > max.z )
            max.z = points.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 );
        if (dist > maxDistance)
            maxDistance = dist;
    }
    mRadius = ::IvSqrt( maxDistance );
}

Just need a basic idea on how to do this. Thank you

First of all, I have no idea of how what you are trying to do is usually achieved, I am a begginner and my answer could be the slowest one or plain wrong and not work, but since noone answered you so far, I'll give it a shot anyway in case it helps.

 

If I had that problem, I would start by trying and solve it in 2D, then think about 3d, image below

i7VPkxF.png

Basically I would check the difference in distances from the points of a shape to the center of the sphere and keep only the shortest ones for each figure, purple lines in the image, then I would take the lenght of the segment using (sqrt(x^2+y^2)) (you don't actually need the square root in this case since sqrt is expensive) and I would compare it with the radius of my sphere (which is the same at every angle so you don't need to compute it). And then I would know if an object is colliding with the sphere if that length was shorter than the sphere radius.

Again, I don't how it is supposed to be, so if is not the right way, aplogize :P

And if that didn't helped, then I would suggest this book -> https://www.amazon.com/Real-Time-Collision-Detection-Interactive-Technology/dp/1558607323

Advertisement

You don't need to loop twice to get the radius. You already have the min and max vertices. So your radius is just Length(max-center).

To check for collisions, there is potentially one if the distance between the 2 centers is less or equal the sum of both the radii.

Alright _Silence_,MarcusASeth, got it to collide right. Thx

This topic is closed to new replies.

Advertisement