53
Fast and Simple Physics using Sequential Impulses Erin Catto Crystal Dynamics

Fast and Simple Physics using Sequential Impulses · 2020-01-19 · Fast and Simple Physics using Sequential Impulses Erin Catto Crystal Dynamics

  • Upload
    others

  • View
    2

  • Download
    0

Embed Size (px)

Citation preview

Page 1: Fast and Simple Physics using Sequential Impulses · 2020-01-19 · Fast and Simple Physics using Sequential Impulses Erin Catto Crystal Dynamics

Fast and Simple Physics using Sequential Impulses

Erin CattoCrystal Dynamics

Page 2: Fast and Simple Physics using Sequential Impulses · 2020-01-19 · Fast and Simple Physics using Sequential Impulses Erin Catto Crystal Dynamics

Physics Engine Checklist

Collision and contact

Friction: static and dynamic

Stacking

Joints

Fast, simple, and robust

Page 3: Fast and Simple Physics using Sequential Impulses · 2020-01-19 · Fast and Simple Physics using Sequential Impulses Erin Catto Crystal Dynamics

Box2D Demo

It’s got collision

It’s got friction

It’s got stacking

It’s got joints

Check the code, it’s simple!

Page 4: Fast and Simple Physics using Sequential Impulses · 2020-01-19 · Fast and Simple Physics using Sequential Impulses Erin Catto Crystal Dynamics

Fast and Simple Physics

Penalty method? Nope

Linear complementarity (LCP)? Nope

Joint coordinates (Featherstone)? Nope

Particles (Jakobsen)? Nope

Impulses? Bingo!

Page 5: Fast and Simple Physics using Sequential Impulses · 2020-01-19 · Fast and Simple Physics using Sequential Impulses Erin Catto Crystal Dynamics

Why Impulses?

Most people don’t hate impulses

The math is almost understandable

Intuition often works

Impulses can be robust

m =

Pv

m

P

Page 6: Fast and Simple Physics using Sequential Impulses · 2020-01-19 · Fast and Simple Physics using Sequential Impulses Erin Catto Crystal Dynamics

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.

Page 7: Fast and Simple Physics using Sequential Impulses · 2020-01-19 · Fast and Simple Physics using Sequential Impulses Erin Catto Crystal Dynamics

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.

Page 8: Fast and Simple Physics using Sequential Impulses · 2020-01-19 · Fast and Simple Physics using Sequential Impulses Erin Catto Crystal Dynamics

The 5 Step Program

Accept penetration

Remember the past

Apply impulses early and often

Pursue the true impulse

Update position last

(for taking the jitter out of impulses)

Page 9: Fast and Simple Physics using Sequential Impulses · 2020-01-19 · Fast and Simple Physics using Sequential Impulses Erin Catto Crystal Dynamics

Penetration

Performance

Simplicity

Coherence

Game logic

Fewer cracks

Page 10: Fast and Simple Physics using Sequential Impulses · 2020-01-19 · Fast and Simple Physics using Sequential Impulses Erin Catto Crystal Dynamics

Algorithm Overview

Compute contact points

Apply forces (gravity)

Apply impulses

Update position

Loop

Page 11: Fast and Simple Physics using Sequential Impulses · 2020-01-19 · Fast and Simple Physics using Sequential Impulses Erin Catto Crystal Dynamics

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

Page 12: Fast and Simple Physics using Sequential Impulses · 2020-01-19 · Fast and Simple Physics using Sequential Impulses Erin Catto Crystal Dynamics

Box-Box SAT

First find the separating axis with the minimum penetration.

In 2D the separating axis is a face normal.

n

Page 13: Fast and Simple Physics using Sequential Impulses · 2020-01-19 · Fast and Simple Physics using Sequential Impulses Erin Catto Crystal Dynamics

Box-Box Clipping Setup

Identify reference face

Identify incident face n

incident

reference

Page 14: Fast and Simple Physics using Sequential Impulses · 2020-01-19 · Fast and Simple Physics using Sequential Impulses Erin Catto Crystal Dynamics

Box-Box Clipping

Clip incident face against reference face side planes (but not the reference face).

Consider clip points with positive penetration.

n

clipping planes

Page 15: Fast and Simple Physics using Sequential Impulses · 2020-01-19 · Fast and Simple Physics using Sequential Impulses Erin Catto Crystal Dynamics

Feature Flip-Flop

Which normal is the separating axis?

Apply weightings to prefer one axis over another.

Improved coherence.

1n

2n

Page 16: Fast and Simple Physics using Sequential Impulses · 2020-01-19 · Fast and Simple Physics using Sequential Impulses Erin Catto Crystal Dynamics

Apply Forces

m

I I

=

+ =

v F

ω ω ω T

1

2 1

1

2 1

t m

t I

= +

= +

v v F

ω ω T

Newton’s Law

Ignore gyroscopic term for improved stability

Use Euler’s rule

Page 17: Fast and Simple Physics using Sequential Impulses · 2020-01-19 · Fast and Simple Physics using Sequential Impulses Erin Catto Crystal Dynamics

Impulses

Impulses are applied at each contact point.

Normal impulses to prevent penetration.

Tangent impulses to impose friction.

0n

t n

P

P P

nP n

tPt

Page 18: Fast and Simple Physics using Sequential Impulses · 2020-01-19 · Fast and Simple Physics using Sequential Impulses Erin Catto Crystal Dynamics

Computing the Impulse

1

2

nP

−P

1r

2r

Page 19: Fast and Simple Physics using Sequential Impulses · 2020-01-19 · Fast and Simple Physics using Sequential Impulses Erin Catto Crystal Dynamics

Linear Momentum

1 1 1

1

1 1 1 1

2 2 2

1

2 2 2 2

/

/

m

I

m

I

= −

= −

= +

= +

v v P

ω ω r P

v v P

ω ω r P

nP=P n

We know the direction of the normal impulse. We only need it’s magnitude.

The normal impulse causes an instant change in velocity.

Page 20: Fast and Simple Physics using Sequential Impulses · 2020-01-19 · Fast and Simple Physics using Sequential Impulses Erin Catto Crystal Dynamics

Relative Velocity

2 2 2 1 1 1 = + − − v v ω r v ω r

n

1

2

1r

2r

nv = v n

Along Normal:

Page 21: Fast and Simple Physics using Sequential Impulses · 2020-01-19 · Fast and Simple Physics using Sequential Impulses Erin Catto Crystal Dynamics

The Normal Impulse

Want: 0nv =

max ,0n

n

Pk

− =

v nGet:

( ) ( )1 1

1 1 1 2 2 2

1 2

1 1nk I I

m m

− − = + + + r n r r n r n

2 2 2 1 1 1 = + − − v v ω r v ω r

Fine Print:

0nP

Page 22: Fast and Simple Physics using Sequential Impulses · 2020-01-19 · Fast and Simple Physics using Sequential Impulses Erin Catto Crystal Dynamics

Bias Impulse

Give the normal impulse some extra oomph.

Proportional to the penetration.

Allow some slop.

Be gentle.

Page 23: Fast and Simple Physics using Sequential Impulses · 2020-01-19 · Fast and Simple Physics using Sequential Impulses Erin Catto Crystal Dynamics

Bias Velocity

n

Slop: slop

Bias Factor: 0.1,0.3

Bias velocity:

( )max 0,bias slopvt

= −

slop

Page 24: Fast and Simple Physics using Sequential Impulses · 2020-01-19 · Fast and Simple Physics using Sequential Impulses Erin Catto Crystal Dynamics

Bias Impulse

max ,0biasn

n

vP

k

− +=

v n

max ,0n

n

Pk

− =

v n

Becomes:

With bias velocity, this:

Page 25: Fast and Simple Physics using Sequential Impulses · 2020-01-19 · Fast and Simple Physics using Sequential Impulses Erin Catto Crystal Dynamics

Friction Impulse

Want: 0tv =

clamp( , , )t n n

t

P P Pk

= −v t

Get:

( ) ( )1 1

1 1 1 2 2 2

1 2

1 1tk I I

m m

− − = + + + r t r r t r t

Fine Print:

n t nP P P −

Tangent Velocity: tv = v t

Page 26: Fast and Simple Physics using Sequential Impulses · 2020-01-19 · Fast and Simple Physics using Sequential Impulses Erin Catto Crystal Dynamics

Sequential Impulses

Apply an impulse at each contact point.

Continue applying impulses for several iterations.

Terminate after:

- fixed number of iterations

- impulses become small

Page 27: Fast and Simple Physics using Sequential Impulses · 2020-01-19 · Fast and Simple Physics using Sequential Impulses Erin Catto Crystal Dynamics

Naïve Impulses

velocity

1P2P

Each impulse is computed independently, leading to jitter.

velocity

Page 28: Fast and Simple Physics using Sequential Impulses · 2020-01-19 · Fast and Simple Physics using Sequential Impulses Erin Catto Crystal Dynamics

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.

Page 29: Fast and Simple Physics using Sequential Impulses · 2020-01-19 · Fast and Simple Physics using Sequential Impulses Erin Catto Crystal Dynamics

Accumulated Impulses

velocity

1P 2P 1P

Each impulse adds to the total. Increments can be negative.

2P

Page 30: Fast and Simple Physics using Sequential Impulses · 2020-01-19 · Fast and Simple Physics using Sequential Impulses Erin Catto Crystal Dynamics

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.

Page 31: Fast and Simple Physics using Sequential Impulses · 2020-01-19 · Fast and Simple Physics using Sequential Impulses Erin Catto Crystal Dynamics

Accumulated Impulse

Clamp the accumulated impulse, not the incremental impulses.

nP

Accumulated impulses:

tP

Page 32: Fast and Simple Physics using Sequential Impulses · 2020-01-19 · Fast and Simple Physics using Sequential Impulses Erin Catto Crystal Dynamics

Correct Clamping

( )max ,0

n

n n n

n n

temp P

P P P

P P temp

=

= +

= −

Normal Clamping:

( )clamp , ,

t

t t t n n

t t

temp P

P P P P P

P P temp

=

= + −

= −

Friction Clamping:

Page 33: Fast and Simple Physics using Sequential Impulses · 2020-01-19 · Fast and Simple Physics using Sequential Impulses Erin Catto Crystal Dynamics

Position Update

Use the new velocities to integrate the positions.

The time step is complete.

Page 34: Fast and Simple Physics using Sequential Impulses · 2020-01-19 · Fast and Simple Physics using Sequential Impulses Erin Catto Crystal Dynamics

Extras

Coherence

Feature-based contact points

Joints

Engine layout

Loose ends

3D Issues

Page 35: Fast and Simple Physics using Sequential Impulses · 2020-01-19 · Fast and Simple Physics using Sequential Impulses Erin Catto Crystal Dynamics

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.

Page 36: Fast and Simple Physics using Sequential Impulses · 2020-01-19 · Fast and Simple Physics using Sequential Impulses Erin Catto Crystal Dynamics

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.

Page 37: Fast and Simple Physics using Sequential Impulses · 2020-01-19 · Fast and Simple Physics using Sequential Impulses Erin Catto Crystal Dynamics

Contact Point IDs

1c

box 1 edge 2

box 2 edge 3

2c

box 2 edge 3

box 2 edge 4

1e

4e

3e

2e

n

1

2

1c2c

Page 38: Fast and Simple Physics using Sequential Impulses · 2020-01-19 · Fast and Simple Physics using Sequential Impulses Erin Catto Crystal Dynamics

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.

Page 39: Fast and Simple Physics using Sequential Impulses · 2020-01-19 · Fast and Simple Physics using Sequential Impulses Erin Catto Crystal Dynamics

Revolute Joint

Two bodies share a common point.

They rotate freely about the point.

Page 40: Fast and Simple Physics using Sequential Impulses · 2020-01-19 · Fast and Simple Physics using Sequential Impulses Erin Catto Crystal Dynamics

Revolute Joint

The joint knows the local anchor point for both bodies.

1r

2r

11

2

Page 41: Fast and Simple Physics using Sequential Impulses · 2020-01-19 · Fast and Simple Physics using Sequential Impulses Erin Catto Crystal Dynamics

Relative Velocity

The relative velocity of the anchor points is zero.

2 2 2 1 1 1 0 = + − − =v v ω r v ω r

An impulse is applied to the two bodies.

P

Page 42: Fast and Simple Physics using Sequential Impulses · 2020-01-19 · Fast and Simple Physics using Sequential Impulses Erin Catto Crystal Dynamics

Linear Momentum

Apply linear momentum to the relative velocity to get:

K = −P v

Fine Print:

1 1

1 1 1 2 2 2

1 2

1 1K I I

m m

− − = + − −

1 r r r r

Tilde (~) for the cross-product matrix.

Page 43: Fast and Simple Physics using Sequential Impulses · 2020-01-19 · Fast and Simple Physics using Sequential Impulses Erin Catto Crystal Dynamics

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.

1

cM K −=

Page 44: Fast and Simple Physics using Sequential Impulses · 2020-01-19 · Fast and Simple Physics using Sequential Impulses Erin Catto Crystal Dynamics

Bias Impulse

The error is the separation between the anchor points

2 2 1 1 = + − −p x r x r

Center of mass: x

Bias velocity and impulse:

bias

bias

t

K

= −

= − +

v p

P v v

Page 45: Fast and Simple Physics using Sequential Impulses · 2020-01-19 · Fast and Simple Physics using Sequential Impulses Erin Catto Crystal Dynamics

Engine Layout

The World class contains all bodies, contacts, and joints.

Contacts are maintained by the Arbiter class.

Page 46: Fast and Simple Physics using Sequential Impulses · 2020-01-19 · Fast and Simple Physics using Sequential Impulses Erin Catto Crystal Dynamics

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.

Page 47: Fast and Simple Physics using Sequential Impulses · 2020-01-19 · Fast and Simple Physics using Sequential Impulses Erin Catto Crystal Dynamics

Arbiters

n

1

2

1c2c

Arbiter

Page 48: Fast and Simple Physics using Sequential Impulses · 2020-01-19 · Fast and Simple Physics using Sequential Impulses Erin Catto Crystal Dynamics

Collision Coherence

Use the arbiter to store the separating axis.

Improve performance at the cost of memory.

Use with broad-phase.

Page 49: Fast and Simple Physics using Sequential Impulses · 2020-01-19 · Fast and Simple Physics using Sequential Impulses Erin Catto Crystal Dynamics

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.

Page 50: Fast and Simple Physics using Sequential Impulses · 2020-01-19 · Fast and Simple Physics using Sequential Impulses Erin Catto Crystal Dynamics

Loose Ends

Ground is represented with bodies whose inverse mass is zero.

Contact mass can be computed as a pre-step.

Bias impulses shouldn’t affect the velocity state (TODO).

Page 51: Fast and Simple Physics using Sequential Impulses · 2020-01-19 · Fast and Simple Physics using Sequential Impulses Erin Catto Crystal Dynamics

3D Issues

Friction requires two axes.

Align the axes with velocity if it is non-zero.

Identify a contact patch (manifold) and apply friction at the center.

This requires a twist friction.

Big CPU savings.

Page 52: Fast and Simple Physics using Sequential Impulses · 2020-01-19 · Fast and Simple Physics using Sequential Impulses Erin Catto Crystal Dynamics

Questions?

http://www.gphysics.com

erincatto at that domain

Download the code there.

Buy Tomb Raider Legend!

Page 53: Fast and Simple Physics using Sequential Impulses · 2020-01-19 · Fast and Simple Physics using Sequential Impulses Erin Catto Crystal Dynamics

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.