Download presentation
Presentation is loading. Please wait.
1
Collision Detection Box-to-Box
2
If collision, return true for that body part.
Introduction Box to box collision tests are among the most used in 2D games. The simple reason for this, is that all images in 2D games are actually rectangles. Meaning, every image has an invisible rectangle tightly bound to the width and height of the image. We call this rectangle the bounding box Sometimes we will break body parts up into their own bounding boxes to determine which part of the body was actually hit However that means a collision test would need to be done on each body part 60 times/second, to make this more efficient we do what is called 2 stage collision detection. Stage 1: Bounding Box Test Stage 2: Sub Collision Tests If collision Go to Stage 2 If collision, return true for that body part. And stop further tests
3
Box-to-Box (AABB Test)
Also called the Axis Aligned Bounding Box (AABB) Test, meaning non rotated rectangles of any size. The problem with detecting whether two arbitrarily sized boxes collide, is that there are so many possibilities… As you can see, there are a lot of scenarios to cover. Too many for an efficient test that is needed to be done for every image against every other image in the game, 60 times/second We need a better way. The solution, we flip the script. Instead of testing for all the possible collisions, we will test for only the impossible collisions
4
Test the Impossible… There are only 4 scenarios in which it is completely impossible for two boxes to collide. Box A’s right wall is to the left of Box B’s left wall (too far left) Box A’s left wall is to the right of Box B’s right wall (too far right) Box A’s bottom wall is above Box B’s top wall (too far above) Box A’s top wall is below Box B’s bottom wall (too far below) If at least one of these tests is true it is impossible for a collision to occur, return false. (compound OR statement) However, if ALL four tests fail, the only logical conclusion is the opposite of an impossible collision, a definite collision, return true. Box B A A A A
5
Box-to-Box Step 1: Define the shape
A box is an (x,y) coordinate (top left corner) and a width and height Step 2: Understand the dimensions of your shapes We know the coordinate of the top left corner of the box, we can calculate the other four corners using this information as well as the box’s dimensions. To take this one step further, we can define the four walls with respect to these values to make our logic simpler to read and understand Left Wall: x Right Wall: x + w Top Wall: y Bottom Wall: y + h w (x,y) (x + w,y) h (x,y + h) (x + w,y + h)
6
Box-to-Box: Test The test is a simple four part compound if statement testing each impossible collision scenario If any impossible test is true, there is no collision, return false. If all four tests are false, there must be a collision, return true. We can use the original dimensions of the shapes or to make things more readable, you could define variables for the four walls private boolean BoxBoxTest(Rectangle box1, Rectangle box2) { if ((box1.x + box1.width) < box2.x) || //(b1.right < b2.left) box1.x > (box2.x + box2.width) || //(b1.left > b1.right) (box1.y + box1.height) < box2.y) || //(b1.bottom < b2.top) box1.y > (box2.y + box2.height)) || //(b1.top > b2.bottom) { //impossible collision return false; } else { //definite collision return true; } }
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.