Public Group

# Bounding Sphere for collision Detection

This topic is 810 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

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;
}
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;
}
}

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

Edited by terrysworkstations

##### Share on other sites

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

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

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

Edited by MarcusAseth

##### Share on other sites

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.

##### Share on other sites

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

Edited by terrysworkstations

• ### Game Developer Survey

We are looking for qualified game developers to participate in a 10-minute online survey. Qualified participants will be offered a \$15 incentive for your time and insights. Click here to start!

• 10
• 15
• 21
• 26
• 11