64
Physics in Games Matthias Müller www.MatthiasMueller.info

Physics in Games - csee.umbc.edu · Particle Physics • Simplest and very popular form of physics effect –droplets, smoke, fire, debris [Reeves, 1983] ... rigid body modes

  • Upload
    lekien

  • View
    214

  • Download
    0

Embed Size (px)

Citation preview

Page 1: Physics in Games - csee.umbc.edu · Particle Physics • Simplest and very popular form of physics effect –droplets, smoke, fire, debris [Reeves, 1983] ... rigid body modes

Physics in Games Matthias Müller

www.MatthiasMueller.info

Page 2: Physics in Games - csee.umbc.edu · Particle Physics • Simplest and very popular form of physics effect –droplets, smoke, fire, debris [Reeves, 1983] ... rigid body modes
Page 3: Physics in Games - csee.umbc.edu · Particle Physics • Simplest and very popular form of physics effect –droplets, smoke, fire, debris [Reeves, 1983] ... rigid body modes

Outline • Comparison

– Physical simulations in engineering

– Offline physics in graphics (mostly movies)

– Interactive physics

– Real time physics in games

• Position Based Dynamics – Algorithm

– Examples: cloth, rigid bodies, fluids, unified solver

• Q & A

Page 4: Physics in Games - csee.umbc.edu · Particle Physics • Simplest and very popular form of physics effect –droplets, smoke, fire, debris [Reeves, 1983] ... rigid body modes

Simulations in Engineering

• Extreme conditions, spatial scale, time scale

• Accuracy most important factor

• Low accuracy: Useless result!

• One central gigantic computer

• Complement real experiments

Page 5: Physics in Games - csee.umbc.edu · Particle Physics • Simplest and very popular form of physics effect –droplets, smoke, fire, debris [Reeves, 1983] ... rigid body modes

Evolution of Compute Power

Zuse’s Z1 (1938) 0.2 ops

Titan (currently number 2) using 18,000 nvidia GPUs

27,000,000,000,000,000 flops!

Page 6: Physics in Games - csee.umbc.edu · Particle Physics • Simplest and very popular form of physics effect –droplets, smoke, fire, debris [Reeves, 1983] ... rigid body modes

Simulation of Hurricane Sandy

• National Center for Supercomputing Applications • 9120 x 9216 x 48 cells (500 m) • 13,680 nodes and 437,760 cores on Titan • Sustained rate of 285 teraflops

Page 7: Physics in Games - csee.umbc.edu · Particle Physics • Simplest and very popular form of physics effect –droplets, smoke, fire, debris [Reeves, 1983] ... rigid body modes

Physics in Graphics

Page 8: Physics in Games - csee.umbc.edu · Particle Physics • Simplest and very popular form of physics effect –droplets, smoke, fire, debris [Reeves, 1983] ... rigid body modes

Re-inventing the Wheel?

• New goals require new methods!

• Goals of physics in graphics – Imitation of physical phenomena / effects

– Plausible behavior (cheating possible)

– Trade accuracy for speed, stability, simplicity

– Control (by director / game developer)

• Since late 80’s [Terzopoulos et al. 87, 88]

• Rediscoveries – Semi-Lagrangian advection, co-rotational FEM,

X introduced Y to graphics (SPH, MPM, FLIP, …)

Page 9: Physics in Games - csee.umbc.edu · Particle Physics • Simplest and very popular form of physics effect –droplets, smoke, fire, debris [Reeves, 1983] ... rigid body modes

Offline Methods

• >> 1 sec of computation for 1 sec of simulation allows: – High resolution (fluid grid, FEM mesh, time steps)

– Re-runs and adaptive time steps

– Time consuming shading

[Emmerich, Movie 2012]

• Main application: Movies

Page 10: Physics in Games - csee.umbc.edu · Particle Physics • Simplest and very popular form of physics effect –droplets, smoke, fire, debris [Reeves, 1983] ... rigid body modes

Interactive Physics

• Between offline and game physics

• Virtual surgery, virtual reality, demos

• All available compute power

• > 15 fps

• No adaptive time steps

• Robust

• No re-runs

• Unforeseeable situations

Page 11: Physics in Games - csee.umbc.edu · Particle Physics • Simplest and very popular form of physics effect –droplets, smoke, fire, debris [Reeves, 1983] ... rigid body modes

Water Demo (GTC 2012)

• First time real-time Eulerian water sim + ray tracing

2 x GTX 680

Multi-grid [Chentanez et al., 2011]

OptiX

Page 12: Physics in Games - csee.umbc.edu · Particle Physics • Simplest and very popular form of physics effect –droplets, smoke, fire, debris [Reeves, 1983] ... rigid body modes

Dragon • Eulerian fluid simulation + combustion model + volumetric rendering

Page 13: Physics in Games - csee.umbc.edu · Particle Physics • Simplest and very popular form of physics effect –droplets, smoke, fire, debris [Reeves, 1983] ... rigid body modes

Physics in Games

Page 14: Physics in Games - csee.umbc.edu · Particle Physics • Simplest and very popular form of physics effect –droplets, smoke, fire, debris [Reeves, 1983] ... rigid body modes

Game Requirements • Cheap to compute

– 30-60 fps of which physics only gets a small fraction

• Low memory consumption

– Consoles, fit into graphics (local) memory

• Stable in extreme settings

– 180 degree turns in one time step

• High level of control

• Challenge

• Meet all these constraints

• Get to offline results as close as possible

Page 15: Physics in Games - csee.umbc.edu · Particle Physics • Simplest and very popular form of physics effect –droplets, smoke, fire, debris [Reeves, 1983] ... rigid body modes

Speedup Tricks • Reduce simulation resolution

– Simple: Use same algorithms

– Interesting details disappear

• Reduce dimension (e.g. 3d → 2d)

• Use different resolution for physics and appearance

• Simulate only in active regions (sleeping)

• Camera dependent level of detail (LOD)

• Invent new simulation methods!

• Use nvidia GPUs and CUDA!

Page 16: Physics in Games - csee.umbc.edu · Particle Physics • Simplest and very popular form of physics effect –droplets, smoke, fire, debris [Reeves, 1983] ... rigid body modes

Game Physics Methods

Page 17: Physics in Games - csee.umbc.edu · Particle Physics • Simplest and very popular form of physics effect –droplets, smoke, fire, debris [Reeves, 1983] ... rigid body modes

Animation

• Pros: – Can be and still is used for almost everything

(3d movie playback)

• Cons – Time consuming manual work

– Hard to handle complex phenomena

– Repeating behavior

– Full control

– What artists are used to do

Page 18: Physics in Games - csee.umbc.edu · Particle Physics • Simplest and very popular form of physics effect –droplets, smoke, fire, debris [Reeves, 1983] ... rigid body modes

Particle Physics

• Simplest and very popular form of physics effect – droplets, smoke, fire, debris [Reeves, 1983]

• Effects physics vs. game play physics – does not influence game play, no path blocking

• Most expensive part: – collision detection with large environments

– particle-particle interaction (often not needed)

– Advection by incompressible velocity field (fluid solver)

Page 19: Physics in Games - csee.umbc.edu · Particle Physics • Simplest and very popular form of physics effect –droplets, smoke, fire, debris [Reeves, 1983] ... rigid body modes
Page 20: Physics in Games - csee.umbc.edu · Particle Physics • Simplest and very popular form of physics effect –droplets, smoke, fire, debris [Reeves, 1983] ... rigid body modes

Rigid Bodies

• Rarely in-house

• Middleware popular (PhysX, havok, bullet)

• Game physics engines = rigid body engines

• Challenges • Stability (stacking)

• Speed (solver and collision detection)

• Continuous collision detection (fast moving objects)

inthevif with blender & bullet

Page 21: Physics in Games - csee.umbc.edu · Particle Physics • Simplest and very popular form of physics effect –droplets, smoke, fire, debris [Reeves, 1983] ... rigid body modes

Destruction • Traditional: static fracture

• Artists pre-fracture models

• Models are replaced by parts when collision forces exceed a threshold

• Pro: • High level of control

• Cons: • Tedious manual work

• Independent of impact location

Page 22: Physics in Games - csee.umbc.edu · Particle Physics • Simplest and very popular form of physics effect –droplets, smoke, fire, debris [Reeves, 1983] ... rigid body modes

PhysX Destruction Tool

Page 23: Physics in Games - csee.umbc.edu · Particle Physics • Simplest and very popular form of physics effect –droplets, smoke, fire, debris [Reeves, 1983] ... rigid body modes

Pattern Based Fracture [Müller et al., 2013]

• Pre-designed fracture pattern

Page 24: Physics in Games - csee.umbc.edu · Particle Physics • Simplest and very popular form of physics effect –droplets, smoke, fire, debris [Reeves, 1983] ... rigid body modes

Pattern Based Fracture

• Pre-designed fracture pattern

[Müller et al., 2013]

• Align pattern with impact location at runtime

• Use pattern as stencil

Page 25: Physics in Games - csee.umbc.edu · Particle Physics • Simplest and very popular form of physics effect –droplets, smoke, fire, debris [Reeves, 1983] ... rigid body modes
Page 26: Physics in Games - csee.umbc.edu · Particle Physics • Simplest and very popular form of physics effect –droplets, smoke, fire, debris [Reeves, 1983] ... rigid body modes

Arena Destruction (SG 2013 real time live)

• 500k faces at start

• GPU1: rigid body simulation

• GPU2: smoke, rendering

• CPU: dynamic fracturing

Page 27: Physics in Games - csee.umbc.edu · Particle Physics • Simplest and very popular form of physics effect –droplets, smoke, fire, debris [Reeves, 1983] ... rigid body modes
Page 28: Physics in Games - csee.umbc.edu · Particle Physics • Simplest and very popular form of physics effect –droplets, smoke, fire, debris [Reeves, 1983] ... rigid body modes

Deformable Objects

• 1d: Ropes, hair

• 2d: Cloth, clothing

• 3d: Fat guys, tires

Page 29: Physics in Games - csee.umbc.edu · Particle Physics • Simplest and very popular form of physics effect –droplets, smoke, fire, debris [Reeves, 1983] ... rigid body modes

Existing Methods

• Force based

• Mass-Spring Systems / FEM

• Explicit integration unstable • Implicit integration

– Expensive

– Large time steps for real time simulation needed

– Numerical damping

Page 30: Physics in Games - csee.umbc.edu · Particle Physics • Simplest and very popular form of physics effect –droplets, smoke, fire, debris [Reeves, 1983] ... rigid body modes

Position Based Dynamics [Müller et al., 2006]

Page 31: Physics in Games - csee.umbc.edu · Particle Physics • Simplest and very popular form of physics effect –droplets, smoke, fire, debris [Reeves, 1983] ... rigid body modes

Position Based Dynamics [Müller et al., 2006]

[google scholar]

Page 32: Physics in Games - csee.umbc.edu · Particle Physics • Simplest and very popular form of physics effect –droplets, smoke, fire, debris [Reeves, 1983] ... rigid body modes

Force Based Update

• Reaction lag

• Small spring stiffness → squashy system

• Large spring stiffness → stiff system, overshooting

penetration causes forces

velocities change positions

forces change velocities

Page 33: Physics in Games - csee.umbc.edu · Particle Physics • Simplest and very popular form of physics effect –droplets, smoke, fire, debris [Reeves, 1983] ... rigid body modes

Position Based Update

• Controlled position change

• Only as much as needed → no overshooting

• Velocity update needed to get 2nd order system!

penetration detection only

move objects so that they do not penetrate

update velocities!

Page 34: Physics in Games - csee.umbc.edu · Particle Physics • Simplest and very popular form of physics effect –droplets, smoke, fire, debris [Reeves, 1983] ... rigid body modes

Position Based Integration

init x0, v0 x𝑛, v𝑛, p, u ∈ ℝ3𝑁

loop

p ← x𝑛 + ∆𝑡 ∙ v𝑛 prediction

x𝑛+1 ← modify p position correction

u ← (x𝑛+1 − x𝑛)/∆𝑡 velocity update

v𝑛+1 ← modify u velocity correction

end loop

Page 35: Physics in Games - csee.umbc.edu · Particle Physics • Simplest and very popular form of physics effect –droplets, smoke, fire, debris [Reeves, 1983] ... rigid body modes

Position Correction

• Example: Particle on circle

prediction

correction new velocity

Page 36: Physics in Games - csee.umbc.edu · Particle Physics • Simplest and very popular form of physics effect –droplets, smoke, fire, debris [Reeves, 1983] ... rigid body modes

Velocity Correction

• External forces: v𝑛+1 = u + ∆𝑡 g𝑚

• Internal damping

• Friction

• Restitution

collision correction

prediction

restitution

friction

corrected velocity

Page 37: Physics in Games - csee.umbc.edu · Particle Physics • Simplest and very popular form of physics effect –droplets, smoke, fire, debris [Reeves, 1983] ... rigid body modes

∆x1 = −𝑤1

𝑤1 + 𝑤2x1 − x2 − 𝑙0

x1 − x2

x1 − x2

Distance Constraint

• Conservation of momentum

• Stiffness: scale corrections by 𝑘 ∈ 0,1 – Easy to tune

– Effect dependent on time step size and iteration count

– Often constant in games

∆x2 = +𝑤2

𝑤1 + 𝑤2x1 − x2 − 𝑙0

x1 − x2

x1 − x2

𝑤𝑖 =1

𝑚𝑖

𝑚1

𝑚2 ∆x1

∆x2

𝑙0

Page 38: Physics in Games - csee.umbc.edu · Particle Physics • Simplest and very popular form of physics effect –droplets, smoke, fire, debris [Reeves, 1983] ... rigid body modes

General Internal Constraint • Define constraint via scalar function:

𝐶𝑑𝑖𝑠𝑡 x1, x2 = x1 − x2 − 𝑙0

𝐶𝑣𝑜𝑙𝑢𝑚𝑒 x1, x2, x3, x4 = x2 − x1 × x3 − x1 ∙ x4 − x1 − 6𝑣0

• Find configuration for which 𝐶 = 0

• Search along 𝛻𝐶 𝐶 = 0

rigid body modes

𝛻𝐶

Page 39: Physics in Games - csee.umbc.edu · Particle Physics • Simplest and very popular form of physics effect –droplets, smoke, fire, debris [Reeves, 1983] ... rigid body modes

Constraint Projection

• Linearization (equal for distance constraint)

𝐶 x + ∆x ≈ 𝐶 x + 𝛻𝐶 x 𝑇∆x = 0

∆x = 𝜆 𝛻𝐶 x

λ = −𝐶 x

𝛻𝐶 x 𝑇𝛻𝐶 x λ = −

𝐶 x

𝛻𝐶 x 𝑇M−1 𝛻𝐶 x

∆x = 𝜆 M−1𝛻𝐶 x

M = 𝑑𝑖𝑎𝑔 𝑚1, 𝑚2, . . , 𝑚𝑛

• Correction vectors

𝐶 x + ∆x = 0

Page 40: Physics in Games - csee.umbc.edu · Particle Physics • Simplest and very popular form of physics effect –droplets, smoke, fire, debris [Reeves, 1983] ... rigid body modes

Constraint Solver • Gauss-Seidel

– Iterate through all constraints and apply projection – Perform multiple iterations – Simple to implement – Atomic operations required for parallelization

• Modified Jacobi – Process all constraints in parallel – Accumulate corrections – After each iteration, average corrections [Bridson et al., 2002]

• Both known for slow convergence

Page 41: Physics in Games - csee.umbc.edu · Particle Physics • Simplest and very popular form of physics effect –droplets, smoke, fire, debris [Reeves, 1983] ... rigid body modes

Global Solver

• Constraint vector

λ = −𝐶 x

𝛻𝐶 x 𝑇M−1 𝛻𝐶 x ∆x = M−1𝛻𝐶 x 𝜆

C x =𝐶1 x

⋯𝐶𝑀 x

𝛻C x =𝛻𝐶1 x 𝑇

⋯𝛻𝐶𝑀 x 𝑇

𝛻𝐶 x M−1𝛻C x 𝑇 λ = −C x ∆x = M−1𝛻C x 𝑇λ

λ =𝜆1

⋯𝜆𝑀

[Goldenthal et al., 2007]

Page 42: Physics in Games - csee.umbc.edu · Particle Physics • Simplest and very popular form of physics effect –droplets, smoke, fire, debris [Reeves, 1983] ... rigid body modes

Global vs. Gauss-Seidel • Gradients fixed

• Linear solution ≠ true solution

• Multiple Newton steps necessary

• Current gradients at each constraint projection

• Solver converges to the true solution

𝛻𝐶2 𝛻𝐶1

𝑙2 𝑙1

𝑙2 𝑙1

Page 43: Physics in Games - csee.umbc.edu · Particle Physics • Simplest and very popular form of physics effect –droplets, smoke, fire, debris [Reeves, 1983] ... rigid body modes

Other Speedup Tricks

• Use as smoother in a multi-grid method

• Long range distance constraints (LRA)

• Shape matching

• Hierarchy of meshes

Page 44: Physics in Games - csee.umbc.edu · Particle Physics • Simplest and very popular form of physics effect –droplets, smoke, fire, debris [Reeves, 1983] ... rigid body modes

Amazing Gauss-Seidel!

• Can handle unilateral (inequality) constraints (LCPs, QPs)! – Fluids: separating boundary conditions [Chentanez at al., 2012]

– Rigid bodies: LCP solver [Tonge et al., 2012]

– Deformable objects: Long range attachments [Kim et al., 2012]

• Works on non-linear problem directly

• Handles under and over-constrained problems

• GS + PBD: garbage in, simulation out (almost )

• Fine grained interleaved solver trivial

• Easy to implement and parallelize

Page 45: Physics in Games - csee.umbc.edu · Particle Physics • Simplest and very popular form of physics effect –droplets, smoke, fire, debris [Reeves, 1983] ... rigid body modes

Analysis of PBD

Page 46: Physics in Games - csee.umbc.edu · Particle Physics • Simplest and very popular form of physics effect –droplets, smoke, fire, debris [Reeves, 1983] ... rigid body modes

Correction = Acceleration

• Predicted position

p = x𝑛 + ∆𝑡v𝑛 = x𝑛 + ∆𝑡x𝑛 − x𝑛−1

∆𝑡= 2x𝑛 − x𝑛−1

x𝑛+1 = p + ∆x

• Projection

∆x = x𝑛+1 − 2x𝑛 + x𝑛−1

Page 47: Physics in Games - csee.umbc.edu · Particle Physics • Simplest and very popular form of physics effect –droplets, smoke, fire, debris [Reeves, 1983] ... rigid body modes

Implicit Euler

min1

2∆x𝑇M∆x + ∆𝑡2𝐸 x𝑛+1

M∆x = ∆𝑡2 f x𝑛+1

M𝐱𝑛+1 − 2𝐱𝑛 + 𝐱𝑛−1

∆𝑡2 = f x𝑛+1

Formulation as an optimization problem for ∆x:

inertia term energy term

Page 48: Physics in Games - csee.umbc.edu · Particle Physics • Simplest and very popular form of physics effect –droplets, smoke, fire, debris [Reeves, 1983] ... rigid body modes

Stiffness Infinity

min1

2∆x𝑇M∆x + ∆𝑡2

1

2𝑘𝐶2 x𝑛+1

min1

2∆x𝑇M∆x subject to 𝐶 x𝑛+1 = 0

∆x = 𝜆 M−1𝛻𝐶 x𝑛+1

• 𝐶 x𝑛+1 = 0

• M∆x = 𝜆𝛻𝐶 x𝑛+1

PBD

Now let 𝑘 → ∞

// 𝐸 x =1

2𝑘𝐶2 x

Page 49: Physics in Games - csee.umbc.edu · Particle Physics • Simplest and very popular form of physics effect –droplets, smoke, fire, debris [Reeves, 1983] ... rigid body modes

Two Interpretations

𝐶 = 0

rigid body modes

𝛻𝐶 = 𝜆𝛻 ∆x2

Page 50: Physics in Games - csee.umbc.edu · Particle Physics • Simplest and very popular form of physics effect –droplets, smoke, fire, debris [Reeves, 1983] ... rigid body modes

Constraint Solver

min1

2∆x𝑇M∆x subject to 𝐶𝑖 x𝑛+1 = 0, 𝑖 ∈ 1, . . , 𝑚

• PBD solves a non-linear optimization problem

by solving a sequence of QPs:

min1

2∆x𝑇M∆x subject to 𝐶𝑖 x𝑛+1 = 0

Page 51: Physics in Games - csee.umbc.edu · Particle Physics • Simplest and very popular form of physics effect –droplets, smoke, fire, debris [Reeves, 1983] ... rigid body modes

Clothing Demo Nurien

Page 52: Physics in Games - csee.umbc.edu · Particle Physics • Simplest and very popular form of physics effect –droplets, smoke, fire, debris [Reeves, 1983] ... rigid body modes

Cloth

• Slow error propagation stretchy cloth

• Low resolution: no detailed wrinkles

• Solutions

– Use hierarchy of meshes (complicated)

– Has been an open problem for us

– Found an embarrassingly simple solution

Page 53: Physics in Games - csee.umbc.edu · Particle Physics • Simplest and very popular form of physics effect –droplets, smoke, fire, debris [Reeves, 1983] ... rigid body modes

Long Range Attachments (LRA) • Upper distance constraint to closest attachment point

• Unilateral: project only if distance too big

[Kim et al., 2012], 90k particles

Page 54: Physics in Games - csee.umbc.edu · Particle Physics • Simplest and very popular form of physics effect –droplets, smoke, fire, debris [Reeves, 1983] ... rigid body modes
Page 55: Physics in Games - csee.umbc.edu · Particle Physics • Simplest and very popular form of physics effect –droplets, smoke, fire, debris [Reeves, 1983] ... rigid body modes

Challenge

• Similar idea for compression?

• Long range distance constraint to the ground?

Page 56: Physics in Games - csee.umbc.edu · Particle Physics • Simplest and very popular form of physics effect –droplets, smoke, fire, debris [Reeves, 1983] ... rigid body modes

Rigid Objects

• Global correction, no propagation needed

• No mesh needed!

• Optimally match un-deformed with deformed shape

• Only allow translation and rotation

p𝑖

∆x𝑖

Page 57: Physics in Games - csee.umbc.edu · Particle Physics • Simplest and very popular form of physics effect –droplets, smoke, fire, debris [Reeves, 1983] ... rigid body modes

Position Based Fluids

• Move particles in local neighborhood such that density = rest density

𝐶 x1, . . , x𝑛 = 𝜌𝑆𝑃𝐻 x1, . . , x𝑛 − 𝜌0

• Density constraint

• Particle based

• Pair-wise lower distance constraints granular behavior

[Macklin et al. 2013]

Page 58: Physics in Games - csee.umbc.edu · Particle Physics • Simplest and very popular form of physics effect –droplets, smoke, fire, debris [Reeves, 1983] ... rigid body modes
Page 59: Physics in Games - csee.umbc.edu · Particle Physics • Simplest and very popular form of physics effect –droplets, smoke, fire, debris [Reeves, 1983] ... rigid body modes

Mesh Independent Deformations

𝐶 x1, . . , x3 = 𝐆𝑖𝑗 x1, . . , x3

• For each triangle:

[Müller et al, 2014]

𝐆 = 𝐅T𝐅 − 𝐈

Page 60: Physics in Games - csee.umbc.edu · Particle Physics • Simplest and very popular form of physics effect –droplets, smoke, fire, debris [Reeves, 1983] ... rigid body modes

FEM

𝐶 x1, . . , x4 = 𝐸𝐹𝐸𝑀 x1, . . , x4

[Bender et al, 2014]

• For each tetrahedron:

Page 61: Physics in Games - csee.umbc.edu · Particle Physics • Simplest and very popular form of physics effect –droplets, smoke, fire, debris [Reeves, 1983] ... rigid body modes

Unified Solver [Macklin et al., 2014]

• Plus – Static friction – Stiff stacks via mass modifications – Two-way fluid – solid coupling

• Putting it all together

Page 62: Physics in Games - csee.umbc.edu · Particle Physics • Simplest and very popular form of physics effect –droplets, smoke, fire, debris [Reeves, 1983] ... rigid body modes
Page 63: Physics in Games - csee.umbc.edu · Particle Physics • Simplest and very popular form of physics effect –droplets, smoke, fire, debris [Reeves, 1983] ... rigid body modes

Acknowledgements • PhysX Research Group

• PhysX Group Nuttapong Chentanez

Tae-Yong Kim

Miles Macklin

Page 64: Physics in Games - csee.umbc.edu · Particle Physics • Simplest and very popular form of physics effect –droplets, smoke, fire, debris [Reeves, 1983] ... rigid body modes

Questions?

Thanks!