Collision Detection an Overview

33
Collision Detection: an Overview James McLaren June 19 2009

description

An overview of different techniques that can be used for spatially portioning your world geometry, in association with collision detection

Transcript of Collision Detection an Overview

Page 1: Collision Detection an Overview

Collision Detection: an Overview

James McLaren

June 19 2009

Page 2: Collision Detection an Overview

About this talk

• Brief overview of the Collision detection.

• Deliberately not heavy on the math (Otherwise I’ll stand here all day).

• Not exhaustive – It’s a big area, lots of ways of doing things. Hopefully this will provide some kind of cook book.

Page 3: Collision Detection an Overview

Stages of Collision Detection

• Broad Phase– Use a some kind of Spatial Subdivision to reduce our

algorithmic complexity.

• Narrow Phase– Volume x Volume– Polygon tests

• Response– Try to do something sensible when collisions occur.

• Ray Casts– Auxiliary tests used for line of sight, bullets etc.

Page 4: Collision Detection an Overview

Broad Phase

• Naïve checking of all objects against each other would be O(n2) == BAD!

• Need to find ways to trivially reject objects that are far away

• Typically use a spatial data structure

• May also take advantage of frame to frame coherence.

Page 5: Collision Detection an Overview

Spatial Data Structures

• Uniform Grids

• Hierarchical Grids

• Octree/Quad-Tree

• BSP-Tree

• KD-Tree

• Axis sorted lists (i.e. Sweep and Prune)

Page 6: Collision Detection an Overview

Uniform Grid

• Simply divide geometry/objects into a grid

• Easy to work with.

• Fast insertion.

• Grid size is important.– Objects can be in many cells.

– Too small -> explosion in storage

– Too large -> reduced benefit

• Can be combined with hashing.

• Various hierarchical schemes exist.

Page 7: Collision Detection an Overview

Octree

• Each branch node divides space into 8 octants

• Can be expensive to update if dataset is dynamic (i.e. moving objects).

Page 8: Collision Detection an Overview

KD-Tree

• Each node is a axis aligned hyper-plane in a k-dimensional space (usually 2 or 3 for us)

• Point in front of the plane are in the left branch, those behind are in the right

Page 9: Collision Detection an Overview

Binary Space Partition Tree

• Same principle as a KD-Tree, but more generalized.

• Each node stores a full plane (no axis alignment restrictions).

• For our purposes leaf nodes usually end up containing convex sets of polygons .

• Often used for Ray casting

• Also useful for rendering (can easily sort polygons back to front with respect to the camera)

Page 10: Collision Detection an Overview

Binary Space Partition Tree

Page 11: Collision Detection an Overview

Sweep and Prune

• Popular Broad Phase method for dealing with lots of dynamic objects.

• Makes good use of frame to frame coherence

• Objects are represented by an axis aligned bounding box

• Algorithm maintains sorted lists of min and max edges for each dimension (typically 3)

• Also maintains a set of objects that are currently overlapping

Page 12: Collision Detection an Overview

Sweep and Prune

• Each frame an insertion sort is performed on each list with the objects new positions

• When min and max edges are swapped for two objects we know a collision may have occurred, or that the objects no longer overlap.

• AABB tests can be done to confirm an overlap

• A bit set for each object-object pair can also be used (less testing but much bigger memory overhead).

Page 13: Collision Detection an Overview

Sweep and Prune

Page 14: Collision Detection an Overview

Broad Phase Considerations

• Many options, important to appropriate data structures.

• Often best to treat static and dynamic objects differently. Some Data structures handle real time updates much better than others i.e.

– Grids/ Sweep and Prune good for dynamic scenes.

– Octree/BSP Tree etc often better suited to static scenes.

Page 15: Collision Detection an Overview

Broad Phase Considerations

• An important recent consideration for the Broad Phase is multi-core. If possible we want to identify “Islands” of objects that for the purposes of this time step can be treated independently (possibly by multiple cores).

• Proximity queries can be integrated into the broad phase (Tell me all the objects in this area).

• Often want to filter based on object groups (No point checking bullets vs bullets or proximity checks vs proximity checks).

Page 16: Collision Detection an Overview

Narrow Phase

• Need to perform more exact tests on the set of potentially colliding candidates found in the Broad Phase

• Object representations will typically take the form of either:

– A bounding volume (usually convex)

– A hierarchy of bounding volumes

– Polygon soup (possibly underneath the volumes)

Page 17: Collision Detection an Overview

Bounding Volumes

• Objects can be represented as a single bounding volume.

• Usually these are convex

– Simpler to deal with than non-convex

– Can make use of Separating Axis Theorem:

– Several popular algorithms based on convexity

Page 18: Collision Detection an Overview

Separating Axis Theorem

• Theorem: “Given two convex shapes, there exists an axis where their projections can be separated if and only if they are not intersecting”.

• Is the basis of many collision detection checks.

• Typically there are a fixed number of axis that need to be checked in order to guarantee there is no collision.– For OBB-OBB checks, this is 15 (6 for the axis for each box, and 9 from

the cross products between them).

• Often gives good “early out” behaviour.– If the objects don’t collide, we will usually find a separating axis after

the first few tests.

Page 19: Collision Detection an Overview

Separating Axis

Page 20: Collision Detection an Overview

Typical bounding volumes

• Spheres

• Ellipsoids (easy to reduce to being spheres)

• Oriented bounding box

• Capsules

• Cylinders

• Convex hulls

Page 21: Collision Detection an Overview

V-Clip

• One of several algorithms which work with pairs of convex hulls.

• Theorem: Let FA and FB be a pair of features from two disjoint convex polyhedra, A and B, and let VR(FA) and VR(FB) denote their Voronoi regions. Let PA ∈ FA and PB ∈ FB be a pair of closest points between the features. Then, if PA ∈ VR(FB) and PB∈ VR(FA), FA and FB are a globally closest pair of features and PA and PB are a globally closest pair of points between A and B (albeit not necessarily unique).

Page 22: Collision Detection an Overview

V-Clip

• Based on the Voronoi diagrams of the two hulls

• Makes use of frame to frame coherence by tracking the closest features between the two objects

• In most cases the closest features from the last frame will be the same.

• If not then we search for the new closest features using the old ones as an initial best guess

Page 23: Collision Detection an Overview

V-Clip

• The vertex feature pair V and F constitute the closest pair of features and containing the closest pair of points Pa and Pb

Page 24: Collision Detection an Overview

Bounding Volume Hierarchies

• Convex volumes often don’t give a very good representation of the object

• Subdividing using a hierarchy of volumes allows us to represent more complex shapes.

• (Yes this is another spatial data structure!)• Sphere trees and OBB trees are both popular.

Page 25: Collision Detection an Overview

Triangle tests

• Often at the bottom of our hierarchy we need to test against the individual triangles of our mesh.

• This might be a test against other triangles, or possibly simple volumes such as a sphere.

• Several well know algorithms for these tests.

• If there is a collision then we want to return the position , time and normal of the collision.

• Also need to return the penetration distance if the objects intersect.

Page 26: Collision Detection an Overview

Collision Response

• Once we have detected a collision, we need to resolve it somehow.

• Dependent on the requirements of the simulation• In a physics based simulation will probably either:

– Apply impulses to the objects to instantaneously change their velocity.

– Let objects penetrate and apply repulsion forces based on the penetration distance ( Can have some instability issues).

– Possibly also generate contact points to specify constraints.

Page 27: Collision Detection an Overview

Collision Response

• If we are non physics based:– We can just wind the objects back to the point of

collision, and inform the game.– Punt and just inform the game.– We also have the freedom to move objects in turn

rather than together, at fast frame rates the effects of this can be negligable

• Must also take note of the fact that if we are simulating collisions mid frame, then our objects now have the potential to collide with different objects

Page 28: Collision Detection an Overview

Penetration

• In an ideal world we detect all our collisions at the instant they occur.

• However, various things can cause this to not be the case:– We are not using swept collision tests– We are dealing with rotating objects– The game logic has decided to teleport you somewhere

• Finding the right direction to move objects to stop penetrating can be challenging.– Resolving one penetration may well cause another– Often use relaxation (resolve, check, resolve, check) until

we are ok, or hit some maximum iteration count.

Page 29: Collision Detection an Overview

Wonderful Rotation

• Most of the standard swept primitive/volume algorithms quietly ignore the angular velocity.

• This means that you have to make instantaneous changes in rotation combined with swept updates along the velocity vector.

• When this causes a penetration you have to either:– Move the objects out of each other.

– Subdivide down to a smaller fraction of your time step and try again.

Page 30: Collision Detection an Overview

Ray Casts

• The other main form of collision query in games.

• Used for line of sight checks, bullets etc.

• Rich history in Ray Tracing literature.

• Also generally make use of some form of spatial data structure to accelerate the checks

• Test can be optimized if the ray is walked from front to back through the spatial subdivision.

• Also possible to batch ray traversals to try to get better cache behavior.

Page 31: Collision Detection an Overview

Ray Traversal

• Example below is a ray walking over a uniform grid (Technique used in PMC and FTB3).

• Note the difference from typical Bresenham line.

• We can early out the algorithm once we detect a collision.

• This kind of traversal is also possible with BSP/Octrees etc.

Page 32: Collision Detection an Overview

References

• Real-Time Collision Detection by ChristerEricson.

• http://realtimecollisiondetection.net/

• http://www.its.caltech.edu/~jiegao/collision-detection.html

Page 33: Collision Detection an Overview

Questions?