Page 1 of 1

Collision of bounding box and sphere

Posted: Thu Jan 24, 2008 12:08 am
by Sylence
Hi!

Currently I'm doing a little space game but I ran into a problem:
I have a cube scene node and a sphere scene node (the cube is textured with an image of a spaceship and the sphere is a planet) but how can I check if the cube collides with the sphere?

The bounding box of the sphere is too big for my needs (it can be intersected at the "edges" that are far away from the real sphere)

Thanks in advance for any help

Re: Collision of bounding box and sphere

Posted: Thu Jan 24, 2008 1:11 am
by randomMesh
Sylence wrote:...how can I check if the cube collides with the sphere?
Maybe this helps?

Posted: Thu Jan 24, 2008 2:52 am
by bitplane
IIRC there's box-ellipsoid intersection methods in the bounding box selector. Another option is a combination of aabbox3d::isPointInside and plane3d::getDistanceFrom.

Posted: Thu Jan 24, 2008 3:55 pm
by radubolovan
1) calculate the direction:
vector3df dir = ( sphere->getPosition() + box->getPosition() ).Normalize();
2) calculate a point on this direction which is on sphere:
vector3df sphereVertex = sphereRadius * dir;
3) test if this point is in bounding box:
bool collision = box.isPointInside( sphereVertex );
If collision is true ... the two objects intersects!