Game Physics References: PBM 2001 Advanced character dynamics Content Newton 2nd law Euler’s method Collision detection & response Particle system data structure Verlet integration Particles with constraints Fall 2012 Constraint dynamics Point based dynamics “character animation paper” (ragdoll, hitman) 2 Basics Newton 2nd Law f ma Equation of Motion of a Newtonian Particle x m1 f Second order ODE requires two conditions Initial value problem: give x(0) and v(0) Fall 2012 3 Euler’s Method x v v m1 f Fall 2012 x v v 1 f m Euler’s Method dx f x dt x f x t xt t xt f x t 4 Point-Plane Collision Detection Collision might occur if n x p nˆ x p Fall 2012 v Close enough and v nˆ 0 Head toward plane 5 Point-Plane Collision Response x n p vN vT frictionless v x : CR: Coefficient of Restitution 0 CR 1 Fall 2012 move x onto the plane v v T c R v N v v nˆ nˆ cR v nˆ nˆ v 1 cR v nˆ nˆ 6 Case Study: rocket Gravity + boosters Time-based animation Euler’s method Collision detection Fall 2012 7 Case: Sprite animation Fall 2012 8 Case Study: fly Fake physics for aircraft motion control Particle based dynamics Fall 2012 9 Particle System Data Structure Fall 2012 10 Force Objects Fall 2012 11 Case Study Particle system Damped spring “Mouse spring” manipulation Gravity + wall collisions Fall 2012 12 Spring with Geometry Shader Fall 2012 13 Verlet Integration (1, 2, 3) Numerical method especially for Loup Verlet molecular dynamics simulations (loo vuhr-LEH) Offer greater stability than Euler integration Developed by French physicist Loup Verlet in 1967 Fall 2012 14 Derivation Taylor’s Expansion (1) (2) (1) + (2) : (1) – (2) : Fall 2012 15 Formula Euler Integration Verlet Integration Velocity is implicit Verlet Integration (damped version: e.g., air resistance in games) Fall 2012 f: Percentage loss of velocity due to friction 16 Implementation Starting… xt 0t xt0 vt0 t at 2 Iterating … xt 0t 2xt0 xt 0t at 2 x new 2 x xold at 2 xold x x xnew st ate variable: x Fall 2012 st atic variable: xold 17 Does not really work … Collision Response in Verlet Integration xold n xnew x p x’old v N vT v Find the mirror image of xold w.r.t. the obstacle Continue Verlet integration Reflection Matrix Q I 2uuT Qu I 2uuT u u u 2u u T u u 2u u I 2nnT xold p p xold Fall 2012 18 Regarding (basic) Verlet Collision From: here Fall 2012 19 Verlet & Velocity Verlet1 2 x(t h) x(t ) hx (t ) h x(t h) x(t ) hx(t ) h x ( t ) 2 2 x ( t ) 2 3 x ( t ) 6 O(h ) 3 x ( t ) 6 O(h 4 ) h h 4 x(t h) x(t h) 2 x(t ) h 2 x(t ) O(h 4 ) x(t h) 2 x(t ) x(t h) h 2 x(t ) O(h 4 ) v(t ) x(t h) x(t h) / 2h O(h 2 ) Basic Verlet Position error O(h4) Velocity error O(h2) Fall 2012 20 Verlet & Velocity Verlet2 x(t h) x(t ) hx(t ) h 2 x(t h) x(t ) hv(t ) h 2 x ( t ) 2 x ( t ) 2 O(h 3 ) O(h 3 ) Good for velocity limiting and collision response v(t h) v(t ) hf (t ) O(h 2 ) (from t to t+h) v(t ) v(t h) hf (t h) O(h 2 ) (from t+h to t) v(t h) v(t ) hf (t h) O(h 2 ) f (t ) f (t h) v(t h) v(t ) h O(h 2 ) 2 Fall 2012 Velocity Verlet Position error O(h3) Velocity error O(h2) 21 Implementation (Wiki) System Implementation State_x(3n), state_v (3n), state_a(3n) First, update state_x from (1) Derive state_a with (2) Update state_v with (3) Fall 2012 22 Comparison (free fall and rebound) Exact Fall 2012 Verlet Euler Velocity Verlet 23 Ragdoll Physics Ref: advanced character dynamics (url) Youtube videos Physics for Games Accuracy not really the primary concern Goals: Fall 2012 believability (stability), and speed of execution 25 Techniques used in Hitman Particle/stick configuration to represent human anatomy Verlet integration Simple constraint solver Square root approximation (next page) Collision engine for penetration depth Fall 2012 26 Square Root Approximation Newton’s method with initial guess r. Fall 2012 27 Handling Constraints (ref) Containment (boundary) Fall 2012 If out of bound, move the point to its projection on the boundary Concave boundary: more complicated 28 Particle-Distance Constraint From wikipedia Remark: x1new x1old delta* 0.5 * diff x2 new x2 old delta* 0.5 * diff x2 new x1new x2 old x1old delta* diff delta delta* diff Fall 2012 deltalen restlen 1 delta deltalen restlen x2old x1old restlen x2old x1old deltalen x2old x1old 29 Moving on … Connect sticks to form rigid bodies Connect rigid bodies to form articulated objects Joint limit: one-sided stick constraint x2 x1 l Or dot product constraints Fall 2012 30 Extension (ref) cloth, hair, rigid body, … Fall 2012 31 Physics Engines Box2D Bullet ODE Havok PhyX Fall 2012 In-depth look at Box2D Lite 32