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

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

Dec 25, 2015

Download

Documents

Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Fast and Simple Physics using Sequential Impulses Erin Catto Crystal Dynamics.

Fast and Simple Physics Fast and Simple Physics using Sequential using Sequential ImpulsesImpulses

Erin CattoErin CattoCrystal Crystal DynamicsDynamics

Page 2: Fast and Simple Physics using Sequential Impulses Erin Catto Crystal Dynamics.

Physics Engine ChecklistPhysics Engine Checklist

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

Page 3: Fast and Simple Physics using Sequential Impulses Erin Catto Crystal Dynamics.

Box2D DemoBox2D 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 Erin Catto Crystal Dynamics.

Fast and Simple PhysicsFast 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 Erin Catto Crystal Dynamics.

Why Impulses?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 Erin Catto Crystal Dynamics.

Making Impulses not SuckMaking 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 Erin Catto Crystal Dynamics.

Impulses without the Impulses without the BounceBounce 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 Erin Catto Crystal Dynamics.

The 5 Step ProgramThe 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 Erin Catto Crystal Dynamics.

PenetrationPenetration

Performance Simplicity Coherence Game logic Fewer cracks

Page 10: Fast and Simple Physics using Sequential Impulses Erin Catto Crystal Dynamics.

Algorithm OverviewAlgorithm Overview

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

Page 11: Fast and Simple Physics using Sequential Impulses Erin Catto Crystal Dynamics.

Contact PointsContact 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 Erin Catto Crystal Dynamics.

Box-Box SATBox-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 Erin Catto Crystal Dynamics.

Box-Box Clipping SetupBox-Box Clipping Setup

Identify reference face

Identify incident face

n

incident

reference

Page 14: Fast and Simple Physics using Sequential Impulses Erin Catto Crystal Dynamics.

Box-Box ClippingBox-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 Erin Catto Crystal Dynamics.

Feature Flip-FlopFeature 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 Erin Catto Crystal Dynamics.

Apply ForcesApply Forces

m

I I

v F

ω ω ω T

12 1

12 1

t m

t I

v v F

ω ω T

Newton’s LawIgnore gyroscopic term for improved stability

Use Euler’s rule

Page 17: Fast and Simple Physics using Sequential Impulses Erin Catto Crystal Dynamics.

ImpulsesImpulses

Impulses are applied at each contact point. Normal impulses to prevent penetration. Tangent impulses to impose friction.

0n

t n

P

P P

nPn

tPt

Page 18: Fast and Simple Physics using Sequential Impulses Erin Catto Crystal Dynamics.

Computing the ImpulseComputing the Impulse

1

2n

P P

1r

2r

Page 19: Fast and Simple Physics using Sequential Impulses Erin Catto Crystal Dynamics.

Linear MomentumLinear Momentum

1 1 1

11 1 1 1

2 2 2

12 2 2 2

/

/

m

I

m

I

v v P

ω ω r P

v v P

ω ω r P

nPP nWe 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 Erin Catto Crystal Dynamics.

Relative VelocityRelative 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 Erin Catto Crystal Dynamics.

The Normal ImpulseThe Normal Impulse

Want:

0nv

max ,0nn

Pk

v nGet:

1 11 1 1 2 2 2

1 2

1 1nk I Im 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 Erin Catto Crystal Dynamics.

Bias ImpulseBias 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 Erin Catto Crystal Dynamics.

Bias VelocityBias 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 Erin Catto Crystal Dynamics.

Bias ImpulseBias Impulse

max ,0biasn

n

vP

k

v n

max ,0nn

Pk

v n

Becomes:

With bias velocity, this:

Page 25: Fast and Simple Physics using Sequential Impulses Erin Catto Crystal Dynamics.

Friction ImpulseFriction Impulse

Want:

0tv

clamp( , , )t n nt

P P Pk

v tGet:

1 11 1 1 2 2 2

1 2

1 1tk I Im 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 Erin Catto Crystal Dynamics.

Sequential ImpulsesSequential 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 Erin Catto Crystal Dynamics.

Naïve ImpulsesNaïve Impulses

velocity

1P 2P

Each impulse is computed independently, leading to jitter.

velocity

Page 28: Fast and Simple Physics using Sequential Impulses Erin Catto Crystal Dynamics.

Where Did We Go Wrong?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 Erin Catto Crystal Dynamics.

Accumulated ImpulsesAccumulated 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 Erin Catto Crystal Dynamics.

The True ImpulseThe 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 Erin Catto Crystal Dynamics.

Accumulated ImpulseAccumulated Impulse

Clamp the accumulated impulse, not the incremental impulses.

nP

Accumulated impulses:

tP

Page 32: Fast and Simple Physics using Sequential Impulses Erin Catto Crystal Dynamics.

Correct ClampingCorrect Clamping

max ,0n

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 Erin Catto Crystal Dynamics.

Position UpdatePosition Update

Use the new velocities to integrate the positions.

The time step is complete.

Page 34: Fast and Simple Physics using Sequential Impulses Erin Catto Crystal Dynamics.

ExtrasExtras

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

Page 35: Fast and Simple Physics using Sequential Impulses Erin Catto Crystal Dynamics.

CoherenceCoherence

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 Erin Catto Crystal Dynamics.

Feature-Based Contact Feature-Based Contact PointsPoints 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 Erin Catto Crystal Dynamics.

Contact Point IDsContact 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 Erin Catto Crystal Dynamics.

JointsJoints

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 Erin Catto Crystal Dynamics.

Revolute JointRevolute Joint

Two bodies share a common point.

They rotate freely about the point.

Page 40: Fast and Simple Physics using Sequential Impulses Erin Catto Crystal Dynamics.

Revolute JointRevolute Joint

The joint knows the local anchor point for both bodies.

1r

2r

11

2

Page 41: Fast and Simple Physics using Sequential Impulses Erin Catto Crystal Dynamics.

Relative VelocityRelative 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 Erin Catto Crystal Dynamics.

Linear MomentumLinear Momentum

Apply linear momentum to the relative velocity to get:

K P v

Fine Print:

1 11 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 Erin Catto Crystal Dynamics.

K MatrixK 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.

1cM K

Page 44: Fast and Simple Physics using Sequential Impulses Erin Catto Crystal Dynamics.

Bias ImpulseBias 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

tK

v p

P v v

Page 45: Fast and Simple Physics using Sequential Impulses Erin Catto Crystal Dynamics.

Engine LayoutEngine 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 Erin Catto Crystal Dynamics.

ArbiterArbiter

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 Erin Catto Crystal Dynamics.

ArbitersArbiters

n

1

2

1c2c

Arbiter

Page 48: Fast and Simple Physics using Sequential Impulses Erin Catto Crystal Dynamics.

Collision CoherenceCollision 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 Erin Catto Crystal Dynamics.

More on ArbitersMore 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 Erin Catto Crystal Dynamics.

Loose EndsLoose 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 Erin Catto Crystal Dynamics.

3D Issues3D 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 Erin Catto Crystal Dynamics.

Questions?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 Erin Catto Crystal Dynamics.

ReferencesReferences

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.