Real Time Fluids in Games

Matthias Müller

• PhysX accelerator chip (PPU)
www.ageia.com

• PhysX SDK (PPU accelerated, free!)
– Rigid bodies + joints – Cloth simulation – Soft bodies – Fluid simulation (SPH) – Heightfield fluids to come
2

Outline
• Fluids in Games • Heightfield Fluids
• A very simple program • Physics background • Object interaction

• Particle Based Fluids
• Simple particle systems • Smoothed Particle Hydrodynamics (SPH)
3

Offline Fluid Simulation
• State of the art is impressive! • Google “Robert Bridson”, “Ron Fedkiw”, “James O’Brien”, …

Gas
4

Liquids

Offline Simulation Times • Typical grid size 2563 cells • Linear system with 16 million unknowns! • Level sets on even finer grids • Raytracing (reflection / refraction / caustics) • Photorealistic results • 10 seconds – 50 minutes per frame! 5 Game Requirements • CHEAP TO COMPUTE! – 40-60 fps of which fluid only gets a small fraction • Stable even in non-realistic settings – Game characters sometimes “walk” at 50 mph • Low memory consumption – Must run on consoles • Challenge: – Get as close as possible to offline results while meeting all these constraints! 6 .

puddles. active regions (sleeping) – Camera dependent level of detail (LOD) – Non-physical animations for specific effects 7 Solutions • Procedural Water – Unbounded surfaces.Reducing Computation Time • Reduce resolution (lazy ) – Simple (use same algorithms) – Results look blobby and coarse. lakes • Particle Systems – Splashing. from 3d to 2d) – Use different resolutions for physics and appearance – Simulate only in interesting.g. smoke 8 . spray. oceans • Heightfield Fluids – Ponds. details disappear • Invent new methods (do research ☺) – Reduce dimension (e.

[Yuksel07].Procedural Animation • Simulate the effect. superimpose sine waves • Difficult but not impossible – Fluid – scene interaction 9 Heightfield Fluids 10 . [Fournier86]. [Hinsinger02] • No limits to creativity – E.g. not the cause [Bridson07].

j-1] + u[i.j] = u[0.j] 12 .j] *= 0.y) → no breaking waves 11 Fluid Simulation „Hello World“ • A trivial algorithm with impressive results! Try this at home! • Initialize u[i.j] + u[i+1.j] + u[i.j] endloop • Clamp on boundary e.j] with some interesting function • Initialze v[i.j] v[i.j] +=(u[i-1.g.j] += v[i.99 u[i.Heightfield Fluids u u y x continuous i j discrete • Represent fluid surface as a 2D function u(x. u[-1.j]=0 loop v[i.y) • Pro: Reduction from 3D to 2D • Cons: One value per (x. def.j+1])/4 – u[i.

14 .Bars Demo 13 The Physics Behind it • We model the water surface as an elastic membrane with low stiffness [Jeffrey02] • Fairly good approximation • Better: Derive a more complex surface model from the Navier Stokes Equations [Thuerey07] • Most games use procedural water today • Membrane model is an improvement and often sufficient for games.

t) x x x+dx • Component of f = σΑ in u-direction: fu ≈ uxσΑ where ux is derivative of u w.The Physics Behind It • A one dimensional membrane is a string u Infinitesimal element Vibrating string A -f f u(x.r.σΑux|x → ρ utt = σ uxx 16 .t) x x x+dx • u(x. x • Newton’s 2nd law for an infinitesimal segment (ρΑdx)utt = σΑux|x+dx .t.t) displacement normal to x-axis at time t • Assuming small displacements and constant stress σ • Force acting normal to cross section A is f = σΑ 15 PDE for the 1D String u Infinitesimal element Vibrating string -f A f fu u(x.

• Thus.t) = a · f (x + ct) + b · f (x . c is the speed at which waves travel 17 The 2D Wave Equation • The wave equation generalizes to 2D as utt = c2 (uxx + uyy) utt = c2 ∇2u utt = c2 ∆u 18 . where c2= σ/ρ • Solution: u(x.The 1D Wave Equation • For the string: ρ utt = σ uxx • Standard form: utt = c2 uxx.ct) for any function f.

Discretization • Replace the 2nd order PDE by two first order PDEs ut = v vt = c2 (uxx + uyy) • Discretize in space and time (semi-implicit Euler. grid spacing h) vt+1[i.j] +∆tc2(u[i+1.j-1]-4u[i.j] = vt[i.j])/h2 ut+1[i.j] +∆t vt+1[i.j] • We are where we started! (correct scaling.j+1]+u[i.j] = ut[i.j]+u[i.j]+u[i-1. no damping) 19 Remarks on Heightfields • The simulation is only conditionally stable – Stability condition: ∆t < h/c • Boundary conditions needed Clamp: Reflection Periodic: Wrap around 20 . time step ∆t.

Object Interaction • Object → Water – Object pushes bars beneath it down – Add the removed water in the vicinity! • Water → Object – Each bar below the object applies force f = -∆u ρ h2 g to body at its location – ∆u is the height replaced by the body. ρ water density. g gravity 21 Fully Immersed Bodies • Body below water surface • Hole appears above the body • Non-physical • See story of divided sea 22 .

j] is not modified directly – ∆ r[i.j] = rt[i.j] currently replaced by solids • At each time step: – u[i.j]: – Each column stores the part r[i.Solution • New state variable r[i.j] is distributed as water u to the neighboring columns – In case of a negative difference water is removed 23 From Bars to Water 24 .j]-rt-1[i.j] of u[i.

Water Rendering • Reflection e n r cube map n • Refraction e Scene below surface rendered to texture • Caustics: Cheating .Animated texture 25 Breaking Waves • Heightfields cannot capture breaking waves • Identify steep wave fronts • Construct and track line along front • Emit patch of connected particles • Generate mesh with given thickness for rendering (plus particles for foam) • See [Thuerey07] 26 .

Breaking Waves 27 Particle Based Fluids 28 .

external forces. velocity. deleted when lifetime is exceeded 30 emitter . position. blood. lifetimes • Integrate d/dt xi = vi d/dt vi = fi/mi xi mi vi f i • Generated by emitters.Particle Based Fluids • Particle systems are simple and fast • Without particle-particle interaction – Spray. splashing • With particle-particle interaction – Small puddles. runnels – Small water accumulations 29 Simple Particle Systems • Particles store mass.

k) [Teschner03] 32 .k] into 1D array via hash function h(i.j.Particle-Particle Interaction h • No interaction → decoupled system → fast • For n particles O(n2) potential interactions! • To reduce to linear complexity O(n) define interaction cutoff distance h 31 Spatial Hashing h h • Fill particles into grid with spacing h • Only search potential neighbors in adjacent cells • Map cells [i.j.

. • How formulate Navier-Stokes Eqn. k2.g. on particles? • We need continuous fields. m. x j ) =  − m  xi − x j xi − x j  • k1.Lennard-Jones Interaction • For simple fluid-like behavior: Repulsion Equilibrium Attraction  k1 k2  f (x i . e. vn sampled on particles • Basic idea: – Particles induce smooth local fields – Global field is sum of local fields 34 . v(x) • Only have v1.. v2. n control parameters 33 n  x −x  i j ⋅   xi − x j  Solving Navier-Stokes Eqn.

SPH • Smoothed Particle Hydrodynamics • Invented for the simulation of stars [Monaghan92] • Often used for real-time fluids in CG [Müller03] • Use scalar kernel function W(r) – Wi(x) = W(|x-xi|) – Normalized: ∫ ∫ ∫ Wi(x) dx = 1 x r xi • Example [Müller03] W ( r . h) = 315 (h 2 − r 2 ) 3 9 64π h 0≤r ≤h 35 Density Computation • Global density field ρ (x) = ∑ m W (x − x j j j ) • Density of each particle ρ i = ρ (x i ) • Mass conservation guaranteed ∫ ρ ( x ) d x = ∑ (m ∫ W ( x − x j j j )dx = ∑ m j j ) 36 .

An → compute A(x) As ( x ) = ∑ j mj ρj A jW (x − x j ) • Gradient of smoothed attribute ∇ As ( x ) = ∑ j mj ρj A j∇ W (x − x j ) 37 Equation of Motion ρ  ∂v  + v ⋅ ∇ v  = ρg − ∇ p + µ∇ 2 v  ∂t  • Because particles follow the fluid we have: D v  ∂v  dv i = ai = + v ⋅∇v  = Dt dt  ∂t  • The acceleration ai of particle i is. • Given A1. thus ai = ρi fi fi is body force evaluated at xi 38 .Smoothing Attributes • Smoothing of attribute A..

g.Pressure • The pressure term yields fi pressure = −∇ p ( x i ) = − ∑ j mj ρj p j∇ W (x i − x j ) • Symmetrize (SPH problem: actio ≠ reactio) fi pressure = −∑ j m j pi + p j ρj 2 ∇ W (x i − x j ) • Pressure pi = k ρi with k gas constant (stiffness) • Other state laws possible [Becker07] 39 Remaining Forces • External force. gravity: fi external = ρi g • Viscosity (simmetrized) fi viscosity = µ∑ m j v j − vi ρj ∇ 2W ( x i − x j ) 40 . e.

far from the camera • Screen Space Meshes [Müller07] 42 . blurry surface – Marching cubes for liquids • Combine particles and heightfields [O’Brien95.Remarks on SPH • Compressibility – Pressure force reacts to density variation (bouncy) – Predict densities. solve for incompressibility [Premoze03] • Parameters hard to tune • Rendering – Sprites for smoke. Thuerey07] 41 Surface Tracking • Two main bottlenecks – Not the simulation! – Collision detection – Surface tracking for liquids • Marching cubes – Often used to in offline simulations – Generates detailed geometry in non visible places.

Screen Space Mesh • Regular 2D mesh • Vertices projected back to world space • Constructed in screen space • Frozen mesh. side view • Modified marching squares 43 SPH Demo Scenes 44 .

. Weakly compressible SPH for free surface flows.Acknowledgements • AGEIA. Curl noise for procedural fluid flow. Siggraph 07 A. Applied Partial Differential Equations. Reeves. Jeffrey. Stephan Duthaler. Smoothed particle hydrodynamics. co-workers (fluid simulation) – Philipp Hatt. Simon Schirm. SCA 07 R. Nils Thuerey. Academic Press. Bridson et al. 1992. Annual Review of Astronomy and Astrophysics. 46 . Bruno Heidelberger. pages 75–84 D. Monaghan.. A simple model of ocean waves. Becker and M. Interactive Animation of Ocean Waves. Isha Geigenfeind. Richard Tonge • Robert for the invitation 45 References 1/2 [Becker07] [Bridson07] [Fournier86] [Hinsinger02] [Jeffrey02] [Monaghan92] M. 30:543–574. T. J. Teschner. Fournier and W. In Proceedings of SCA 02 A. SIGGRAPH 86. ISBN 0-12-382252-1 J. Hinsinger et al.

Particle based simulation of fluids. Müller et al. In Computer Animation 95. Hodgins. Müller et al. pages 198–205 S. Siggraph 07 47 Slides & demos available soon at www. Particle-Based Fluid Simulation for Interactive Applications. Screen Space Meshes. pages 401-410 M. pages 154-159..MatthiasMueller.ubc.. O’Brien and J.info www. VMV 03 N. M.. Dynamic simulation of splashing fluids. Eurographics 03. SCA 07.. J.cs. Thuerey et al.. Teschner et al. SCA 03. Optimized Spatial Hashing for Collision Detection of Deformable Objects. Wave Particles.References 2/2 [Müller07] [Müller03] [O’Brien95] [Premoze03] [Teschner03] [Thuerey07] [Yuksel07] M..ca/~rbridson/fluidsimulation/ Thank your for your attention! Questions? . Real-time Breaking Waves for Shallow Water Simulations Pacfific Graphics 07 Cem Yuksel et al. Premoze et al.