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
Collision of bounding box and sphere
Collision of bounding box and sphere
Software documentation is like sex. If it's good you want more. If it's bad it's better than nothing.
-
- Posts: 1186
- Joined: Fri Dec 29, 2006 12:04 am
Re: Collision of bounding box and sphere
Maybe this helps?Sylence wrote:...how can I check if the cube collides with the sphere?
-
- Posts: 60
- Joined: Tue Nov 13, 2007 7:03 pm
- Location: Bucharest - Romania
- Contact:
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!
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!