CPSC 427 Video Game Programming
Animation
Physics-Based Simulation
• Movement governed by forces • Simple – Independent particles • Complex – Correct collisions, stacking, sliding 3D rigid bodies • Many many simulators! – PhysX (Unity, Unreal), Bullet, Open Dynamics Engine, MuJoCo, Havok, Box2D, Chipmunk, 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. Collision detection 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 t t
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 𝒏 𝒋 𝟏 𝝐 𝒎 ⃑ 𝒋 𝒏