# Fluid dynamics

I’ve collected a bunch of stuff relating to the fluid dynamics work I did with Hardcore Computer Simulator, and from previously when I was working on a water game prototype. The end of the story was that I didn’t implement a new version of any of this, but the links are cool and I reckon that it’s only a matter of time until there are games with proper fluid dynamics that aren’t just a bunch of particles or the sand algorithm. I’ve collected them all here both for future reference and for anyone else who’s interested in this topic.

Baby steps

The starting point for all this is Jos Stam’s work. A good paper to read is his 2003 paper, Real-Time Fluid Dynamics For Games, which will show you how to create an incompressible single fluid volume, which is useful if you want to show smoke, for instance. This is basically what I used in HCS to simulate the heat flow. A collection of Stam’s published work is held at his page at the University Of Toronto. This is the same stuff that I believe was used in the now-defunct Plasma Pong.

How it works is that you have a field of velocity and a field of density. You then make the density follow the velocity field, and do the same for the velocity. You then conserve mass over the entire system. This paper will give you a few further insights into the method. It was extremely helpful for me to read this paper.

The major setback with this method for the Coder About Town is conserving the mass of the system. This is done, apparently, by using a Hodge decomposition, which apparently states that a velocity field is the sum of a mass-conserving field and a gradient field. Finding the gradient field involves solving a Poisson equation, which is a linear system, which Stam achieves by using something called a Gauss-Siedel relaxation. You may have guessed that I didn’t understand a single concept or process in that last paragraph.

Here is a java implementation of the Stam work with some additional work added for vorticity confinement. This is the version I used for HCS; Java is very close to C# as a language so it was easy to port this. The code in the Stam paper was heavily optimised and hard to get to grips with.

Here is another implementation. Not that much info.

There is an interactive 2D version of a similar solver by Andrew Selle, who has gone on to do some really excellent work at Stanford.

Moving forward

The main problem with the above work is that it deals with a volume containing a single fluid. What you really want for a fluid simulation is at least two fluids, let’s say water and air. There are plenty of solutions for this problem but most of them are not suitable for real-time interaction. One that I did find was a deliberately-compromised version intended for educational purposes. The idea is that you run particles over the fluid simulation and ensure that boundary conditions are correct between cells containing fluids, solid cells and empty cells. Correcting the boundary conditions is left as an exercise for the student. It is not trivial.

This guy used similar methods to get his fluid interacting with a soft body. He correctly identifies the major pain in solving the boundary condition calculations.

I was talking to a friend at Sony about this and he told me I should take a look at smoothed particle hydrodynamics. I haven’t gone through the paper, so I don’t know what it’s all about yet.

The current state-of-the-art work at nVidia was presented recently at GDC 2008. Here’s the PDF, there’s some nice stuff int here even if it is just presentation slides.

UPDATE: Mick West has been kind enough to upload some code and accompanying Game Developer articles for a Stam solver he wrote. Very nice!

UPDATE: Found a pretty hot SPH demonstration with source by Takashi Amada. This runs 2000 particles and does surface calculation for the lot. Super!