Collision Detection
Tomas Akenine-Möller
Department of Computer Engineering
Chalmers University of Technology
Introduction
Without collision detection (CD), it is practically
impossible to e.g., games, movie production
tools (e.g., Toy Story)
Because, without CD, we’ll get ”quantum
effects” all the time
– Objects will pass through other objects
So, CD is a way of increasing the level of
realism
Not a pure CG algorithm, but extremely
important
– And we have many building blocks in place already
(spatial data structures, intersection testing)
Tomas Akenine-Mőller © 2002
What we’ll treat today
Three techniques:
1) Using ray tracing
– Very simple
– Not accurate
– Very fast
– Sometimes sufficient
2) Using bounding volume hierarchies
– More complicated
– More accurate
– Slower
– Can compute exact results
3) Efficient CD for several hundreds of objects
Tomas Akenine-Mőller © 2002
In general
Three major parts
– Collision detection
– Collision determination
– Collision response
We’ll deal with the two first
– The third involves physically-based animation
Use rays for simple applications
Use BVHs to test two complex objects
against each other
But what if several hundreds of objects?
Tomas Akenine-Mőller © 2002
For many, many objects…
Test BV of each object against BV of other
object
Works for small sets, but not very clever
Reason…
Assume moving n objects
n
Gives: tests
2
n
If m static objects, then: nm
2
There are smarter ways: third topic of CD
lecture Tomas Akenine-Mőller © 2002
Collision detection with rays
Imagine a car is driving on a road sloping
upwards
Could test all triangles of all wheels against
road geometry
For certain applications, we can approximate,
and still get a good result
Idea: approximate a complex object with a set
of rays
Tomas Akenine-Mőller © 2002
CD with rays, cont’d
Put a ray at each wheel
Compute the closest intersection
distance, t, between ray and road
geometry
If t=0, then car is on the road
If t>0, then car is flying above road
If t<0, then car is ploughing deep in the
road
Use values of t to compute a simple
collision response
Tomas Akenine-Mőller © 2002
CD with rays, cont’d
We have simplified car, but not the road
Turn to spatial data structures for the
road
Use BVH or BSP tree, for example
The distance along ray can be negative
Therefore, either search ray in both
positive and negative direction
Or move back ray, until it is outside the
BV of the road geometry
Tomas Akenine-Mőller © 2002
Another simplification
Sometimes 3D can be turned into 2D
operations
Example: maze
A human walking in maze,
can be approximated by a
circle
Test circle against lines of
maze
Or even better, move walls outwards with circle
radius
test center of circle against moved walls
Tomas Akenine-Mőller © 2002
A CD system for accurate detection
and for many objects
We’ll deal with ”pruning” and ”exact CD”
”Simulation” is how objects move
Tomas Akenine-Mőller © 2002
Complex object against
complex object
Ifaccurate result is needed, turn to BVHs
Use a separate BVH for the two objects
Test BVH against other BVH for overlap
When triangles overlap, compute exact
intersection, if needed
But, first, a clarification on BVH building
Tomas Akenine-Mőller © 2002
BVH building example
Can split on triangle level as well (not
clear from previous presentation)
Use split Sort using
plane plane, w.r.t
triangle
centroids
Find minimal
= + boxes
…and so on.
Tomas Akenine-Mőller © 2002
Pseudo code for BVH against BVH
Pseudocode
deals with 4 cases:
1) Leaf against
leaf node
2) Internal node
against internal node
3) Internal against leaf
4) Leaf against internal
PSEUDO coden ÄR fel
Tomas Akenine-Mőller © 2002
Comments on pseudocode
The code terminated when it found the
first triangle pair that collided
Simple to modify code to continue
traversal and put each pair in a list
Reasonably simple to include rotations
for objects as well
Note that if we use AABB for both BVHs,
then the AABB-AABB test becomes a
AABB-OBB test
Tomas Akenine-Mőller © 2002
Tradeoffs
The choice of BV
– AABB, OBB, k-DOP, sphere
In general, the tighter BV, the slower test
Less tight BV, gives more triangle-
triangle tests in the end
Cost function:
Tomas Akenine-Mőller © 2002
CD between
many objects
Why needed?
Consider several hundreds of rocks tumbling
down a slope…
This system is often called ”First-Level CD”
We execute this system because we want to
execute the 2nd system less frequently
Assume high frame-to-frame coherency
– Means that object is close to where it was previous
frame
– Reasonable
Tomas Akenine-Mőller © 2002
Sweep-and-prune algorithm
[by Ming Lin]
Assume objects may translate and rotate
Then we can find a minimal cube, which
is guaranteed to contain object for all
rotations
Do collision overlap three times
– One for x,y, and z-axes
Let’sconcentrate on one axis at a time
Each cube on this axis is an interval,
from si to ei, where i is cube number
Tomas Akenine-Mőller © 2002
Sweep-and-prune algorithm
Sort all si and ei into a list
Traverse list from start to end
When an s is encounted, mark
corresponding interval as active in an
active_interval_list
When an e is encountered, delete the
interval in active_interval_list
All intervals in active_interval_
list are overlapping!
Tomas Akenine-Mőller © 2002
Sweep-and-prune algorithm
Now sorting is expensive: O(n*log n)
But, exploit frame-to-frame coherency!
The list is not expected to change much
Therefore, ”resort” with bubble-sort, or
insertion-sort
Expected: O(n)
Tomas Akenine-Mőller © 2002
Sweep-and-prune algorithm
Keep a boolean for each pair of intervals
Invert when sort order changes
If all boolean for all three axes are true, overlap
Tomas Akenine-Mőller © 2002
CD Conclusion
Very important part of games!
Many different algorithms to choose from
Decide what’s best for your case,
and implement…
Tomas Akenine-Mőller © 2002