Collision Detection

Collision Detection
The Story so Far
• Rigid bodies moving in space according to forces
applied on them.
• Gravity, drag, rotation, etc.
• Reaction forces occur when a rigid body is in
contact with another body.
• Handling the event correctly is then two problems:
• Collision detection
• Collision solving
Game Physics
2
Collisions & Geometry
• We need the actual geometry of the object
• A point (e.g. COM) is not enough anymore.
• We must know where the objects are in contact to apply
the reaction force at that position.
CryEngine 3
(BeamNG)
Game Physics
3
Collision Detection Algorithms
• To save time and computation, collision detection
is done top-down, to rule out non-collisions fast:
– Broad phase
• Disregard pairs of objects that cannot collide.
 Model and space partitioning.
– Mid phase
• Determine potentially-colliding primitives.
 movement bounds.
– Narrow phase
• determine exact contact between two shapes.
 Triangle-triangle intersections.
Game Physics
4
Broad Phase
Can they possibly roughly collide at
all?
Collisions and Geometry
• Problem: Comparing ‘every vertex of every mesh’
at each frame is not possible in real-time.
• Solution: rule out most non-collision by
approximating intricate shapes with primitive
shapes
• Estimating geometry and inertia.
• Collision shapes do not have to be the same as inertia
shapes.
Game Physics
6
Bounding Volumes
• Quickly check complex objects using
approximating bounding volumes.
• For a quality bounding volume:
•
•
•
•
As tight as possible fit to the object.
Fast overlap test with another volume.
Described with little parameters.
Fast to recalibrate under transformation.
Game Physics
7
Convex Hull
• The smallest convex surface/volume enclosing the
object:
• Exact representation of convex objects.
• Many false positive collisions for highly-concave objects.
• Might be yet complex, so costly detection.
Game Physics
8
Bounding Sphere
• The minimal sphere enclosing the object:
• Usually a poor fit. (e.g. pipe).
• Stored in only 4 scalars, collision detection between
spheres is very effective (11 prim. op.)
• Trivial to update under rotations.
Game Physics
9
Bounding Capsule
• The minimal bounding swept sphere enclosing the
object:
• Better fit than bounding sphere.
• Collision detection still quite fast.
Game Physics
10
Axis-Aligned Bounding Box
• A box which axes are aligned with the axes of the
world coordinate system:
• Usually poor fit of the object (e.g. diagonal box), many
false positive collisions, recalculation after rotation.
• Stored in 6 scalars, collision detection between AABB is
very fast (6 prim. op.).
Game Physics
11
Oriented Bounding Box
• The general minimal bounding:
• Better fit than AABB, but worse than convex hull (e.g.
triangle).
• Stored in 9+6 scalars, collision detection slower than AABB
(200 prim. op.), but much faster than convex hull.
• Similar to
bounding capsule
with sharp ends.
Game Physics
12
Other Primitives
• Anything else which might prove faster:
• Bounding cylinder
• Bounding ellipsoid
• etc.
• Context related to the objects in the scene.
http://flylib.com/books/2/587/1/html/2/files/fig80_01.jpg
Game Physics
13
Bounding Hierarchies
• A single volume for the entire shape: too many false
positives.
• Build Bounding Volume Hierarchy (BVH)
• Tree structure:
• Primitive volumes as leaves.
• Enclosing volumes as nodes.
• Children are checked only when parents intersect.
Game Physics
14
Space Partitioning
• Fast selection of which models to test for collision.
• Based on the spatial configuration of the scene.
• Associate objects that are physically close.
• Only testing object collision in the same partition.
• Quickly disregards many unnecessary tests.
Game Physics
15
Octree
• each node has exactly eight children.
• Partition each parent cube into eight cubes (called
octants) of equal volume along the dimensions of
the space.
Game Physics
16
Kd-Tree
• A Kd-tree (k-dimensional) is a binary tree where
every node is alternately associated with one of
the k-dimensions.
• The median hyperplane is chosen at each node.
Game Physics
17
Binary Space Partitioning (BSP)
• Hyperplanes recursively partition space into two
volumes.
• The planes can have any orientation.
• Hyperplanes are usually chosen for polygons in
the scene.
A1
PART 3
4
PART
2
3
A
PART 1
A2
1
B1
A3
2
PART
1
PART 6
PART 4
B2
PART 2
A4
3
PART
5
2 B 1
PART
4
PART 5
B3
PART 6
Game Physics
PART 3
A
B
18
Space-Partitioning Summary
Uniform spatial
subdivision
Quadtree (2D)
Octree (3D)
Kd-tree
Game Physics
BSP-tree
19
Mid Phase
How do primitives intersect?
Collision between Primitives
• Recap: representing with suiting primitives:
•
•
•
•
•
A simple convex object => convex hull.
A spherical object like a ball => bounding sphere.
A body part => bounding capsule.
A box sliding on the floor => AABB.
A box-like object that can translate and rotate => OBB.
• Need detection algorithms for every possible
combination of primitives.
• Some are easier to implement than others…
Game Physics
21
Sphere-Sphere
• Spheres 𝐴 and 𝐵 intersect  the distance
between centers 𝑐𝐴 and 𝑐𝐵 is smaller than the sum
of radii 𝑟𝐴 and 𝑟𝐵 :
𝐴 ∩ 𝐵 ≠ ∅ ⇔ 𝑐𝐴 − 𝑐𝐵 ≤ 𝑟𝐴 + 𝑟𝐵
• Distance between two non-intersecting spheres:
𝑑 𝐴, 𝐵 = max 𝑐𝐴 − 𝑐𝐵 − 𝑟𝐴 + 𝑟𝐵 , 0
• Penetration depth:
𝑝 𝐴, 𝐵 = max 𝑟𝐴 + 𝑟𝐵 − 𝑐𝐴 − 𝑐𝐵 , 0
Game Physics
22
AABB-AABB
• Project boxes onto the axes
• Obtaining two/three intervals per box.
• the boxes collide if all intervals overlap.
A
A
B
B
Game Physics
23
AABB-AABB
𝐴∩𝐵 =∅⇔
𝑥𝑚𝑎𝑥 𝐴 < 𝑥𝑚𝑖𝑛 𝐵 ∨ 𝑦𝑚𝑎𝑥 𝐴 < 𝑦𝑚𝑖𝑛 𝐵 ∨
𝑥𝑚𝑖𝑛 𝐴 > 𝑥𝑚𝑎𝑥 𝐵 ∨ 𝑦𝑚𝑖𝑛 𝐴 > 𝑦𝑚𝑎𝑥 𝐵
𝑦𝑚𝑎𝑥𝐴
𝑦
A
𝑦𝑚𝑎𝑥𝐵
𝑦𝑚𝑖𝑛𝐴
𝑦𝑚𝑖𝑛𝐵
B
𝑥𝑚𝑖𝑛𝐴
𝑥𝑚𝑎𝑥𝐴
𝑥𝑚𝑖𝑛𝐵
Game Physics
𝑥𝑚𝑎𝑥𝐵
𝑥
24
Separating-Axis Theorem
• Given two convex shapes, if there is an axis along
which the projections of the two shapes do not
overlap => the shapes do not collide.
Game Physics
25
Separating-Axis Theorem
• In 2D, each of these potential separating axes is
perpendicular to one of the edges of each shape.
• Solving using a series of 1D queries.
• Finding such an axis => no overlap => queries
stop.
• It is more likely for two objects (in a game)
to not overlap, thus this speeds up calculations.
Game Physics
26
Separating-Axis Theorem
• For AABB-AABB it is easy to apply.
• The possible separating axes are the main axes.
• Equivalent to our previous collision checking of
overlap of intervals.
• For non-axis-aligned shapes, we have to project
our objects on the axes perpendicular to the
edges.
Game Physics
27
Sweep & Prune Algorithm
• First sort objects, then prune.
• Objects are defined with their AABB.
• 2 objects overlap  their projections on the x, y
and z coordinate axes overlap
•
•
•
•
The projections give 3 [min,max] intervals
The min and max are stored in 3 sorted structures
Scan the objects in increasing order of min.
Detect possible overlapping pair when min of an object
is smaller than max of another.
• Combine the three results (AND condition to overlap).
Game Physics
28
Sweep & Prune Algorithm
x, y or z axis
A
B
C
D
CurrentObjects [] [C]
CandidatePairs
[C,A]
[CA]
[C,A,B] [A,B]
∪
[BC,BA]
[A] [A,D] [A] []
∪
[DA]
=
[CA,BC,BA,DA]
Game Physics
29
The Time Issue
• Looking at uncorrelated sequences of positions is
not enough.
• Our objects are in motion and we need to know
when and where they collide.
At 𝑡
At 𝑡 + ∆𝑡
Game Physics
30
Tunneling
• Collision in-between steps can lead to tunneling.
• Objects pass through each other
• Colliding neither at 𝑡 nor at 𝑡 + ∆𝑡!
• …but somewhere in between.
• Leads to false negatives.
• Tunneling is a serious issue in gameplay.
• Players getting to places they should not.
• Projectiles passing through characters and walls.
• Impossibility for the player to trigger actions on contact
events.
Game Physics
31
Tunneling
Game Physics
32
Tunneling
• Small objects tunnel more easily
• Fast moving objects tunnel more easily
Game Physics
33
Tunneling
• Possible solutions
• Minimum size requirement?
• Fast object still tunnel…
• Maximum speed limit?
• Small and fast objects not allowed (e.g. bullets...)
• Smaller time step?
• Essentially the same as speed limit!
• Another approach needed.
Game Physics
34
Movement Bounds
• Bounds enclosing the
motion of the shape
• In the time interval
∆𝑡, the linear motion
of the shape is
enclosed.
• Convex bounds are
used => movement
bounds are also
primitive shapes.
Sphere
AABB
OBB
Game Physics
35
Movement Bounds
• If movement bounds do not collide, there is no
collision
• If movement bounds collide, there is possibly a
collision.
Game Physics
36
Swept Bounds
• As primitive based movement bounds do not have
a really good fit, we can use swept bounds.
• More accurate, but more costly.
• Constructed from the union of all surfaces
(volumes) of a shape under a transformation
• We use the affine transformation from 𝑡 to t + ∆𝑡.
Game Physics
37
Swept Bounds
• Swept sphere
 capsule
• Swept AABB
 convex poly
• Swept triangle
 convex poly
• Swept convex poly
 convex poly
Game Physics
38