Jump to content
  • Advertisement

Archived

This topic is now archived and is closed to further replies.

Chatterbox

Bounding Box Collision Detection - HELP??

This topic is 5991 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
Here''s how I look at it:

  
Box 1:

x1,y1__
| |
| |
|______x2,y2

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

bool collided = false;

//collision checking function

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

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!