Title: Physics%20for%20Games%20Programmers%20Tutorial%20Motion%20and%20Collision%20
1Physics for Games Programmers TutorialMotion
and Collision Its All Relative
- Squirrel Eiserloh
- squirrel_at_eiserloh.net
- Lead Programmer
- Ritual Entertainment
- www.ritual.com
- www.algds.org
2Takeaway
- A comfortable, intuitive understanding of
- The Problems of Discrete Simulation
- Continuous Collision Detection
- Applying Relativity to Game Physics
- Configuration Space
- Collisions in Four Dimensions
- The Problems of Rotation
- Why this is all really important even if youre
doing simple cheesy 2d games at home in your
underwear in your spare time
3The Problem
- Discrete physics simulation falls embarrassingly
short of reality. - Real physics is prohibitively expensive...
- ...so we cheat.
- We need to cheat enough to be able to run in real
time. - We need to not cheat so much that things break in
a jarring and unrecoverable way. - Much of the challenge is knowing how and when to
cheat.
4Overview
- Simulation
- Tunneling
- Movement Bounds
- Swept Shapes
- Einstein Says...
- Minkowski Says...
- Rotation
5Also, I promise...
No math
6Simulation
(Sucks)
7Problems with Simulation
8Problems with Simulation
- Flipbook syndrome
- Things can happen in-between snapshots
9Problems with Simulation
- Flipbook syndrome
- Things mostly happen in-between snapshots
10Problems with Simulation
- Flipbook syndrome
- Things mostly happen in-between snapshots
- Curved trajectories treated as piecewise linear
11Problems with Simulation
- Flipbook syndrome
- Things mostly happen in-between snapshots
- Curved trajectories treated as piecewise linear
- Terms often assumed to be constant throughout the
frame
12Problems with Simulation
- Flipbook syndrome
- Things mostly happen in-between snapshots
- Curved trajectories treated as piecewise linear
- Terms often assumed to be constant throughout the
frame - Error accumulates
13Problems with Simulation (contd)
- Rotations are often assumed to happen
instantaneously at frame boundaries
14Problems with Simulation (contd)
- Rotations are often assumed to happen
instantaneously at frame boundaries - Energy is not always conserved
- Energy loss can be undesirable
- Energy gain is evil
- Simulations explode!
15Problems with Simulation (contd)
- Rotations are often assumed to happen
instantaneously at frame boundaries - Energy is not always conserved
- Energy loss can be undesirable
- Energy gain is evil
- Simulations explode!
- Tunneling
- (Also evil!)
16Overlapping Objects
- Question 1 Do A and B overlap?
- Plenty of reference material to help solve this,
but... - ...this is often the wrong question to ask (begs
tunneling).
17Tunneling
(Sucks)
18Tunneling
- Small objects tunnel more easily
19Tunneling (contd)
- Possible solutions
- Minimum size requirement?
- Inadequate fast objects still tunnel
20Tunneling (contd)
- Fast-moving objects tunnel more easily
21Tunneling (contd)
- Possible solutions
- Minimum size requirement?
- Inadequate fast objects still tunnel
- Maximum speed limit?
- Inadequate since speed limit is a function of
object size, this would mean small fast objects
(bullets) would not be allowed - Smaller time step?
- Helpful, but inadequate this is essentially the
same as a speed limit
22Tunneling (contd)
- Besides, even with min. size requirements and
speed limits and a small timestep, you still have
degenerate cases that cause tunneling
23Tunneling (contd)
- Tunneling is very, very bad this is not a
mundane detail - Things falling through world
- Bullets passing through people or walls
- Players getting places they shouldnt
- Players missing a trigger boundary
- Okay, so tunneling really sucks. What can we do
about it?
24Movement Bounds
25Movement Bounds
26Movement Bounds
- Disc / Sphere
- AABB (Axis-Aligned Bounding Box)
27Movement Bounds
- Disc / Sphere
- AABB (Axis-Aligned Bounding Box)
- OBB (Oriented Bounding Box)
28Movement Bounds
- Question 2 Could A and B have collided during
the frame? - Better than Question 1 (solves tunneling!),
but...
29Movement Bounds
- Question 2 Could A and B have collided during
the frame? - Better than Question 1 (solves tunneling!),
but... - ...even if the answer is yes, we still dont
know for sure (false positives).
30Movement Bounds
- Conclusion
- Good They prevent tunneling! (i.e. no false
negatives) - Bad They dont actually tell us whether A and B
collided (still have false positives). - Good They can be used as a cheap, effective
early rejection test.
31Swept Shapes
32Swept Shapes
- Swept disc / sphere (n-sphere) capsule
33Swept Shapes
- Swept disc / sphere (n-sphere) capsule
- Swept AABB convex polytope (polygon in 2d,
polyhedron in 3d)
34Swept Shapes
- Swept disc / sphere (n-sphere) capsule
- Swept AABB convex polytope (polygon in 2d,
polyhedron in 3d) - Swept triangle / tetrahedron (simplex) convex
polytope
35Swept Shapes
- Swept disc / sphere (n-sphere) capsule
- Swept AABB convex polytope (polygon in 2d,
polyhedron in 3d) - Swept triangle / tetrahedron (simplex) convex
polytope - Swept polytope convex polytope
36Swept Shapes (contd)
- Like movement bounds, only with a perfect fit!
37Swept Shapes (contd)
- Like movement bounds, only with a perfect fit!
- Still no false negatives (tunneling).
38Swept Shapes (contd)
- Like movement bounds, only with a perfect fit!
- Still no false negatives (tunneling).
- Finally, no false positives, either!
39Swept Shapes (contd)
- Like movement bounds, only with a perfect fit!
- Still no false negatives (tunneling).
- Finally, no false positives, either!
- No, wait, nevermind. Still have em. Rats.
40Swept Shapes (contd)
- Conclusion
- Suck?
- Can be used as early rejection test, but...
- ...movement bounds are better for that.
- If youre not too picky...
- ...they DO solve a large number of nasty problems
(especially tunneling) - ...and can serve as a poor mans continuous
collision detection for a basic engine.
41(No Transcript)
42Einstein Says...
- Coordinate systems are relative
43Relative Coordinate Systems
44Relative Coordinate Systems
45Relative Coordinate Systems
- World coordinates
- As local coordinates
46Relative Coordinate Systems
- World coordinates
- As local coordinates
- Bs local coordinates
47Relative Coordinate Systems
Math is often nicer at the origin.
x2 y2 r2
(x-h)2 (y-k)2 r2
48Einstein Says...
- Coordinate systems are relative
- Motion is relative
49Relative Motion
50Relative Motion
- "Frames of Reference"
- World frame
51Relative Motion
- "Frames of Reference"
- World frame
- A's frame
52Relative Motion
- "Frames of Reference"
- World frame
- A's frame
- B's frame
53Relative Motion
- "Frames of Reference"
- World frame
- A's frame
- B's frame
- Inertial frame
54Relative Motion
- A Rule of Relativistic Collision Detection
- It is always possible to reduce a collision check
between two moving objects to a collision check
between a moving object and a stationary object
(by reframing)
55(Does Not Suck)
56Relative Collision Bodies
57Relative Collision Bodies
- Collision check equivalencies (disc)
58Relative Collision Bodies
- Collision check equivalencies (disc)
- ...AABB
59Relative Collision Bodies
- Collision check equivalencies (disc)
- ...AABB
- Can even reduce one body to a singularity
60Relative Collision Bodies
- Collision check equivalencies (disc)
- ...AABB
- Can even reduce one body to a singularity
- Tracing or Rubbing collision bodies together
61Relative Collision Bodies
- Collision check equivalencies (disc)
- ...AABB
- Can even reduce one body to a singularity
- Tracing or Rubbing collision bodies together
- Spirograph-out the reduced bodys origin
62Relative Collision Bodies (contd)
63Relative Collision Bodies (contd)
64Relative Collision Bodies (contd)
- Disc disc
- AABB AABB
- Triangle AABB
65Relative Collision Bodies (contd)
- Disc disc
- AABB AABB
- Triangle AABB
- AABB triangle
66Relative Collision Bodies (contd)
- Disc disc
- AABB AABB
- Triangle AABB
- AABB triangle
- Polytope polytope
67Relative Collision Bodies (contd)
- Disc disc
- AABB AABB
- Triangle AABB
- AABB triangle
- Polytope polytope
- Polytope disc
68Relative Collision Bodies (contd)
- Things start to get messy when combining bodies
explicitly / manually. - (Especially in 3d.)
- General solution?
69Minkowski Arithmetic
70Minkowski Sums
- The Minkowski Sum (AB) of A and B is the result
of adding every point in A to every point in B.
71Minkowski Sums
- The Minkowski Sum (AB) of A and B is the result
of adding every point in A to every point in B. - Minkowski Sums are commutative
- AB BA
- Minkowski Sum of convex objects is convex
72Minkowski Differences
- The Minkowski Difference (A-B) of A and B is the
result of subtracting every point in B from every
point in A (or A -B)
73Minkowski Differences
- The Minkowski Difference (A-B) of A and B is the
result of subtracting every point in B from every
point in A - Resulting shape is different from AB.
74Minkowski Differences (contd)
- Minkowski Differences are not commutative
- A-B ! B-A
- Minkowski Difference of convex objects is convex
(since A-B A -B)
75Minkowski Differences (contd)
- Minkowski Differences are not commutative
- A-B ! B-A
- Minkowski Difference of convex objects is convex
(since A-B A -B) - Minkowski Difference produces the same shape as
Spirograph
76Minkowski Differences (contd)
- If the singularity is outside the combined body,
A and B do not overlap.
77Minkowski Differences (contd)
- If the singularity is outside the combined body,
A and B do not overlap. - If the singularity is inside the combined body
(A-B), then A and B overlap.
78Minkowski Differences (contd)
-
- Aorigin vs. Borigin
- -Borigin -Borigin
- ___ ___
- (A-B)origin vs. 0
79Minkowski Differences (contd)
- In world space, A-B is near the origin
80Minkowski Differences (contd)
- Since the singularity point is always at the
origin (B-B), we can say... - If (A-B) does not contain the origin, A and B do
not overlap.
81Minkowski Differences (contd)
- If (A-B) contains the origin, A and B overlap.
- In other words, we reduce A vs. B to
- combined body (A-B) vs.
- point (B-B, or origin)
82Minkowski Differences (contd)
- If A and B are in the same coordinate system, the
comparison between A-B and the origin is said to
happen in configuration space - ...in which case A-B is said to be a
configuration space obstacle (CSO)
83Minkowski Differences (contd)
Translations in A or B simply translate the CSO
84Minkowski Differences (contd)
Rotations in A or B mutate the CSO
85Minkowski Sum vs. Difference
- Lots of confusion over Minkowski Sum vs.
Difference. - Sum is used to fatten an object by adding
another object (in local coordinates) to it - Difference is used to put the objects in
configuration space, i.e. A-B vs. origin. - Difference sometimes called Sum since A-B can be
expressed as A(-B)!
86Minkowski Sum vs. Difference (contd)
- Difference is the same as Spirograph or
rubbing - Difference is not commutative!
- A-B ! B-A
- Difference and sum produce different-shaped
results - Difference produces CSO (configuration space
obstacle)
87(Does Not Suck)
88Relative Everything
89Relative Everything
- Lets combine
- Relative Coordinate Systems
- Relative Motion
- Relative Collision Bodies
90Relative Everything (contd)
91Relative Everything (contd)
- A vs. B in world frame
- A vs. B, inertial frame
92Relative Everything (contd)
- A vs. B in world frame
- A vs. B, inertial frame
- A is moving, B is still
93Relative Everything (contd)
- A vs. B in world frame
- A vs. B, inertial frame
- A is moving, B is still
- A is CSO, B is point
94Relative Everything (contd)
- A vs. B in world frame
- A vs. B, inertial frame
- A is moving, B is still
- A is CSO, B is point
- A is moving CSO, B is still point
95Relative Everything (contd)
- A vs. B in world frame
- A vs. B, inertial frame
- A is moving, B is still
- A is CSO, B is point
- A is moving CSO, B is still point
- A is still CSO, B is moving point
96Relative Everything (contd)
- Question 3 Did A and B collide during the
frame? - Yes! We can now get an exact answer.
- No false negatives, no false positives!
- However, we still dont know WHEN they collided...
97Relative Everything (contd)
- Why does the exact collision time matter?
- Outcomes can be different
- Order of events (e.g. multiple collisions) is
relevant - Collision response is easier when you can
reconstruct the exact moment of impact
98Relative Everything (contd)
- The Minkowski Difference (A-B) / CSO can also be
thought of as the set of all translations from
the origin that would cause a collision. - A.K.A. the set of inadmissible translations.
99Determining Collision Time
100Determining Collision Time
- Method 1 Frame Subdivision
101Subdividing Movement Frame
- If a swept-shape (or movement bounds) test says
yes
102Subdividing Movement Frame
- If a swept-shape (or movement bounds) test says
yes - Cut the frame in half perform two separate tests
(first half first, second half second). - First positive test is when the collision
occurred.
103Subdividing Movement Frame (contd)
- Can recurse (1/2, 1/4, 1/8...) to the desired
level of granularity
104Subdividing Movement Frame (contd)
- Can recurse (1/2, 1/4, 1/8...) to the desired
level of granularity - If both tests negative, no collision (was a false
positive). - Still inexact (minimizing, not eliminating, false
positives) - Gets expensive
105Determining Collision Time
- Method 1 Frame Subdivision
- Method 2 4D Continuous Collision Detection
- (N1 dimensions 3D for 2D physics, etc.)
106Spacetime
107Spacetime
- Spacetime is a Physics construct which combines
N-dimensional space with an extra dimension for
time, yielding a unified model with N1
dimensions. - Space (1D) time (1D) spacetime (2D)
- Space (2D) time (1D) spacetime (3D)
- Space (3D) time (1D) spacetime (4D)
108Spacetime Diagrams
- 1D space time 2D
- Just an X vs. T graph!
109Spacetime Diagrams
- 1D space time 2D
- Just an X vs. T graph!
- 2D space time 3D
- No problem.
110Spacetime Diagrams
- 1D space time 2D
- Just an X vs. T graph!
- 2D space time 3D
- No problem.
- Another example (2d space time 3D)
111Spacetime Diagrams
- 1D space time 2D
- Just an X vs. T graph!
- 2D space time 3D
- No problem.
- Another example (2d space time 3D)
- 3D space time 4D
- Brainbuster!
112Spacetime Diagrams (contd)
- Note that an N-dimensional system in motion is
the same as a still snapshot in N1 dimensions - 1D animation 2D spacetime still image
- 2D animation 3D spacetime still image
- 3D animation 4D spacetime still image
113Spacetime Diagrams (contd)
2D spacetime still image
1D animation
114Spacetime Diagrams (contd)
3D spacetime still image
2D animation
115Spacetime Diagrams (contd)
- How do you envision a 4D object?
- Use 2D animation -gt 3D spacetime diagram as a
mental analogy. - Fun reading Flatland by Edwin Abbott
- Think about a 4D object that youre already
familiar with. - (The universe in motion!)
116Spacetime Diagrams (contd)
- Invented by Hermann Minkowski
- Also called Minkowski Diagrams
117(Rules)
118Time-Swept Shapes
119Time-Swept Shapes
- Sweep out shapes, but do it over time in a
spacetime diagram - Define time over frame as being in the interval
0,1 - As before, we can play around with lots of
relativistic variations
120Time-Swept Shapes (contd)
121Time-Swept Shapes (contd)
- A vs. B in world frame
- A is moving, B is still
122Time-Swept Shapes (contd)
- A vs. B in world frame
- A is moving, B is still
- A is CSO, B is point
123Time-Swept Shapes (contd)
- A vs. B in world frame
- A is moving, B is still
- A is CSO, B is point
- A is still CSO, B is moving (swept) point
124Time-Swept Shapes (contd)
- To solve for collision time, we intersect the
point-swept ray against the CSO - The t coordinate at the intersection point is
the time 0,1 of collision - Collision check is done in N1 dimensions
- Which means, in a 3D game, we collide a 4D ray
vs. a 4D body! (What?)
125Time-Swept Shapes (contd)
- Wait, it gets easier...
- When we view this diagram (CSO vs moving point)
down the time axis, i.e. from overhead - Since CSO is not moving, it looks 2D from
overhead...
126Time-Swept Shapes (contd)
- We can reduce this back down to N dimensions
(from N1) since we are looking down the time
axis! - So it becomes an N-dimesional ray vs.
N-dimensional body again. - Which means, in a 3D game, we collide a 3D ray
vs. a 3D body.
127(No Transcript)
128Time-Swept Shapes (contd)
- Question 4 When, during the frame, did A and B
collide? - Finally, the right question - and we have a
complete answer! - With fixed cost, and with exact results (no false
anything).
129Time-Swept Shapes (contd)
- BTW, this is essentially the same as solving for
the fraction of the singularity-translation ray
from our original Minkowski Difference
inadmissible translations picture!
130Quality vs. Quantity
- or
- You Get What You Pay For
131Quality vs. Quantity
- The more you ask, the more you pay.
- Question 1 Do A and B overlap?
- Question 2 Could A and B have collided during
the frame? - Question 3 Did A and B collide during the
frame? - Question 4 When, during the frame, did A and B
collide?
132Rotations
(Suck)
133Rotations
- Continuous rotational collision detection sucks
- Rotational tunneling alone is problematic
134Rotations
- Continuous rotational collision detection sucks
- Rotational tunneling alone is problematic
- Methods weve discussed here often dont work on
rotations, or their rotational analogue is quite
complex
135Rotations (contd)
- However
- Rotational tunneling is usually not as jarring as
translational tunneling - Rotational speed limits are actually feasible
- Can do linear approximations of swept rotations
- Can use bounding shapes to contain pre- and
post-rotated positions - This is something that many engines never solve
robustly
136Summary
137Summary
- The nature of simulation causes us real
problems... problems which cant be ignored. - Have to worry about false negatives (tunneling!)
as well as false positives. - Knowing when a collision event took place can be
very important (especially when resolving it). - Sometimes a problem (and math) looks easier when
we look at it from a different viewpoint. - Can combine bodies in cheaty ways to simplify
things even further.
138Summary (contd)
- Einstein and Minkowski are cool.
- Rotations suck.
- Doing real-time collision detection in 4D
spacetime doesnt have to be hard. - Or expensive.
- Or confusing.
139Questions?
- Feel free to reach me by email at
- squirrel_at_eiserloh.net
- or
- squirrel_at_ritual.com