CPSC 427 Programming

Animation

Physics-Based

• Movement governed by forces • Simple – Independent particles • Complex – Correct collisions, stacking, sliding 3D rigid bodies • Many many simulators! – PhysX (, Unreal), , , MuJoCo, , , , OpenSim, RBDL, Simulink (MATLAB), ADAMS, SD/FAST, DART etc…

© Alla Sheffer/M. van de Panne

Copyright: Alla Sheffer/Michiel van de Panne 1 Examples

• Particle systems – Fire, water, smoke, pebbles • Rigid-body simulation – Blocks, robots, humans • Continuum systems – Deformable solids – Fluids, cloth, hair • Group movement – Flocks, crowds © Alla Sheffer/M. van de Panne

Simulation Basics

Simulation loop… 1. Equations of Motion  sum forces & torques  solve for accelerations: 𝑭 𝒎𝒂 2. Numerical integration  update positions, velocities 3. 4. Collision resolution

© Alla Sheffer/M. van de Panne

Copyright: Alla Sheffer/Michiel van de Panne 2 Particles: Newtonian Physics as First-Order ODE

• Motion of one particle • Motion of many particles

𝑣 Second-order ODE 𝑥 𝜕 𝑥 𝑣 𝐹 /𝑚 𝐹⃑ 𝑚 𝜕𝑡 𝜕 𝑥 𝑣 𝑣 𝜕𝑡 𝐹/𝑚 First-order ODE ⋮ ⋮ 𝜕 𝑣⃑ 𝑥 𝑣 𝑥⃑ ⃑ 𝑣 𝜕𝑡 𝑣⃑ 𝛴𝐹/𝑚 𝐹/𝑚

© Alla Sheffer/M. van de Panne

Basic Particle Simulation (first try)

Forces only 𝒇 𝒎𝒂

𝒅𝒕 𝒕𝒊𝟏 𝒕𝒊

𝒗𝒊𝟏 𝒗 𝒕𝒊 𝒇𝒕𝒊/𝒎𝒅𝒕 𝒑𝒊𝟏 𝒑 𝒕𝒊 𝒗𝒕𝒊𝟏𝒅𝒕

© Alla Sheffer/M. van de Panne

Copyright: Alla Sheffer/Michiel van de Panne 3 Basic Particle Simulation (first try)

Forces only 𝑭 𝒎𝒂

𝒅𝒕 𝒕𝒊𝟏 𝒕𝒊

𝒗𝒊𝟏 𝒗 𝒕𝒊 𝒇𝒕𝒊/𝒎𝒅𝒕 𝒑𝒊𝟏 𝒑 𝒕𝒊 𝒗𝒕𝒊𝟏𝒅𝒕

© Alla Sheffer/M. van de Panne

Basic Particle Forces

• Gravity 0 𝐹 𝑚𝑔

• Viscous damping 𝐹 𝑏𝑣

• Spring & dampers 𝐹𝑘𝑥 𝑏𝑣

© Alla Sheffer/M. van de Panne

Copyright: Alla Sheffer/Michiel van de Panne 4 Proxy Forces

• Behavior forces: • Fluids flocking birds, schooling fish, etc. [“Curl Noise for Procedural Fluid Flow” [“Boids”, Craig Reynolds, SIGGRAPH 1987] R. Bridson, J. Hourihan, M. Nordenstam, Proc. SIGGRAPH 2007]

© Alla Sheffer/M. van de Panne

Basic Particle Simulation: Small Problem…

Forces only 𝑭 𝒎𝒂

𝒅𝒕 𝒕𝒊𝟏 𝒕𝒊

𝒗𝒊𝟏 𝒗 𝒕𝒊 𝒇𝒕𝒊/𝒎𝒅𝒕 𝒑𝒊𝟏 𝒑 𝒕𝒊 𝒗𝒕𝒊𝟏𝒅𝒕

Equations of motion describe state (equilibrium)

Use: get values at time 𝒕𝒊𝟏from values at time 𝒕𝒊

© Alla Sheffer/M. van de Panne

Copyright: Alla Sheffer/Michiel van de Panne 5 Newtonian Physics as First-Order ODE

• Motion of one particle • Motion of many particles

𝑣 Second-order ODE 𝑥 𝜕 𝑥 𝑣 𝐹 /𝑚 𝐹⃑ 𝑚 𝜕𝑡 𝜕 𝑥 𝑣 𝑣 𝜕𝑡 𝐹/𝑚 First-order ODE ⋮ ⋮ 𝜕 𝑣⃑ 𝑥 𝑣 𝑥⃑ ⃑ 𝑣 𝜕𝑡 𝑣⃑ 𝛴𝐹/𝑚 𝐹/𝑚

© Alla Sheffer/M. van de Panne

Ordinary Differential Equations

𝜕 𝑋⃑𝑡 𝑓𝑋⃑ 𝑡,𝑡 𝜕𝑡

Given that 𝑋⃑ 𝑋⃑ 𝑡

Compute 𝑋⃑ 𝑡 for t𝑡 ∆𝑋⃑𝑡 𝑓𝑋⃑ 𝑡,𝑡∆𝑡 • Simulation: – path through state-space – driven by vector field

© Alla Sheffer/M. van de Panne

Copyright: Alla Sheffer/Michiel van de Panne 6 ODE Numerical Integration: Explicit (Forward) Euler

𝜕 𝑋⃑𝑡 𝑓𝑋⃑ 𝑡,𝑡 𝜕𝑡

Given that 𝑋⃑ 𝑋⃑ 𝑡 𝒇𝑿 𝒕,𝒕 Compute 𝑋⃑ 𝑡 for tt

t t ∆𝑡 𝑿 𝒕∆𝒕 ∆𝒕 𝒇𝑿 𝒕,𝒕 ∆𝑋⃑ 𝑡∆𝑡 𝑓𝑋⃑ 𝑡,𝑡 𝑿 𝒕 𝑋⃑ 𝑋⃑ + ∆𝑡 𝑓𝑋⃑,t

© Alla Sheffer/M. van de Panne

ODE Numerical Integration: Explicit (Forward) Euler

𝜕 𝑋⃑𝑡 𝑓𝑋⃑ 𝑡,𝑡 𝜕𝑡

Given that 𝑋⃑ 𝑋⃑ 𝑡 𝒇𝑿 𝒕,𝒕 Compute 𝑋⃑ t for t t

∆𝑡 t t 𝑿 𝒕∆𝒕 ∆𝒕 𝒇𝑿 𝒕,𝒕 ∆𝑋⃑ t ∆𝑡 𝑓𝑋⃑ t ,t 𝑿 𝒕 𝑋⃑ 𝑋⃑ + ∆𝑡 𝑓𝑋⃑,t

© Alla Sheffer/M. van de Panne

Copyright: Alla Sheffer/Michiel van de Panne 7 Explicit Euler Problems

• Solution spirals out – Even with small time steps – Although smaller time steps are still better

© Alla Sheffer/M. van de Panne

Explicit Euler Problems

• Can lead to instabilities

© Alla Sheffer/M. van de Panne

Copyright: Alla Sheffer/Michiel van de Panne 8 Midpoint Method

1. ½ Euler step 2. evaluate f at 𝑿 m 𝒎 𝒇𝑿 𝒕,𝒕 3. full step using fm

𝒇𝒎

𝑿𝒎 𝑿 𝒕 ½ ∆𝑿𝒕

½ ∆𝒕 𝒇 𝑿 𝒕,𝒕

𝑿 𝒕∆𝒕𝒎 𝒇 𝑿 𝒕

© Alla Sheffer/M. van de Panne

Trapezoid Method

1. full Euler step get 𝑿𝒂

2. evaluate ft at 𝑿𝒂 𝒇𝑿 𝒕,𝒕

3. full step using ft get 𝑿𝒃 𝒇𝒕 4. average 𝑿𝒂 and 𝑿𝒃 𝑿𝒂 𝑿∆𝑿𝒕

∆𝒕 𝒇𝑿 𝒕,𝒕 ½𝑿𝒂 ½𝑿𝒃

𝑿 𝒕 𝑿𝒃 𝑿 𝒕∆𝒕𝒇𝒕 © Alla Sheffer/M. van de Panne

Copyright: Alla Sheffer/Michiel van de Panne 9 Midpoint & Trapezoid Method

• Not exactly the same – But same order of accuracy

𝑻𝒓𝒂𝒑𝒆𝒛𝒐𝒊𝒅 𝒎𝒆𝒕𝒉𝒐𝒅

𝑴𝒊𝒅𝒐𝒊𝒏𝒕 𝒎𝒆𝒕𝒉𝒐𝒅 𝑿 𝒕

© Alla Sheffer/M. van de Panne

Explicit Euler: Code

© Alla Sheffer/M. van de Panne

Copyright: Alla Sheffer/Michiel van de Panne 10 Midpoint Method: Code

𝒇𝒎

𝑿𝒎 𝑿 𝒕 ½ ∆𝑿𝒕

½ ∆𝒕 𝒇 𝑿 𝒕,𝒕

𝑿 𝒕∆𝒕𝒎 𝒇 𝑿 𝒕

© Alla Sheffer/M. van de Panne

Implicit (Backward) Euler:

• Use the derivative at the destination

Forward Euler

𝒙𝒏𝟏 𝒙𝒏 𝒉 𝒏𝒗 𝒌 𝒙 𝒗 𝒗 𝒉 𝒏 𝒏𝟏 𝒏 𝒎 Backward Euler

𝒙𝒏𝟏 𝒙𝒏 𝒉 𝒏𝟏𝒗 • Problem is we don’t know the 𝒌 𝒙 𝒗 𝒗 𝒉 𝒏𝟏 destination yet 𝒏𝟏 𝒏 𝒎 © Alla Sheffer/M. van de Panne

Copyright: Alla Sheffer/Michiel van de Panne 11 Implicit (Backward) Euler: Solve a (linear) system

• Use the derivative at the destination

Forward Euler

𝒙𝒏𝟏 𝒙𝒏 𝒉 𝒏𝒗 𝒌 𝒙 𝒗 𝒗 𝒉 𝒏 𝒏𝟏 𝒏 𝒎 Backward Euler

𝒙𝒏𝟏 𝒙𝒏 𝒉 𝒏𝟏𝒗 • Problem is we don’t know the 𝒌 𝒙 𝒗 𝒗 𝒉 𝒏𝟏 destination yet 𝒏𝟏 𝒏 𝒎 © Alla Sheffer/M. van de Panne

Simulation Basics

Simulation loop… 1. Equations of Motion 2. Numerical integration 3. Collision detection 4. Collision resolution

© Alla Sheffer/M. van de Panne

Copyright: Alla Sheffer/Michiel van de Panne 12 Collisions

• Collision detection – Broad phase: AABBs, bounding spheres – Narrow phase: detailed checks • Collision response – Collision impulses – Constraint forces: resting, sliding, hinges, ….

© Alla Sheffer/M. van de Panne

Particle-Plane Collisions

• Particle-plane frictionless impulse response – Invert & scale perpendicular velocity component

𝒗 𝝐𝒗 𝒏 𝒗∥ 𝒗∥

𝒗 𝒗

© Alla Sheffer/M. van de Panne

Copyright: Alla Sheffer/Michiel van de Panne 13 Particle-Plane Collisions

• More formally… – Apply an impulse of magnitude j Inversely proportional to mass of particle – In direction of normal 𝒏 𝒋 𝟏𝝐 𝒎 ⃑ 𝒋 𝒏

⃑ 𝒗 𝒗 𝒗 𝒗 𝒎

© Alla Sheffer/M. van de Panne

Particle-Particle Collisions

• Particle-particle frictionless elastic impulse response

𝒗 𝒗𝟏 𝟏 𝒗 𝒎 𝒎𝟏 𝟐 𝟏 𝒗𝟐 𝒎 𝒎𝟐 𝟐 Before After

• Momentum is preserved 𝒎𝟏𝒗𝟏 𝒎𝟐𝒗𝟐 𝒎𝟏𝒗𝟏 𝒎𝟐𝒗𝟐 • Kinetic energy is preserved 𝟐 𝟐 𝟐 𝟐 ½ 𝒎𝟏𝒗𝟏 ½ 𝒎𝟐𝒗𝟐 ½ 𝒎𝟏𝒗𝟏 ½ 𝒎𝟐𝒗𝟐 © Alla Sheffer/M. van de Panne

Copyright: Alla Sheffer/Michiel van de Panne 14 Particle-Particle Collisions (radius >0)

• What we know… – Particle centers 𝒗𝟐 – Initial velocities 𝒑𝟐: 𝒙𝟐,𝒚𝟐 – Particle Masses

• What we can calculate… 𝒎𝟐 𝒑 : 𝒙 ,𝒚 – Contact normal 𝟏 𝟏 𝟏 𝒗𝟏 – Contact tangent

𝒎𝟏

© Alla Sheffer/M. van de Panne

Particle-Particle Collisions (radius >0)

• Impulse direction 𝒗𝟐

reflected across tangent 𝒗𝟐

• Impulse magnitude 𝒑𝟐: 𝒙𝟐,𝒚𝟐 proportional to mass of other 𝒎𝟐 particle 𝒑 : 𝒙 ,𝒚 𝟏 𝟏 𝟏 𝒗𝟏

𝒎 𝟏 𝒗𝟏 𝒎𝟐 𝒎 © Alla Sheffer/M. van de Panne

Copyright: Alla Sheffer/Michiel van de Panne 15 Particle-Particle Collisions (radius >0)

• More formally…

𝟐𝒎𝟐 𝒗𝟏 𝒗𝟐 · 𝒑𝟏 𝒑𝟐 𝒗𝟏 𝒗𝟏 𝟐 𝒑𝟏 𝒑𝟐 𝒎𝟏 𝒎𝟐 𝒑𝟏 𝒑𝟐

𝟐𝒎𝟏 𝒗𝟐 𝒗𝟏 · 𝒑𝟐 𝒑𝟏 𝒗𝟐 𝒗𝟐 𝟐 𝒑𝟐 𝒑𝟏 𝒎𝟏 𝒎𝟐 𝒑𝟐 𝒑𝟏

© Alla Sheffer/M. van de Panne

Rigid Body Dynamics

• From particles to rigid bodies…

Particle Rigid body

𝒙 𝒑𝒐𝒔𝒊𝒕𝒊𝒐𝒏 𝒙 𝒑𝒐𝒔𝒊𝒕𝒊𝒐𝒏 𝒔𝒕𝒂𝒕𝒆 𝒗 𝒗𝒆𝒍𝒐𝒄𝒊𝒕𝒚 𝒗 𝒗𝒆𝒍𝒐𝒄𝒊𝒕𝒚 𝒔𝒕𝒂𝒕𝒆 𝒒, 𝑹 𝒓𝒐𝒕𝒂𝒕𝒊𝒐𝒏 𝒎𝒂𝒕𝒓𝒊𝒙𝟑𝒙𝟑 ℝ𝟒 in 2D 𝒘 𝒂𝒏𝒈𝒖𝒍𝒂𝒓 𝒗𝒆𝒍𝒐𝒄𝒊𝒕𝒚 ℝ𝟔 in 3D ℝ𝟏𝟐 in 3D © Alla Sheffer/M. van de Panne

Copyright: Alla Sheffer/Michiel van de Panne 16 Rigid Body Dynamics

• From particles to rigid bodies…

Newton’s equations of motion Newton-Euler equations of motion 𝚺𝑭 𝒎𝒂 𝒎 𝒂𝒙 𝒎 𝒂𝒚 𝚺𝑭 𝒂 𝒎 𝒂𝒛 𝒎 𝒙 𝒎 𝒂𝒚 𝚺𝑭 𝒘𝒙 𝒎 𝒂𝒛 𝚰 𝒘𝒚 𝒘𝒛 𝑴𝒂 𝚺𝑭 Inertia tensor 𝚺𝝉 𝒘𝚰𝒘

© Alla Sheffer/M. van de Panne

Resources

• Non-convex rigid bodies with stacking 3D collision processing and stacking http://www.cs.ubc.ca/~rbridson/docs/rigid_bodies.pdf

• Physically-based Modeling, course notes, SIGGRAPH 2001, Baraff & Witkin http://www.pixar.com/companyinfo/research/pbm2001/

• Doug James CS 5643 course notes http://www.cs.cornell.edu/courses/cs5643/2015sp/

• Rigid Body Dynamics, Chris Hecker http://chrishecker.com/Rigid_Body_Dynamics

• Video tutorial https://www.toptal.com/game/video-game-physics-part-i-an-introduction-to-rigid-body-dynamics

• Box2D live demos http://heikobehrens.net/misc/box2d.js/examples/

• Rigid body collisions javascript demo https://www.myphysicslab.com/engine2D/collision-en.html

• Rigid Body Collision Reponse, Michael Manzke, course slides https://www.scss.tcd.ie/Michael.Manzke/CS7057/cs7057-1516-09-CollisionResponse-mm.pdf

• Interactive simulation of rigid body dynamics in , CGF 2014 http://onlinelibrary.wiley.com/doi/10.1111/cgf.12272/abstract

• A Mathematical Introduction to Robotic Manipulation (textbook) http://www.cds.caltech.edu/~murray/books/MLS/pdf/mls94-complete.pdf

• Particle-based Fluid Simulation for Interactive Applications, SCA 2003, PDF © Alla Sheffer/M. van de Panne

Copyright: Alla Sheffer/Michiel van de Panne 17