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]

**0**

# Sphere-box collision

Started by Bas Zalmstra, Feb 12 2005 12:20 AM

10 replies to this topic

Sponsor:

###
#2
Members - Reputation: **253**

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.

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.

###
#3
Members - Reputation: **1977**

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.

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.

###
#4
Moderators - Reputation: **1682**

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.

###
#5
Members - Reputation: **1977**

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.

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.

###
#7
Members - Reputation: **129**

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.

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.

###
#8
Members - Reputation: **349**

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

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

###
#9
Crossbones+ - Reputation: **1106**

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

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

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

###
#11
Members - Reputation: **799**

Posted 23 February 2005 - 04:30 AM

check out oliii's work in this thread.

lonesockPiranha are people too.www.lonesock.netSOIL: Simple OpenGL Image LibraryMovies I've mocked: "The Core", "The Mummy", "Tale of Despereaux"