• FEATURED

View more

View more

View more

### Image of the Day Submit

IOTD | Top Screenshots

### The latest, straight to your Inbox.

Subscribe to GameDev.net Direct to receive the latest updates and exclusive content.

# Sphere-box collision

Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

10 replies to this topic

### #1Bas Zalmstra  Members

Posted 12 February 2005 - 12:20 AM

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]

### #2Sagar_Indurkhya  Members

Posted 12 February 2005 - 01:57 AM

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.

### #3scgames  Members

Posted 12 February 2005 - 07:10 AM

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.

### #4Zahlman  Members

Posted 12 February 2005 - 09:28 AM

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.

### #5scgames  Members

Posted 12 February 2005 - 09:47 AM

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.

### #6Zahlman  Members

Posted 12 February 2005 - 06:39 PM

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.

### #7Bas Zalmstra  Members

Posted 13 February 2005 - 08:03 PM

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.

### #8stevenmarky  Members

Posted 14 February 2005 - 12:18 AM

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 :/

### #9Cygon  Members

Posted 14 February 2005 - 12:27 AM

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-

### #10Bas Zalmstra  Members

Posted 23 February 2005 - 03:36 AM

Tnx, a lot I've got it now!
Now I need the same thing for a box against a box..
Any ideas? :)

### #11lonesock  Members

Posted 23 February 2005 - 04:30 AM

check out oliii's work in this thread.

Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.