Modeling and Solving Constraints Erin Catto Blizzard Entertainment - - PowerPoint PPT Presentation

modeling and solving constraints
SMART_READER_LITE
LIVE PREVIEW

Modeling and Solving Constraints Erin Catto Blizzard Entertainment - - PowerPoint PPT Presentation

Modeling and Solving Constraints Erin Catto Blizzard Entertainment Basic Idea Constraints are used to simulate joints, contact, and collision. We need to solve the constraints to stack boxes and to keep ragdoll limbs attached.


slide-1
SLIDE 1

Modeling and Solving Constraints

Erin Catto Blizzard Entertainment

slide-2
SLIDE 2

Basic Idea

Constraints are used to simulate joints, contact,

and collision.

We need to solve the constraints to stack boxes

and to keep ragdoll limbs attached.

Constraint solvers do this by calculating impulse

  • r forces, and applying them to the constrained

bodies.

slide-3
SLIDE 3

Overview

Constraint Formulas Jacobians, Lagrange Multipliers Modeling Constraints Joints, Motors, Contact Building a Constraint Solver Sequential Impulses

slide-4
SLIDE 4

Constraint Types

Contact and Friction

slide-5
SLIDE 5

Constraint Types

Ragdolls

slide-6
SLIDE 6

Constraint Types

Particles and Cloth

slide-7
SLIDE 7

Show Me the Demo!

slide-8
SLIDE 8

Bead on a 2D Rigid Wire

( , ) C x y =

Implicit Curve Equation: This is the position constraint.

slide-9
SLIDE 9

How does it move?

v The normal vector is perpendicular to the velocity. n

dot( , ) = n v

slide-10
SLIDE 10

Enter The Calculus

( ) C = x

Position Constraint:

C = &

Velocity Constraint: If C is zero, then its time derivative is zero. x y ⎡ ⎤ = ⎢ ⎥ ⎣ ⎦ x

slide-11
SLIDE 11

Velocity Constraint

Velocity constraints define the allowed motion. Next we’ll show that velocity constraints depend

linearly on velocity.

C = &

slide-12
SLIDE 12

The Jacobian

Due to the chain rule the velocity constraint has a special structure:

C = Jv &

J is a row vector called the Jacobian. J depends on position. x y ⎡ ⎤ = ⎢ ⎥ ⎣ ⎦ v & & The velocity constraint is linear.

slide-13
SLIDE 13

The Jacobian

v

T

J The Jacobian is perpendicular to the velocity.

C = = Jv &

slide-14
SLIDE 14

Constraint Force

v Assume the wire is frictionless. What is the force between the wire and the bead?

slide-15
SLIDE 15

Lagrange Multiplier

v

c

F Intuitively the constraint force Fc is parallel to the normal vector.

T c

λ = F J

Direction known. Magnitude unknown. implies

slide-16
SLIDE 16

Lagrange Multiplier

The Lagrange Multiplier (lambda) is the constraint force

signed magnitude.

We use a constraint solver to compute lambda. More on this later.

slide-17
SLIDE 17

Jacobian as a CoordinateTransform

Similar to a rotation matrix. Except it is missing a couple rows. So it projects some dimensions to zero. The transpose is missing some columns, so some

dimensions get added.

slide-18
SLIDE 18

Velocity Transform

v J C &

Cartesian Space Velocity Constraint Space Velocity

C = Jv &

slide-19
SLIDE 19

Force Transform

c

F

Constraint Space Force Cartesian Space Force

λ

T

J

T c

λ = F J

slide-20
SLIDE 20

Refresher: Work and Power

Work = Force times Distance Work has units of Energy (Joules) Power = Force times Velocity (Watts)

( )

dot , P = F V

slide-21
SLIDE 21

Principle of Virtual Work

T c

λ = F J

Principle: constraint forces do no work.

( )

T T T c c

P λ λ = = = = F v J v Jv

Proof (compute the power): The power is zero, so the constraint does no work. We can ensure this by using:

slide-22
SLIDE 22

Constraint Quantities

C C & J λ

Position Constraint Velocity Constraint Jacobian Lagrange Multiplier

slide-23
SLIDE 23

Why all the Painful Abstraction?

We want to put all constraints into a common form for

the solver.

This allows us to efficiently try different solution

techniques.

slide-24
SLIDE 24

Addendum: Modeling Time Dependence

Some constraints, like motors, have prescribed motion. This is represented by time dependence.

( )

, C t = x ( ) C b t = + = Jv &

Position: Velocity: velocity bias

slide-25
SLIDE 25

Example: Distance Constraint

T

C = x v x &

T

= x J x

y x L C L = − x b = Position: Velocity: Jacobian: Velocity Bias: λ is the tension particle x y ⎡ ⎤ = ⎢ ⎥ ⎣ ⎦ x

slide-26
SLIDE 26

Gory Details

( )

( ) ( )

2 2 2 2 2 2 2 2 2 2

1 2 2 2 1

x y T T x y

dC d x y L dt dt d dL x y dt dt x y xv yv x y v x v y x y = + − = + − + + = − + ⎡ ⎤ ⎡ ⎤ = = ⎢ ⎥ ⎢ ⎥ + ⎣ ⎦ ⎣ ⎦ x v x

slide-27
SLIDE 27

Computing the Jacobian

At first, it is not easy to compute the Jacobian. It gets easier with practice. If you can define a position constraint, you can find its

Jacobian.

Here’s how …

slide-28
SLIDE 28

A Recipe for J

Use geometry to write C. Differentiate C with respect to time. Isolate v. Identify J and b by inspection.

C b = + Jv &

slide-29
SLIDE 29

Constraint Potpourri

Joints Motors Contact Restitution Friction

slide-30
SLIDE 30

Joint: Distance Constraint

T c

λ = F J y x v

a

m = F g

T

= x J x

slide-31
SLIDE 31

Motors

A motor is a constraint with limited force (torque). θ sin C t θ = − Example 10 10 λ − ≤ ≤ A Wheel Note: this constraint does work.

slide-32
SLIDE 32

Velocity Only Motors

ω 2 C ω = − & Example Usage: A wheel that spins at a constant rate. We don’t care about the angle. 5 5 λ − ≤ ≤

slide-33
SLIDE 33

Inequality Constraints

So far we’ve looked at equality constraints (because

they are simpler).

Inequality constraints are needed for contact and joint

limits.

We put all inequality position constraints into this form:

( , ) C t ≥ x

slide-34
SLIDE 34

Inequality Constraints

C ≤

The corresponding velocity constraint: If

C ≥ &

Else skip constraint enforce:

slide-35
SLIDE 35

Inequality Constraints

Force Limits: Inequality constraints don’t suck.

λ ≤ ≤ ∞

slide-36
SLIDE 36

Contact Constraint

Non-penetration. Restitution: bounce Friction: sliding, sticking, and rolling

slide-37
SLIDE 37

Non-Penetration Constraint

δ n

C δ =

p body 2 body 1 (separation)

slide-38
SLIDE 38

Non-Penetration Constraint

( ) ( ) ( ) ( )

2 1 2 2 2 1 1 1 1 1 1 2 2 2

( )

p p T

C = − ⋅ ⎡ ⎤ = + × − − − × − ⋅ ⎣ ⎦ − ⎡ ⎤ ⎡ ⎤ ⎢ ⎥ ⎢ ⎥ − − × ⎢ ⎥ ⎢ ⎥ = ⎢ ⎥ ⎢ ⎥ ⎢ ⎥ ⎢ ⎥ − × ⎣ ⎦ ⎣ ⎦ v v n v ω p x v ω p x n n v p x n ω n v p x n ω & J

( ) ( ) ( )

⋅ × = ⋅ × = ⋅ × A B C C A B B C A Handy Identities

slide-39
SLIDE 39

Restitution

2 1

( )

n p p

v − ⋅ v v n ฀

Relative normal velocity Adding bounce as a velocity bias

n

b ev− =

n n

C v ev

+ −

= + ≥ &

n n

v ev

+ −

≥ −

Velocity Reflection

slide-40
SLIDE 40

Friction Constraint

Friction is like a velocity-only motor. The target velocity is zero. p

( ) ( )

p T

C = ⋅ ⎡ ⎤ = + × − ⋅ ⎣ ⎦ ⎡ ⎤ ⎡ ⎤ = ⎢ ⎥ ⎢ ⎥ − × ⎣ ⎦ ⎣ ⎦ v t v ω p x t t v p x t ω & J t

slide-41
SLIDE 41

Friction Constraint

The friction force is limited by the normal force. Coulomb’s Law:

t n

λ μλ ≤

In 2D:

n t n

μλ λ μλ − ≤ ≤

3D is a bit more complicated. See the references.

slide-42
SLIDE 42

Constraints Solvers

We have a bunch of constraints. We have unknown constraint forces. We need to solve for these constraint forces. There are many ways different ways to compute

constraint forces.

slide-43
SLIDE 43

Constraint Solver Types

Global Solvers (slow) Iterative Solvers (fast)

slide-44
SLIDE 44

Solving a Chain

λ1 λ2 λ3 Global: solve for λ1, λ2, and λ3 simultaneously. Iterative: while !done solve for λ1 solve for λ2 solve for λ3

1

m

2

m

3

m

slide-45
SLIDE 45

Sequential Impulses (SI)

An iterative solver. SI applies impulses at each constraint to correct the

velocity error.

SI is fast and stable. Converges to a global solution.

slide-46
SLIDE 46

Why Impulses?

Easier to deal with friction and collision. Lets us work with velocity rather than acceleration. Given the time step, impulse and force are

interchangeable.

h = P F

slide-47
SLIDE 47

Sequential Impulses

Step1: Integrate applied forces, yielding tentative velocities. Step2: Apply impulses sequentially for all constraints, to correct the velocity errors. Step3: Use the new velocities to update the positions.

slide-48
SLIDE 48

Step 1: Newton’s Law

a c

= + Mv F F &

We separate applied forces and constraint forces.

mass matrix

slide-49
SLIDE 49

Step 1: Mass Matrix

m m m ⎡ ⎤ ⎢ ⎥ = ⎢ ⎥ ⎢ ⎥ ⎣ ⎦ M Particle Rigid Body m ⎡ ⎤ = ⎢ ⎥ ⎣ ⎦ E M I May involve multiple particles/bodies.

slide-50
SLIDE 50

Step 1: Applied Forces

Applied forces are computed according to some law. Gravity: F = mg Spring: F = -kx Air resistance: F = -cv2

slide-51
SLIDE 51

Step 1 : Integrate Applied Forces

1 2 1 a

h

= + v v M F

Euler’s Method for all bodies. This new velocity tends to violate the velocity constraints.

slide-52
SLIDE 52

Step 2: Constraint Impulse

The constraint impulse is just the time step times the constraint force.

c c

h = P F

slide-53
SLIDE 53

Step 2: Impulse-Momentum

Newton’s Law for impulses: c

Δ = M v P

In other words:

1 2 2 c −

= + v v M P

slide-54
SLIDE 54

Step 2: Computing Lambda

For each constraint, solve these for λ:

1 2 2 2 c T c

b λ

= + = + = v v M P P J Jv Newton’s Law: Virtual Work: Velocity Constraint: Note: this usually involves one or two bodies.

slide-55
SLIDE 55

Step 2: Impulse Solution

( )

2 1

1

C C T

m b m λ

= − + = Jv JM J

The scalar mC is the effective mass seen by the constraint impulse:

C

m C λ Δ = &

slide-56
SLIDE 56

Step 2: Velocity Update

1 2 2 T c c

λ

= = + P J v v M P

Now that we solved for lambda, we can use it to update the velocity. Remember: this usually involves one or two bodies.

slide-57
SLIDE 57

Step 2: Iteration

Loop over all constraints until you are done:

  • Fixed number of iterations.
  • Corrective impulses become small.
  • Velocity errors become small.
slide-58
SLIDE 58

Step 3: Integrate Positions

2 1 2

h = + x x v

Use the new velocity to integrate all body positions (and orientations): This is the symplectic Euler integrator.

slide-59
SLIDE 59

Extensions to Step 2

Handle position drift. Handle force limits. Handle inequality constraints. Warm starting.

slide-60
SLIDE 60

Handling Position Drift

Velocity constraints are not obeyed precisely. Joints will fall apart.

slide-61
SLIDE 61

Baumgarte Stabilization

Feed the position error back into the velocity constraint.

B

C C h β = + = Jv &

New velocity constraint: Bias factor:

1 β ≤ ≤

slide-62
SLIDE 62

Baumgarte Stabilization

What is the solution to this?

C C h β + = &

First-order differential equation …

slide-63
SLIDE 63

Answer

0 exp

t C C h β ⎛ ⎞ = − ⎜ ⎟ ⎝ ⎠

( )

exp t −

slide-64
SLIDE 64

Tuning the Bias Factor

If your simulation has instabilities, set the bias factor to

zero and check the stability.

Increase the bias factor slowly until the simulation

becomes unstable.

Use half of that value.

slide-65
SLIDE 65

Handling Force Limits

First, convert force limits to impulse limits.

impulse force

h λ λ =

slide-66
SLIDE 66

Handling Impulse Limits

Clamping corrective impulses:

( )

min max

clamp , , λ λ λ λ =

Is it really that simple? Hint: no.

slide-67
SLIDE 67

How to Clamp

Each iteration computes corrective impulses. Clamping corrective impulses is wrong! You should clamp the total impulse applied over the

time step.

The following example shows why.

slide-68
SLIDE 68

Example: 2D Inelastic Collision

v P A Falling Box P Global Solution 1 2 m = P v

slide-69
SLIDE 69

Iterative Solution

1

P

2

P iteration 1 constraint 1 constraint 2 Suppose the corrective impulses are too strong. What should the second iteration look like?

slide-70
SLIDE 70

Iterative Solution

1

P

2

P iteration 2 To keep the box from bouncing, we need downward corrective impulses. In other words, the corrective impulses are negative!

slide-71
SLIDE 71

Iterative Solution

But clamping the negative corrective impulses wipes them out:

clamp( , 0, ) λ λ = ∞ =

This is one way to introduce jitter into your simulation. ☺

slide-72
SLIDE 72

Accumulated Impulses

For each constraint, keep track of the total impulse

applied.

This is the accumulated impulse. Clamp the accumulated impulse. This allows the corrective impulse to be negative yet the

accumulated impulse is still positive.

slide-73
SLIDE 73

New Clamping Procedure

  • 1. Compute the corrective impulse, but don’t

apply it.

  • 2. Make a copy of the old accumulated impulse.
  • 3. Add the corrective impulse to the accumulated

impulse.

  • 4. Clamp the accumulated impulse.
  • 5. Compute the change in the accumulated

impulse using the copy from step 2.

  • 6. Apply the impulse delta found in Step 5.
slide-74
SLIDE 74

Handling Inequality Constraints

Before iterations, determine if the inequality constraint is

active.

If it is inactive, then ignore it. Clamp accumulated impulses:

acc

λ ≤ ≤ ∞

slide-75
SLIDE 75

Inequality Constraints

A problem:

  • vershoot

active inactive active gravity Aiming for zero overlap leads to JITTER!

slide-76
SLIDE 76

Preventing Overshoot

( )

slop

C h β δ δ = + − Jv &

Allow a little bit of penetration (slop). If separation < slop C = Jv & Else Note: the slop will be negative (separation).

slide-77
SLIDE 77

Warm Starting

Iterative solvers use an initial guess for the lambdas. So save the lambdas from the previous time step. Use the stored lambdas as the initial guess for the new

step.

Benefit: improved stacking.

slide-78
SLIDE 78

Step 1.5

Apply the stored impulses. Use the stored impulses to initialize the accumulated

impulses.

slide-79
SLIDE 79

Step 2.5

Store the accumulated impulses.

slide-80
SLIDE 80

Further Reading & Sample Code

http://www.gphysics.com/downloads/

slide-81
SLIDE 81

Box2D

An open source 2D physics engine. http://www.box2d.org Written in C++.