Sphere-box collision

Started by
9 comments, last by lonesock 19 years, 1 month ago
Hello, I'm trying to collide a box with a sphere. The box can turn in all directions, it's just a solid object. I can't figure out how to test a box with a sphere. The info I know when I'm testing: Sphere: - Radius - Position Box: - Size - Position - Rotation Can anyone please help me with some advice or code? GBas [Edited by - GraphicsBas on February 12, 2005 6:46:43 AM]
GBS
Advertisement
Can you tell us how accurate you want this thing to be?

Also, what specific situations do you have? Will the sphere's be close to the same size as the rectangle?

One way is to use a sphere for the rectangle. Make it so that the sphere has equal volume as the rectangle. Than check for sphere-sphere collision.
As Sagar suggested, some more info might be helpful. Since you say sphere, I'm assuming 3D. So then box.size is a vector? And is box.rotation a 3x3 matrix? Finally, do you want static (fairly easy) or swept (harder) intersection?

For static intersection, it goes something like this. First, you transform the sphere center into the local space of the box, where the box is axis-aligned. Then you find the closest point on the box to the sphere center. This can be done very easily - I'd search for 'sphere AABB intersection', but if that doesn't turn it up I can help you with that code. Finally, if the squared distance from the sphere center to the closest point is less than the squared radius, the box and sphere intersect. If you need it, you can construct a translation vector which will push the objects apart until they are just touching.
To collision-test sphere against box:  For each side of box:    Collision-test sphere against plane of that side.    If collision occurs, within the bounds of the side, report that collision.  Else, report no collision.To collision-test sphere against plane:  Find the line parallel to the plane normal, passing through the centre of the sphere.  Find the intersection of that line with the plane.  If the distance between that point and the sphere centre <= the radius of the sphere:    Report collision at that point.  Else, report no collision.
Zahlman, unless I'm overlooking something, I don't think that method will work. It will return false negatives in certain cases where the sphere intersects a corner or edge of the box.

I may be misunderstanding though...

In any case, I would still go with the closest-point-on-box method, which will be faster and probably more robust.
You are correct. It would also be necessary to test against each edge (in a similar way) and corner (just check distance from the corner). Doh.
In 3D. I want it to be very accurate, I also want to find the normal of the collision and the collision depth. It's just a sphere dropping on a box. It can be close to the box. But also very far away.

I've got rotation in an vector but I constructed a 3*3 matrix of it while testing. The size of the box is also stored in one vector. The box can move and rotate in the simulation as well as the sphere.
I would really like to see some code please.

I need this for a physics engine.
GBS
My initial thought is to make a plane for each surface of the cube. Then find the distance from the center of the sphere to the nearest point on the plane. Subtract the radius of the sphere.
Do this for each plane, if the results are all negative you have a collision.
Not definite this will work yet.

- As an optimisation maybe you wouldn't have to do it for each plane, only three planes then subtract or add the width of the cube for the 'opposite' sides.

Edit: This method seems to be the same as Zahlmans idea, I can't see where the false negatives come in though? I can see false positives around the corners though :/
It's quite simple:

- First you have to get the sphere into the box' coordinate system (eg. by rotating the entire thing so that it is axis aligned).

- Then take the point within the box that is closest to the sphere's center. A plain set of if statements in C++:
if(sphere.center.x < box.leftx)  closest.x = box.leftx;elseif(sphere.center.x > box.rightx)  closest.x = box.rightx;else  closest.x = sphere.center.x;// Same for all other dimensions you have


- Now, if the distance from this closest point to the sphere's center is less then the sphere's radius, you've got a collision.


Things quickly become complicated if one or both objects are moving, of course ;)

-Markus-
Professional C++ and .NET developer trying to break into indie game development.
Follow my progress: http://blog.nuclex-games.com/ or Twitter - Topics: Ogre3D, Blender, game architecture tips & code snippets.
Tnx, a lot I've got it now!
Now I need the same thing for a box against a box..
Any ideas? :)
GBS

This topic is closed to new replies.

Advertisement