Collision Detection Presented by Jerry Ashcroft-Thew
What is collision? • Physics. the meeting of particles or of bodies in which each exerts a force upon the other, causing the exchange of energy or momentum. • Games. When two objects attempt to occupy the same space in the coordinate planes, or world.
When does collision matter? • Preventing two objects from ghosting through each other • Prevent an object from simply hovering when it should fall • Detect projectile contact for “hit” vs “miss” • When might you NOT want collision, when it would occur in real life?
Components of Collision • Detecting if a collision occurred • Reacting accordingly • Discovering if a collision has occurred is the basis of this problem.
Detecting collision • The first step is to find out if the camera or player will pass through any polygons or planes on its next move • All polygons are extended indefinitely along their plane. If the camera does not intersect the plane, then no calculations are necessary (less processing). When a collision with the plane is detected, then check if the camera intersects the polygon itself.
2D Collisions • Much easier to detect • World can be represented by 2D array grid
Tile Example • Detecting collisions with tiles • Player occupies one tile, all objects made up of tile sized pieces • When the player moves, check if the new tiles it occupies contains any objects we want to collide with instead. • Update player position accordingly
Collision Detection Mathematics • Vector - directed line segment with magnitude and direction. i.e. angle and length or distance. • Vector can also represent force or speed of an object • A scalar, as opposed to a vector, has magnitude, but not direction.
Vectors • Different ways to express vectors: • V = P2 - P1 • V = (x2 - x1, y2 - y1) • V = (Vx, Vy) • A 2D world has only x and y components, whereas a 3D world adds the z axis and another component to each vector.
Vectors cont. • Vectors determine how far apart objects. • The length of the vector can be found by the distance formula for two points • 2D Vector: |V| = sqrt(Vx2 + Vy2) • 3D Vector: |V| = sqrt(Vx2 + Vy2 + Vz2)
Normalizing Vectors • Off topic, but still important • Divide each component of the vector by its magnitude. • Creates Unit Vector • A plane's normal unit vector is important to provide realistic lighting and collision detection.
The discrete time issue: • What if objects collide between updates? • Imagine the following 2D scene: a black ball is standing still, • and a white ball moves quickly towards the black ball • If the white ball can cover more pixels in one frame than the black ball's diameter, we may not detect the collision
Solution: • Keep track of distance between relevant objects • If distance suddenly changes sign, i.e. becomes negative, we have passed through the object
Quick collision detection • Use Bounding Geometric shapes • If general shapes collide, assume collision occurred. • Sphere or circle, Cylinder or rectangle • Not very precise…
Sphere-Plane method • Uses the quick collision detection, but with polygons of the environment • Test if player-sphere intersects the plane of an objects polygon • The viewer or camera can be thought of as one solid entity, such as a ball, instead of a creature with several limbs.
Sphere-Plane cont. • Easier to calculate – all points on the sphere are equidistant from center • Thus it is easy to determine if an object intersected with the sphere • If distance to an object is less than or equal to radius, then collision occurred, and we react accordingly
Main point – don’t get too close! • Use planar equation Ax+By+Cz+D = 0 to determine normal vector, <A,B,C> • To find distance between sphere and plane of an objects surface, dot product the normal vector with the position of the sphere, or its center • Distance = Normal Vector of Plane DOT Position of Center of Sphere
The distance will be either positive or negative, depending on the side of the plane you are on. – Useful for the discrete time issue mentioned before • If the distance becomes less than the radius of the sphere, or if the sign of the distance vector to the wall changes, a collision has occurred
Reacting Accordingly • What are our options? • Stop altogether • Slide along surface • Slow down and push object • Bounce off object • Sink into object • Pass through object and ignore collision
The Sliding Plane • The plane with which we will continue our movement • Sliding Plane = Tangent plane to place on sphere which collides with the object Images taken from http://www.peroxide.dk/papers/collision/collision.pdf
To find the tangent plane, we need 2 things • Normal vector • Point on plane • Point on plane is point of intersection • Normal vector is simply vector from edge of sphere (our point of intersection) to center of sphere • Now we have the tangent plane, and thus the plane to move along after a collision!
Sliding the Sphere • Move as close to surface as possible • Find sliding plane • Project original velocity onto sliding plane to get new position • Make new velocity vector by finding distance from collision point to new position • Recursively call the entire collision detection routine with the new position and the new velocity vector.
Recursively call the collision function and check ALL nearby surfaces again. • Continue until: • No more collision is detected, or • Velocity vector is very small • Run into a corner, don’t want to slide through one wall because it was ignored. • More directly we hit a wall, the less we slide along its surface!
Gravity • Very easy once you have a working collision function • Just call the function twice, once with the velocity vector, and once with the gravity vector • Allows for climbing stairs and ramps, or falling from heights with automatic acceleration • NOTE: PAY ATTENTION!
Homework • When might you NOT want collision in a game, when it would occur in real life? • What is the limiting factor with combining gravity and velocity vectors into one call of the collision function instead of separating it out?
References • http://www.peroxide.dk/papers/collision/collision.pdf • http://www.edenwaith.com/products/pige/tutorials/collision.php • http://www.dictionary.com • Google image search