Fast and Simple Physics using Sequential Impulses
Physics Engine Checklist
Box2D Demo
Fast and Simple Physics
Why Impulses?
Making Impulses not Suck
Impulses without the Bounce
The 5 Step Program
Penetration
Algorithm Overview
Contact Points
Box-Box SAT
Box-Box Clipping Setup
Box-Box Clipping
Feature Flip-Flop
Apply Forces
Impulses
Computing the Impulse
Linear Momentum
Relative Velocity
The Normal Impulse
Bias Impulse
Bias Velocity
Bias Impulse
Friction Impulse
Sequential Impulses
Naïve Impulses
Where Did We Go Wrong?
Accumulated Impulses
The True Impulse
Accumulated Impulse
Correct Clamping
Position Update
Extras
Coherence
Feature-Based Contact Points
Contact Point IDs
Joints
Revolute Joint
Revolute Joint
Relative Velocity
Linear Momentum
K Matrix
Bias Impulse
Engine Layout
Arbiter
Arbiters
Collision Coherence
More on Arbiters
Loose Ends
3D Issues
Questions?
References
579.50K
Category: physicsphysics

Fast and Simple Physics using Sequential Impulses

1. Fast and Simple Physics using Sequential Impulses

Erin Catto
Crystal Dynamics

2. Physics Engine Checklist

Collision and contact
Friction: static and dynamic
Stacking
Joints
Fast, simple, and robust

3. Box2D Demo

It’s got collision
It’s got friction
It’s got stacking
It’s got joints
Check the code, it’s simple!

4. Fast and Simple Physics

Penalty method?
Linear complementarity (LCP)?
Nope
Particles (Jakobsen)?
Nope
Joint coordinates (Featherstone)?
Nope
Nope
Impulses?
Bingo!

5. Why Impulses?

Most people don’t hate impulses
The math is almost understandable
Intuition often works
Impulses can be robust
m
P
P
v
m

6. Making Impulses not Suck

Impulses are good at making things
bounce.
Many attempts to use impulses leads to
bouncy simulations (aka jitter).
Forget static friction.
Forget stacking.

7. Impulses without the Bounce

Forget bounces for a moment.
Let’s concentrate on keeping things still.
It’s always easy to add back in the
bounce.

8. The 5 Step Program

(for taking the jitter out of impulses)
Accept penetration
Remember the past
Apply impulses early and often
Pursue the true impulse
Update position last

9. Penetration

Performance
Simplicity
Coherence
Game logic
Fewer cracks

10. Algorithm Overview

Compute contact points
Apply forces (gravity)
Apply impulses
Update position
Loop

11. Contact Points

Position, normal, and penetration
Box-box using the SAT
Find the axis of minimum penetration
Find the incident face on the other box
Clip

12. Box-Box SAT

First find the separating
axis with the minimum
penetration.
In 2D the separating axis
is a face normal.
n

13. Box-Box Clipping Setup

Identify reference face
Identify incident face
incident
reference
n

14. Box-Box Clipping

clipping planes
Clip incident face
against reference
face side planes (but
not the reference
face).
Consider clip points
with positive
penetration.
n

15. Feature Flip-Flop

Which normal is the
separating axis?
Apply weightings to
prefer one axis over
another.
Improved coherence.
n1
n2

16. Apply Forces

Newton’s Law
mv F
Iω ω Iω T
Ignore gyroscopic term
for improved stability
v 2 v1 t m 1F
Use Euler’s rule
ω2 ω1 t I 1T

17. Impulses

Impulses are applied at each contact point.
Normal impulses to prevent penetration.
Tangent impulses to impose friction.
Pn 0
Pt
t
Pn n
Pt Pn

18. Computing the Impulse

r2
2
n
1
P
P
r1

19. Linear Momentum

v1 v1 P / m1
The normal impulse
causes an instant
change in velocity.
We know the direction of
the normal impulse. We
only need it’s magnitude.
ω1 ω1 I1 1r1 P
v 2 v 2 P / m2
ω 2 ω 2 I 2 1r2 P
P Pnn

20. Relative Velocity

v v 2 ω2 r2 v1 ω1 r1
2
r2
n
Along Normal:
r1
1
vn v n

21. The Normal Impulse

Want:
vn 0
Get:
v n
Pn max
,0
kn
Pn 0
Fine Print:
v v 2 ω2 r2 v1 ω1 r1
kn
1
1
I1 1 r1 n r1 I 2 1 r2 n r2 n
m1 m2

22. Bias Impulse

Give the normal impulse some extra
oomph.
Proportional to the penetration.
Allow some slop.
Be gentle.

23. Bias Velocity

Slop:
slop
Bias Factor: 0.1,0.3
Bias velocity:
vbias
t
max 0, slop
n
slop

24. Bias Impulse

With bias velocity, this:
v n
Pn max
,0
kn
Becomes:
v n vbias
Pn max
,0
kn

25. Friction Impulse

Tangent Velocity:
vt v t
Pn Pt Pn
Want:
vt 0
Get:
v t
Pt clamp(
, Pn , Pn )
kt
Fine Print:
kt
1
1
I1 1 r1 t r1 I 2 1 r2 t r2 t
m1 m2

26. Sequential Impulses

Apply an impulse at each contact point.
Continue applying impulses for several
iterations.
Terminate after:
- fixed number of iterations
- impulses become small

27. Naïve Impulses

velocity
Each impulse is computed
independently, leading to
jitter.
velocity
P1
P2

28. Where Did We Go Wrong?

Each contact point forgets its impulse
history.
Each contact point requires that every
impulse be positive.
There is no way to recover from a bad
impulse.

29. Accumulated Impulses

velocity
P1
P2
Each impulse adds to
the total. Increments
can be negative.
P1
P2

30. The True Impulse

Each impulse adds to an accumulated
impulse for each contact point.
The accumulated impulse approaches
the true impulse (hopefully).
True impulse: an exact global solution.

31. Accumulated Impulse

Clamp the accumulated impulse, not the
incremental impulses.
Accumulated impulses:
P n
P t

32. Correct Clamping

Normal Clamping:
temp P n
P n max P n Pn , 0
Pn P n temp
Friction Clamping:
temp P t
P t clamp P t Pt , P n , P n
Pt P t temp

33. Position Update

Use the new velocities to integrate the
positions.
The time step is complete.

34. Extras

Coherence
Feature-based contact points
Joints
Engine layout
Loose ends
3D Issues

35. Coherence

Apply old accumulated impulses at the
beginning of the step.
Less iterations and greater stability.
We need a way to match old and new
contacts.

36. Feature-Based Contact Points

Each contact point is the result of
clipping.
It is the junction of two different edges.
An edge may come from either box.
Store the two edge numbers with each
contact point – this is the Contact ID.

37. Contact Point IDs

c1
box 1 edge 2
e1
2
n
box 2 edge 3
e4
e2
e3
c1
c2
c2
1
box 2 edge 3
box 2 edge 4

38. Joints

Specify (constrain) part of the motion.
Compute the impulse necessary to
achieve the constraint.
Use an accumulator to pursue the true
impulse.
Bias impulse to prevent separation.

39. Revolute Joint

Two bodies share
a common point.
They rotate freely
about the point.

40. Revolute Joint

2
The joint knows the
local anchor point
for both bodies.
r2
r1
1

41. Relative Velocity

The relative velocity of the anchor
points is zero.
v v 2 ω2 r2 v1 ω1 r1 0
An impulse is applied to the two
bodies.
P

42. Linear Momentum

Apply linear momentum to the relative
velocity to get:
KP v
Fine Print:
1
1
1
1
K
1
r
I
r
r
I
1 1 1
2 2 r2
m1 m2
Tilde (~) for the cross-product matrix.

43. K Matrix

2-by-2 matrix in 2D, 3-by-3 in 3D.
Symmetric positive definite.
Think of K as the inverse mass matrix of
the constraint.
M c K 1

44. Bias Impulse

The error is the separation between the
anchor points
p x2 r2 x1 r1
Center of mass: x
Bias velocity and impulse:
vbias
p
t
KP v vbias

45. Engine Layout

The World class contains all bodies,
contacts, and joints.
Contacts are maintained by the Arbiter
class.

46. Arbiter

An arbiter exists for every touching pair
of boxes.
Provides coherence.
Matches new and old contact points
using the Contact ID.
Persistence of accumulated impulses.

47. Arbiters

2
n
Arbiter
c1
c2
1

48. Collision Coherence

Use the arbiter to store the separating
axis.
Improve performance at the cost of
memory.
Use with broad-phase.

49. More on Arbiters

Arbiters are stored in a set according to
the ordered body pointers.
Use time-stamping to remove stale
arbiters.
Joints are permanent arbiters.
Arbiters can be used for game logic.

50. Loose Ends

Ground is represented with bodies whose
inverse mass is zero.
Contact mass can be computed as a prestep.
Bias impulses shouldn’t affect the
velocity state (TODO).

51. 3D Issues

Friction requires two axes.
Align the axes with velocity if it is nonzero.
Identify a contact patch (manifold) and
apply friction at the center.
This requires a twist friction.
Big CPU savings.

52. Questions?

http://www.gphysics.com
erincatto at that domain
Download the code there.
Buy Tomb Raider Legend!

53. References

Physics-Based Animation by Kenny Erleben et al.
Real-Time Collision Detection by Christer Ericson.
Collision Detection in Interactive 3D Environments by
Gino van den Bergen.
Fast Contact Reduction for Dynamics Simulation by Adam
Moravanszky and Pierre Terdiman in Game Programming
Gems 4.
English     Русский Rules