#### Archived

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

# Bounding Box Collision Detection - HELP??

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

## 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 on other sites
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 on other sites
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)

1. 1
Rutin
44
2. 2
3. 3
4. 4
5. 5

• 11
• 9
• 12
• 10
• 13
• ### Forum Statistics

• Total Topics
632984
• Total Posts
3009712
• ### Who's Online (See full list)

There are no registered users currently online

×