Modeling and Solving Constraints. Erin Catto Blizzard Entertainment

Similar documents
Here s a typical flow chart for game physics.

the gamedesigninitiative at cornell university Lecture 17 Physics and Motion

Game Physics. Game and Media Technology Master Program - Utrecht University. Dr. Nicolas Pronost

Lagrangian Dynamics: Generalized Coordinates and Forces

PHY 101. Work and Kinetic Energy 7.1 Work Done by a Constant Force

July 19 - Work and Energy 1. Name Date Partners

PSE Game Physics. Session (6) Angular momentum, microcollisions, damping. Oliver Meister, Roland Wittmann

[variable] = units (or dimension) of variable.

Chapter 8: Momentum, Impulse, & Collisions. Newton s second law in terms of momentum:

PHYSICS 221, FALL 2011 EXAM #2 SOLUTIONS WEDNESDAY, NOVEMBER 2, 2011

Exam 2: Equation Summary

the gamedesigninitiative at cornell university Lecture 18 Box2D Physics

Physics Tutorial 6: Collision Response

Distance travelled time taken and if the particle is a distance s(t) along the x-axis, then its instantaneous speed is:

LAB 6: WORK AND ENERGY

St. Joseph s Anglo-Chinese School

Lesson 8. Luis Anchordoqui. Physics 168. Thursday, October 11, 18

Chapter 9. Linear Momentum

Constrained motion and generalized coordinates

STEP Support Programme. Mechanics STEP Questions

Principles of Computer Game Design and Implementation. Lecture 17

GAME PHYSICS ENGINE DEVELOPMENT

Chapter 11. Angular Momentum

Announcements. 1. Do not bring the yellow equation sheets to the miderm. Idential sheets will be attached to the problems.

Visual Interactive Simulation, TDBD24, Spring 2006

Recall: Gravitational Potential Energy

Game Physics. Game and Media Technology Master Program - Utrecht University. Dr. Nicolas Pronost

Center of Gravity. The location of the center of gravity is defined by: n mgx. APSC 111 Review Page 7

!T = 2# T = 2! " The velocity and acceleration of the object are found by taking the first and second derivative of the position:

Physics 2514 Lecture 26

2007 Problem Topic Comment 1 Kinematics Position-time equation Kinematics 7 2 Kinematics Velocity-time graph Dynamics 6 3 Kinematics Average velocity

r CM = ir im i i m i m i v i (2) P = i

First Year Physics: Prelims CP1 Classical Mechanics: DR. Ghassan Yassin

Simulation in Computer Graphics Elastic Solids. Matthias Teschner

Kinematics (special case) Dynamics gravity, tension, elastic, normal, friction. Energy: kinetic, potential gravity, spring + work (friction)

CS-184: Computer Graphics

Assignments VIII and IX, PHYS 301 (Classical Mechanics) Spring 2014 Due 3/21/14 at start of class

Collision Resolution

Stop my Constraints from Blowing Up! Oliver Strunk (Havok)

Physics 351 Wednesday, January 14, 2015

Practice Problems for Exam 2 Solutions

MASSACHUSETTS INSTITUTE OF TECHNOLOGY Physics Department 8.01 Physics I Fall Term 2009 Review Module on Solving N equations in N unknowns

Concept Question: Normal Force

Forces Part 1: Newton s Laws

Time scales. Notes. Mixed Implicit/Explicit. Newmark Methods [ ] [ ] "!t = O 1 %

Notes on numerical solution of differential equations

Physics Tutorial 6: Collision Response

Physics 351 Wednesday, February 14, 2018

WORK ENERGY AND POWER

Concepts in Physics. Friday, October 16th

ES 111 Mathematical Methods in the Earth Sciences Lecture Outline 15 - Tues 20th Nov 2018 First and Higher Order Differential Equations

Exam 3 Practice Solutions

Mechanics II. Which of the following relations among the forces W, k, N, and F must be true?

3. Kinetics of Particles

AN INTRODUCTION TO LAGRANGE EQUATIONS. Professor J. Kim Vandiver October 28, 2016

Work and energy. 15 m. c. Find the work done by the normal force exerted by the incline on the crate.

PHY131 Summer 2011 Class 9 Notes 6/14/11

( m/s) 2 4(4.9 m/s 2 )( 52.7 m)

( m/s) 2 4(4.9 m/s 2 )( 53.2 m)

Chapter 3 Numerical Methods

DO NOT USE CELL PHONES. Good Luck!!!

Torque and Rotation Lecture 7

Lecture 5. Dynamics. Forces: Newton s First and Second

Lecture 9 - Rotational Dynamics

Basic Mathematics and Units

PLANAR KINETIC EQUATIONS OF MOTION (Section 17.2)

Physics 8 Friday, November 4, 2011

Review of Linear Momentum And Rotational Motion

Chapter 9. Linear Momentum and Collisions

Simple Car Dynamics. Outline. Claude Lacoursière HPC2N/VRlab, Umeå Universitet, Sweden, May 18, 2005

Coordinate systems and vectors in three spatial dimensions

Chapter 11. Angular Momentum

8.012 Physics I: Classical Mechanics Fall 2008

Lecture 13: Forces in the Lagrangian Approach

Welcome back to Physics 211

Announcements. Principle of Work and Energy - Sections Engr222 Spring 2004 Chapter Test Wednesday

ME 230: Kinematics and Dynamics Spring 2014 Section AD. Final Exam Review: Rigid Body Dynamics Practice Problem

Lab I. 2D Motion. 1 Introduction. 2 Theory. 2.1 scalars and vectors LAB I. 2D MOTION 15

Chapter 6: Work, Energy and Power Tuesday February 10 th

= o + t = ot + ½ t 2 = o + 2

8.012 Physics I: Classical Mechanics Fall 2008

Physics A - PHY 2048C

The content contained in all sections of chapter 6 of the textbook is included on the AP Physics B exam.

Momentum & Energy Review Checklist

Problem Set 6: Magnetism

Momentum & Energy Review Checklist

PHYS 121: Work, Energy, Momentum, and Conservation Laws, Systems, and Center of Mass Review Sheet

Lab I. 2D Motion. 1 Introduction. 2 Theory. 2.1 scalars and vectors LAB I. 2D MOTION 15

MOMENTUM, IMPULSE & MOMENTS

Physics 111. Thursday, October 07, Conservation of Momentum. Kinetic Energy

Chapter 10: Dynamics of Rotational Motion

Name & Surname:... No:... Class: 11 /...

Inverse Kinematics. Mike Bailey. Oregon State University. Inverse Kinematics

OCR Physics Specification A - H156/H556

APPLIED MATHEMATICS AM 02

Lecture 17 - Gyroscopes

Circular motion. Aug. 22, 2017

Thursday Simulation & Unity

Physics 8, Fall 2011, equation sheet work in progress

Chapter 1. Basic Concepts. 1.1 Trajectories

Transcription:

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. Constraint solvers do this by calculating impulse or forces, and applying them to the constrained bodies.

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

Constraint Types Contact and Friction

Constraint Types Ragdolls

Constraint Types Particles and Cloth

Show Me the Demo!

Bead on a 2D Rigid Wire Implicit Curve Equation: Cxy (, ) = 0 This is the position constraint.

How does it move? The normal vector is perpendicular to the velocity. n v dot( nv, ) = 0

Enter The Calculus Position Constraint: C ( x) = 0 x x = y If C is zero, then its time derivative is zero. Velocity Constraint: C & = 0

Velocity Constraint C & = 0 Velocity constraints define the allowed motion. Next we ll show that velocity constraints depend linearly on velocity.

The Jacobian Due to the chain rule the velocity constraint has a special structure: C & = Jv v x& = y & J is a row vector called the Jacobian. J depends on position. The velocity constraint is linear.

The Jacobian The Jacobian is perpendicular to the velocity. T J v C & = Jv = 0

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

Lagrange Multiplier Intuitively the constraint force F c is parallel to the normal vector. F c v Direction known. Magnitude unknown. implies F c = J T λ

Lagrange Multiplier The Lagrange Multiplier (lambda) is the constraint force signed magnitude. We use a constraint solver to compute lambda. More on this later.

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.

Velocity Transform v J C & Cartesian Space Velocity Constraint Space Velocity C & = Jv

Force Transform λ Constraint Space Force T J F c Cartesian Space Force F c = J T λ

Refresher: Work and Power Work = Force times Distance Work has units of Energy (Joules) Power = Force times Velocity (Watts) P = ( FV) dot,

Principle of Virtual Work Principle: constraint forces do no work. We can ensure this by using: F c = J T λ Proof (compute the power): c T c T ( T ) 0 P λ λ = Fv= J v= Jv= The power is zero, so the constraint does no work.

Constraint Quantities Position Constraint Velocity Constraint Jacobian Lagrange Multiplier C C & J λ

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.

Addendum: Modeling Time Dependence Some constraints, like motors, have prescribed motion. This is represented by time dependence. Position: C( x, t ) = 0 Velocity: C& = Jv + b() t = 0 velocity bias

Example: Distance Constraint x Position: C = x L y L x x = y particle λ is the tension Velocity: Jacobian: Velocity Bias: T C & = x v x T J = x x b = 0

Gory Details dc dt d ( ) 2 2 = x + y L dt 1 d ( 2 2 = x + y ) 2 2 2 x + y dt ( xv ) x + yvy 2 = 0 2 2 2 x + y 1 x vx = 2 2 x y y = v + y T T x x dl dt v

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

A Recipe for J Use geometry to write C. Differentiate C with respect to time. Isolate v. Identify J and b by inspection. C& = Jv + b

Constraint Potpourri Joints Motors Contact Restitution Friction

Joint: Distance Constraint x y F c = J T λ J T = x x v F a = mg

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

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

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(,) x t 0

Inequality Constraints The corresponding velocity constraint: If Else C 0 enforce: C& 0 skip constraint

Inequality Constraints Force Limits: 0 λ Inequality constraints don t suck.

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

Non-Penetration Constraint body 2 body 1 p n δ C = δ (separation)

Non-Penetration Constraint C & = ( v v ) n p2 p1 ( ) ( ) = v2 + ω2 p x2 v1 ω1 p x1 n n v1 ( 1) p x n ω1 = n v 2 ( p x2 ) n ω2 J T Handy Identities ( ) ( ) ( ) A B C = C A B = B C A

Restitution Relative normal velocity Velocity Reflection v ( v v ) n n p2 p1 Adding bounce as a velocity bias v + ev n n C& = v + ev + n n 0 b = ev n

Friction Constraint Friction is like a velocity-only motor. The target velocity is zero. p t C & = v p t ( ) = v + ω p x t t v = ( ) p x t ω J T

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.

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.

Constraint Solver Types Global Solvers (slow) Iterative Solvers (fast)

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

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.

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. P = hf

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.

Step 1: Newton s Law We separate applied forces and constraint forces. Mv& = F + F a c mass matrix

Step 1: Mass Matrix Particle M m 0 0 = 0 m 0 0 0 m Rigid Body M me 0 = 0 I May involve multiple particles/bodies.

Step 1: Applied Forces Applied forces are computed according to some law. Gravity: F = mg Spring: F = -kx Air resistance: F = -cv 2

Step 1 : Integrate Applied Forces Euler s Method for all bodies. = +h 1 2 1 a v v M F This new velocity tends to violate the velocity constraints.

Step 2: Constraint Impulse The constraint impulse is just the time step times the constraint force. P c = hf c

Step 2: Impulse-Momentum Newton s Law for impulses: Mv Δ = P c In other words: = + 1 2 2 c v v M P

Step 2: Computing Lambda For each constraint, solve these for λ: Newton s Law: 1 v2 = v2+ M Pc Virtual Work: P c = J T λ Velocity Constraint: Jv + = 2 b 0 Note: this usually involves one or two bodies.

Step 2: Impulse Solution λ = m Jv + b C ( ) 2 m C = 1 1 T JM J The scalar m C is the effective mass seen by the constraint impulse: m Δ C& = λ C

Step 2: Velocity Update Now that we solved for lambda, we can use it to update the velocity. P c = J T 2 2 λ 1 v = v + M P c Remember: this usually involves one or two bodies.

Step 2: Iteration Loop over all constraints until you are done: - Fixed number of iterations. - Corrective impulses become small. - Velocity errors become small.

Step 3: Integrate Positions Use the new velocity to integrate all body positions (and orientations): x = x +hv 2 1 2 This is the symplectic Euler integrator.

Extensions to Step 2 Handle position drift. Handle force limits. Handle inequality constraints. Warm starting.

Handling Position Drift Velocity constraints are not obeyed precisely. Joints will fall apart.

Baumgarte Stabilization Feed the position error back into the velocity constraint. New velocity constraint: & CB β = Jv + C = h 0 Bias factor: 0 β 1

Baumgarte Stabilization What is the solution to this? C& β + C = h 0 First-order differential equation

Answer β C C exp t = 0 h exp ( t)

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.

Handling Force Limits First, convert force limits to impulse limits. λ impulse = h λ force

Handling Impulse Limits Clamping corrective impulses: ( ) λ = clamp λ, λ, λ min max Is it really that simple? Hint: no.

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.

Example: 2D Inelastic Collision A Falling Box v Global Solution P= 1 2 mv P P

Iterative Solution iteration 1 P1 P2 constraint 1 constraint 2 Suppose the corrective impulses are too strong. What should the second iteration look like?

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

Iterative Solution But clamping the negative corrective impulses wipes them out: λ = clamp( λ, 0, ) = 0 This is one way to introduce jitter into your simulation.

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.

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.

Handling Inequality Constraints Before iterations, determine if the inequality constraint is active. If it is inactive, then ignore it. Clamp accumulated impulses: 0 λacc

Inequality Constraints A problem: overshoot gravity active inactive active Aiming for zero overlap leads to JITTER!

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

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.

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

Step 2.5 Store the accumulated impulses.

Further Reading & Sample Code http://www.gphysics.com/downloads/

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