Bounding Box Collision Detection - HELP??

Started by
1 comment, last by Chatterbox 22 years, 2 months ago
Hi, I need some help with bounding box collision detection. I have read many articles on the Internet but they have just confused me. Would anyone be able to assist me with this. Thx - Chatterbox
Advertisement
Bounding box collision detection works because of the principle that any object will have some ''box'' the completely surrounds it. For example, if you have a sphere (a ball or something), there is a rectangular prism (box) that fits around it. This is useful because it is much easier to tell if two boxes are touching than it is to tell if two arbitrary shapes are touching. Say you have two spaceships that you want to check for collision: would it be faster to see if the boxes surrounding them touched or to check to see if every single engine, turret, viewport, and port hole touched?

To use bounding box collision detection, you''ll need to establish a box for each one of your objects. Often times this is done by finding the biggest and smallest x, y, and z values of your models and using those to construct what''s called an axis-aligned bounding box, but there are other methods. Which one you use depends on which is better suited for your program. Anyway, after you''ve set up the boxes for each of your objects, collision detection is simple: test for intersection between boxes. I''m sure there are more detailed descriptions on the internet - I know you said they confused you before, but I hope now you can understand them. A basic study of a geometry text book might help you there.

Hope this helped ~
~ riley
--Riley
Here''s how I look at it:

  Box 1:x1,y1__|      ||      ||______x2,y2Box 2:x1,y1__|      ||      ||______x2,y2  


You have to check if one is over-lapping the other, hence, this equation:

(x2 >= x3) && (x4 >= x1) && (y2 >= y3) && (y4 >= y1)

If you''re wondering how to implement this in your code:

  //global variablebool collided = false;//collision checking functionbool CheckCollision(int x1, int x3, int y1, int y3){     if((x1 + widthOfBoxOne >= x3) && (x3 + widthOfBoxTwo >= x1) && (y1 + heightOfBoxOne >= y3) && (y3 + heightOfBoxTwo >= y1)     {          return true;     }     else     {          return false;     }}//and then in main, call it like this:collided = CheckCollision(BoxOne_x, BoxOne_y, BoxTwo_x, BoxTwo_y);if(collided){     DoSomething();     //this is the collision response}  


Hope that helps!

------------------------------
Simple DirectMedia Layer:

Main Site - (www.libsdl.org)
Cone3D Tutorials- (cone3D.gamedev.net)
GameDev.net''s Tutorials - (Here)

OpenGL:

Main Site - (www.opengl.org)
NeHe Tutorials - (nehe.gamedev.net)
Online Books - (Red Book) (Blue Book)
------------------------------Put THAT in your smoke and pipe it

This topic is closed to new replies.

Advertisement