cpsc 427 video game programming - university of british

17
Copyright: Alla Sheffer/Michiel van de Panne 1 CPSC 427 Video Game Programming Animation Animation ยฉ 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โ€ฆ

Upload: others

Post on 17-Oct-2021

2 views

Category:

Documents


0 download

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