Download presentation
Presentation is loading. Please wait.
Published byChad Knight Modified over 6 years ago
1
Intro & Point-to-Box, Circle-to-Circle, Point-to-Circle
Collision Detection Intro & Point-to-Box, Circle-to-Circle, Point-to-Circle
2
15 pixel movement…oops, passed through
Introduction Collision detection is the process of determining when two objects touch (intersect) This is one of the most common operations in gaming, therefore it is also one that is most focused on when ensuring efficiency in our programs Imagine a game with dozens of characters on screen and hundreds of bullets. Each bullet would have to be tested against each character…60 times per second Every type of collision is determined differently depending on shape and speed of the objects being tested For example, if you have a bullet moving at 15 pixels per update and you are testing to see if it collides with a target 5 pixels wide, it is very possible that the collision is missed as its placement before and after the update most likely is on either side of the target. TARGET bullet Pre Update 15 pixel movement…oops, passed through Post Update
3
Point-to-Box To detect whether a point is touching a box we simply need to test whether it lies between all four walls of the box. The most common use for this collision check is the between the mouse and graphical buttons Step 1: Define each shape: A point is simply an (x,y) coordinate 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. w (x,y) (x + w,y) h (pX,pY) (x,y + h) (x + w,y + h)
4
Point-to-Box: Test The test is a simple four part compound if statement. If the point’s x value is between both the left and right wall of the box and the point’s y value is between both the top and bottom wall of the box there is a collision Collisions tests are typically done in a subprogram that return true for collision, false otherwise. Its parameters are the shapes being tested: private boolean PointBoxTest(Vector2F pt, Rectangle box) { if (pt.x >= box.x && pt.x <= (rec.x + rec.width) && pt.y >= box.y && pt.y <= (rec.y + rec.height)) { return true; } else { return false; } }
5
Circle-to-Circle To detect whether two circles of arbitrary size are touching we need to see if the distance between their centres is less than the sum of their radii Step 1: Define each shape Circles are defined by a centre (x,y) point and a radius radius (x,y)
6
Circle-to-Circle Test
(c1x,c1y) (c2x,c2y) r1 r2 Circle-to-Circle Test The test is a single calculation followed by a simple if statement Calculate the distance between the two centres Compare the distance against (radius1 + radius2) The distance can be calculated using the Pythagorean theorem where the distance is the hypotenuse and the other two sides are simply the difference (subtraction) between the points a2 + b2 = c2 (a2 + b2) = distance ((c2x – c1x)2 + (c2y – c1y)2) = distance If the distance <= (r1 + r2) then there is a collision
7
Circle-to-Circle Code
(c1x,c1y) (c2x,c2y) r1 r2 Circle-to-Circle Code private boolean CircleCircleTest(Vector2F c1, float rad1, Vector2F c2, float rad2) { float dist = Math.sqrt(Math.pow((c2.x-c1.x),2) + Math.pow((c2.y – c1.y),2)); if (dist <= (rad1 + rad2)) { return true; } else { return false; } } The problem is, square roots are very expensive on the CPU, if we can avoid them we should. How do we get rid of a square root in an equation, we square both sides: so ((c2x – c1x)2 + (c2y – c1y)2) <= (r1 + r2) becomes ((c2x – c1x)2 + (c2y – c1y)2) <= (r1 + r2)2 private boolean CircleCircleTest(Vector2F c1, float rad1, Vector2F c2, float rad2) { float dist = Math.pow((c2.x-c1.x),2) + Math.pow((c2.y – c1.y),2); if (dist <= Math.pow((rad1 + rad2),2)) { return true; } else { return false; } }
8
Point-to-Circle We need to calculate the distance from the point to the circle’s centre, if that distance is less than the radius then there is a collision We have already defined both circles and points in previous slides We have also discussed how to calculate the distance between two points Finally, we have discussed a CPU friendly shortcut of eliminating the square root calculation. Let’s put it together
9
Point-to-Circle Test Calculate the distance between the point and the circle centre Compare that distance to the circle’s radius private boolean PointCircleTest(Vector2F pt, Vector2F centre, float rad) { float dist = Math.pow((centre.x-pt.x),2) + Math.pow((centre.y – pt.y),2); if (dist <= Math.pow(rad,2)) { return true; } else { return false; } }
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.