cpsc 427 video game programming - university of british
TRANSCRIPT
Copyright: Alla Sheffer/Michiel van de Panne 1
CPSC 427Video Game Programming
AnimationAnimation
ยฉ Alla Sheffer/M. van de Panne
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โฆ
Copyright: Alla Sheffer/Michiel van de Panne 2
ยฉ Alla Sheffer/M. van de Panne
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
Copyright: Alla Sheffer/Michiel van de Panne 3
ยฉ Alla Sheffer/M. van de Panne
Particles: Newtonian Physics as First-Order ODE
โข Motion of one particle
Second-order ODE
๏ฟฝ๏ฟฝ ๐ ๐ ๐ฅ๐๐ก
First-order ODE๐๐๐ก
๏ฟฝ๏ฟฝ๏ฟฝ๏ฟฝ
๏ฟฝ๏ฟฝ๐ด๏ฟฝ๏ฟฝ/๐
โข Motion of many particles
๐๐๐ก
๐ฅ๐ฃ๐ฅ๐ฃโฎ
๐ฅ๐ฃ
๐ฃ
๐น /๐๐ฃ
๐น /๐โฎ
๐ฃ
๐น /๐
ยฉ Alla Sheffer/M. van de Panne
Basic Particle Simulation (first try)
Forces only ๐ ๐๐
๐ ๐ ๐๐ ๐ ๐๐
๐๐ ๐ ๐ ๐๐ ๐ ๐๐ /๐ ๐ ๐
๐๐ ๐ ๐ ๐๐ ๐ ๐๐ ๐ ๐ ๐
Copyright: Alla Sheffer/Michiel van de Panne 4
ยฉ Alla Sheffer/M. van de Panne
Basic Particle Simulation (first try)
Forces only ๐ญ ๐๐
๐ ๐ ๐๐ ๐ ๐๐
๐๐ ๐ ๐ ๐๐ ๐ ๐๐ /๐ ๐ ๐
๐๐ ๐ ๐ ๐๐ ๐ ๐๐ ๐ ๐ ๐
ยฉ Alla Sheffer/M. van de Panne
Basic Particle Forces
โข Gravity
๐น 0๐๐
โข Viscous damping
๐น ๐๐ฃ
โข Spring & dampers
๐น ๐๐ฅ ๐๐ฃ
Copyright: Alla Sheffer/Michiel van de Panne 5
ยฉ Alla Sheffer/M. van de Panne
Proxy Forces
โข Behavior forces: flocking birds, schooling fish, etc. [โBoidsโ, Craig Reynolds, SIGGRAPH 1987]
โข Fluids[โCurl Noise for Procedural Fluid Flowโ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 ๐๐
Copyright: Alla Sheffer/Michiel van de Panne 6
ยฉ Alla Sheffer/M. van de Panne
Newtonian Physics as First-Order ODE
โข Motion of one particle
Second-order ODE
๏ฟฝ๏ฟฝ ๐ ๐ ๐ฅ๐๐ก
First-order ODE๐๐๐ก
๏ฟฝ๏ฟฝ๏ฟฝ๏ฟฝ
๏ฟฝ๏ฟฝ๐ด๏ฟฝ๏ฟฝ/๐
โข Motion of many particles
๐๐๐ก
๐ฅ๐ฃ๐ฅ๐ฃโฎ
๐ฅ๐ฃ
๐ฃ
๐น /๐๐ฃ
๐น /๐โฎ
๐ฃ
๐น /๐
ยฉ Alla Sheffer/M. van de Panne
Ordinary Differential Equations
๐๐๐ก
๏ฟฝ๏ฟฝ ๐ก ๐ ๏ฟฝ๏ฟฝ ๐ก , ๐ก
Given that ๏ฟฝ๏ฟฝ ๏ฟฝ๏ฟฝ ๐ก
Compute ๏ฟฝ๏ฟฝ ๐ก for t ๐ก
โ๏ฟฝ๏ฟฝ ๐ก ๐ ๏ฟฝ๏ฟฝ ๐ก , ๐ก โ๐ก
โข Simulation:
โ path through state-space
โ driven by vector field
Copyright: Alla Sheffer/Michiel van de Panne 7
ยฉ Alla Sheffer/M. van de Panne
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
๐ ๐ฟ ๐ , ๐
โ๐ ๐ ๐ฟ ๐ , ๐
๐ฟ ๐
๐ฟ ๐ โ๐
Copyright: Alla Sheffer/Michiel van de Panne 8
ยฉ Alla Sheffer/M. van de Panne
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
Copyright: Alla Sheffer/Michiel van de Panne 9
ยฉ Alla Sheffer/M. van de Panne
Midpoint Method
1. ยฝ Euler step
2. evaluate fm at ๐ฟ๐
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 ๐ฟ๐
๐ ๐ฟ ๐ , ๐
Copyright: Alla Sheffer/Michiel van de Panne 10
ยฉ Alla Sheffer/M. van de Panne
Midpoint & Trapezoid Method
๐ป๐๐๐๐๐๐๐๐ ๐๐๐๐๐๐
โข Not exactly the same
โ But same order of accuracy
๐ด๐๐ ๐๐๐๐ ๐๐๐๐๐๐
๐ฟ ๐
ยฉ Alla Sheffer/M. van de Panne
Explicit Euler: Code
Copyright: Alla Sheffer/Michiel van de Panne 11
ยฉ Alla Sheffer/M. van de Panne
Midpoint Method: Code
๐๐
๐ฟ ๐
๐ฟ ๐ โ๐ ๐๐
ยฝ โ๐ ๐ ๐ฟ ๐ , ๐
๐ฟ๐ ๐ฟ ๐ ยฝ โ๐ฟ ๐
ยฉ Alla Sheffer/M. van de Panne
โข Use the derivative at the destination
โข Problem is we donโt know the destination yet
Implicit (Backward) Euler:
Backward Euler
Forward Euler๐๐ ๐ ๐๐ ๐ ๐๐
๐๐ ๐ ๐๐ ๐๐ ๐๐
๐
๐๐ ๐ ๐๐ ๐ ๐๐ ๐
๐๐ ๐ ๐๐ ๐๐ ๐๐ ๐
๐
Copyright: Alla Sheffer/Michiel van de Panne 12
ยฉ Alla Sheffer/M. van de Panne
โข Use the derivative at the destination
โข Problem is we donโt know the destination yet
Implicit (Backward) Euler: Solve a (linear) system
Backward Euler
Forward Euler๐๐ ๐ ๐๐ ๐ ๐๐
๐๐ ๐ ๐๐ ๐๐ ๐๐
๐
๐๐ ๐ ๐๐ ๐ ๐๐ ๐
๐๐ ๐ ๐๐ ๐๐ ๐๐ ๐
๐
ยฉ Alla Sheffer/M. van de Panne
Simulation Basics
Simulation loopโฆ
1. Equations of Motion
2. Numerical integration
3. Collision detection
4. Collision resolution
Copyright: Alla Sheffer/Michiel van de Panne 13
ยฉ Alla Sheffer/M. van de Panne
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
๐ ๐๐
๐โฅ ๐โฅ
๐ ๐
๐
Copyright: Alla Sheffer/Michiel van de Panne 14
ยฉ Alla Sheffer/M. van de Panne
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
โข Momentum is preserved
๐๐๐๐ ๐๐๐๐ ๐๐๐๐ ๐๐๐๐
โข Kinetic energy is preserved
ยฝ ๐๐๐๐๐
ยฝ ๐๐๐๐๐
ยฝ ๐๐๐๐๐
ยฝ ๐๐๐๐๐
๐๐
๐๐
๐๐
๐๐
๐๐ ๐๐๐๐
๐๐
Before After
Copyright: Alla Sheffer/Michiel van de Panne 15
ยฉ Alla Sheffer/M. van de Panne
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
โข Impulse directionreflected across tangent
โข Impulse magnitude proportional to mass of other particle
Particle-Particle Collisions (radius >0)
๐๐
๐๐
๐๐
๐๐
๐๐
๐๐๐๐ ๐
๐๐: ๐๐, ๐๐
๐๐: ๐๐, ๐๐
Copyright: Alla Sheffer/Michiel van de Panne 16
ยฉ Alla Sheffer/M. van de Panne
Particle-Particle Collisions (radius >0)
๐๐ ๐๐๐๐๐
๐๐ ๐๐
๐๐ ๐๐ ยท ๐๐ ๐๐
๐๐ ๐๐๐ ๐๐ ๐๐
๐๐ ๐๐๐๐๐
๐๐ ๐๐
๐๐ ๐๐ ยท ๐๐ ๐๐
๐๐ ๐๐๐ ๐๐ ๐๐
โข More formallyโฆ
ยฉ Alla Sheffer/M. van de Panne
Rigid Body Dynamics
โข From particles to rigid bodiesโฆ
Rigid body
๐๐๐๐๐
๐ ๐๐๐๐๐๐๐๐ ๐ ๐๐๐๐๐๐๐๐ ๐, ๐น ๐๐๐๐๐๐๐๐ ๐๐๐๐๐๐ ๐๐๐๐ ๐๐๐๐๐๐๐ ๐๐๐๐๐๐๐๐
โ๐๐ in 3D
Particle
๐๐๐๐๐๐ ๐๐๐๐๐๐๐๐๐ ๐๐๐๐๐๐๐๐
โ๐ in 2Dโ๐ in 3D
Copyright: Alla Sheffer/Michiel van de Panne 17
ยฉ Alla Sheffer/M. van de Panne
โข From particles to rigid bodiesโฆ
Newton-Euler equations of motion๐
๐๐
๐ฐ
๐๐๐๐๐๐๐๐๐๐๐๐
๐บ๐ญ
Inertia tensor ๐บ๐ ๐ ๐ฐ๐
Rigid Body Dynamics
Newtonโs equations of motion
๐บ๐ญ ๐๐
๐๐
๐
๐๐๐๐๐๐
๐บ๐ญ
๐ด๐ ๐บ๐ญ
ยฉ 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 & Witkinhttp://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 game physics tutorial https://www.toptal.com/game/video-game-physics-part-i-an-introduction-to-rigid-body-dynamics
โข Box2D javascript 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 slideshttps://www.scss.tcd.ie/Michael.Manzke/CS7057/cs7057-1516-09-CollisionResponse-mm.pdf
โข Interactive simulation of rigid body dynamics in computer graphics, CGF 2014http://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