Animation and Games Development 242-515, Semester 1, 2014-2015 • Objective • to discuss some of the basic issues related to collision detection and collision response 13. Collision Detection
Overview • Collision Detection Uses 2. Collision Algorithms 3.Spatial Partitioning 4. Simplifying Shapes 5. Terrain Collision 6. Collision Time • The Tunneling Problem • More Complex Tunneling Solutions 9.Where do Objects Touch? 10. Collision Response Algorithms 11. Collision Changes 12. Momentum and Collisions 13. Complex Collisions 14. More Collision Issues 15.Summary
1. Collision Detection Uses • Determine if the player has a hit a wall or obstacle • Determine if a missile has hit a target • Detect when things should change • e.g. a hand touches a light switch • Clean up animation • Make sure a character’s feet touch the floor • To help implementing realistic motion • E.g. of cloth, water, fire
Collision Detection and Response • Collision Detection • Check for the intersection of two objects • Calculate the trajectories of the objects, impact times, and impact points • Collision Response • provide a response that fits the game and (customized) laws of physics
Types of Collisions • Main character and static objects: • terrain, floor, walls, furniture, buildings, etc. • Main character and dynamic objects: • enemies, bullets, arrows, particles, etc. • Dynamic game objects (e.g. enemies) and static and other dynamic objects.
Convex Concave An object is convex if for every pair of points inside the object, the line joining them is also inside the object 2. Collision Algorithms • Shapes can be very complex, potentially requiring expensive testing: • points, line, convex/concave objects • Shapes move in different ways: • Use different algorithms for fast or slow moving objects • The algorithms will also depend on how frequently an object must be updated
Design Principles • Design principles for collision algorithms. • Fast simple tests first to eliminate most potential collisions • e.g.: test bounding volumes before complex shapes • Exploit locality to eliminate many potential collisions • e.g. use spatial partitioning to avoid testing two objects that are far apart
Use as much geometry informationas possible • e.g. spheres have special properties that make collision testing faster • Exploit coherence between successive tests • coherence means that things (objects) tend to stay the same over time
Collisions in Pairs • Compare all game objects against all other objects, one pair at a time • This approach is easy to understand and to implement. • But it is slow -- O(n2) running time since every object can potentially collide with every other object.
Non-collidable Objects • Include non-collidable objects in your game: • background: sky, clouds, grass, etc • dead enemies (maybe) • unimportant game objects • No need to include in collision testing, so speeds up the game.
Collision Challenges • Question: • Do objects A and B intersect
Collision Challenges • Question: • Do objects A and B intersect • Challenge: • Can be in motion
Collision Challenges • Question: • Do objects A and B intersect • Challenge: • Can be in motion • Bigger Challenge: • Need to know whenthey collide. • Realistic physics needs the exact point of collision • Necessary for collision response
Time-based Techniques • Priori: check if something is going to collide before moving it, then make the necessary correction • Posteri: check if something has collided and make the necessary corrections
3. Spatial Partitioning • Common types of spatial partitioning: • Octrees (called quadtrees in 2D) • Uniform Grids • Others types: portals (used in Quake), BSP trees (Binary Space Partitions), spatial hashing
An Octree game regions game regions
Data stored in each region: • coordinates defining the region • though a smart implementation can greatly simplify this • a list of objects inside a region
Octree Drawbacks • Dynamic objects may move between regions • this requires the octree to be updated • A static (or dynamic) object may be positioned so it spans several regions • collision detection will involve objects from multiple regions
Uniform Grids • Steps: • divide the (2D or 3D) world into a grid of equal sized cells • associate each object with the cells it is located inside • only objects sharing a cell are tested for collisions
Calculating a Grid Position • What cells does the object currently occupy? • Possible calculations: • For min/max tile rows/columns: • sprite.X/tileWidth • sprite.Y/tileHeight • (sprite.X+sprite.Width)/tileWidth • (sprite.Y+sprite.Height)/tileHeight • For the 'man' object in cells (0,0), (0,1), (1,0), (1,1) • only test against: • other objects in those cells • also, don’t let an object occupy “collidable cells” (0,2), (1,2), etc 0 1 2 3 0 1 2
Advantages • easy to implement • very fast: O(n) • Disadvantages • constrains look of game to a grid-like world
4. Simplifying Shapes • Typically divide an object into convex shapes • Mathematically the easiest shapes to compute with • Assumed by most algorithms • What do you do if the shape is not convex? • Break it up into several parts
Bounding Volumes (BVs) • A simple geometry used in the collision tests instead of the shape’s real, complicated geometry. • Properties of good BVs: • inexpensive collision tests • tight fit to the original shape • inexpensive to create • easy to rotate and transform • uses little memory
Common Bounding Volumes • AABBs:axis-aligned bounding boxes • OBBs: Oriented bounding boxes • DOPs: Discrete oriented polytopes
AABBs • Axis Aligned Bounding-Boxes (AABBs) • Intersection is very cheap • Project rectangle onto axes • Two intervals per box • Check both intervals overlap • Poor fit for most objects
Do AABB's Collide? • Common solution: • axis separation test • More sophisticated solution: • Plane Sweep: an extension of axis separation which is more efficient when dealing with many objects
Axis Separation Test • How do we know if two AABBs are colliding? • their edges overlap on all axes NO COLLISION COLLISION A C B D
Plane Sweep • Requires sorting of the objects along the axes • Running time: O(n)
OBBs • Oriented Bounding Boxes (OBBs) • Tighter fit than AABB • Intersection more expensive • Must check 4 edges for overlap
DOPs • Discrete Oriented Polytopes (DOPs) • Captures almost all convex shapes • Intersection is more expensive • O(a2), a = number of edges
Capsule • Capsule • An OBB with rounded ends • Intersection cost is roughly the same • Efficient for swept shapes
Circles/Disks • Circles/Disks • Not cheaper than AABB • Poor fit for anything other than a circle
How about a Complicated Object? • We can use two AABBs to represent the object • Don’t test them against each other
Multiple Bounding Boxes • Complex objects can have multiple bounding boxes • e.g. a Human object can have one big bounding box covering the whole body; or • one bounding box per limb, head, etc • Bounding boxes can be hierarchical: • Test the big 'human' box first • If there is a possible collision, test the smaller boxes inside the big one (e.g. the limb, head boxes)
Bounding Box Hierarchies • Build a tree of boxes: • a parent box surrounds all the boxes of its children • Intersection test against a bounding box tree by travelling down the tree to a leaf box. • Generally used to test a moving object against lots of static obstacles • Put all the static obstacles into one large box tree
5. Terrain Collision • Utilizes the heightmap information, and the fact that the terrain is a mesh of triangles.
Locate Triangle on Mesh Simplifies to a 2D problem of points inside triangles.
Barycentric Coordinates • We can use Barycentric coordinates to test if a point is inside a triangle.
6. Collision Time • Given a ball with center at position p0= (x1, x2, x3), with initial velocity v = (v1, v2, v3), and a constant acceleration a = (a1, a2, a3). • When will its center hit a plane defined by f(x, y, z) = 0 ? • p = p0 + vt + ½ at2 • Solve the equation f(p) = 0
Time Interval Problem • If the game is running at 30 fps, it only renders 1/30th of the object's states • sometimes called the Flipbook syndrome • Many things happen that we don’t see We don’t see the ball hit the ground
7. The Tunneling Problem • An object moves through another object because the collision testing is not done at the 'right' time: 8 7 6 time: 5
Tunneling is a Big Problem • Things can fall through the floor • Cars can pass through people or walls • Players can enter rooms with no doors • Players can travel over holes • etc...
Tunneling • Small objects can tunnel more easily than large shapes: