Faking Fluid Simulation
Kapil Kapre | Tuesday, March 02, 2010

This is a little trick that I came across a few years ago. As the title suggests it provides for a quick-and-dirty way to fake fluid simulation or more specifically, faking the interface of a fluid as it reacts to various disturbances on its surface level. Its low computational overhead and trivial implementation, make for a very interesting algorithm. The core algorithm, is just a few lines of code.


for_all i,j from 0,0 to MAX_I,MAX_J
	V(i,j) += (U(i-1,j) + U(i+1,j) + U(i,j-1) + U(i,j+1))/4 - U(i,j)
	V(i,j) *= dampingFactor
for_all i,j from 0,0 to MAX_I,MAX_J
	U(i,j) += V(i,j)

The fluid surface is approximated as a height map and is represented by the height-function U(x,y,t) which gives us the height of the wave at time t and position x,y. Also, V(x,y,t) represents the wave velocity at position x,y and time t. We don't really need to use a 3d array, as the updates can be done in situ without storing copies of the values at other time intervals.

One other important thing to mention is the boundary conditions. For the waves to be able to reflect off the boundary rectangle - (0,0,MAX_I,MAXJ), the following conditions are employed:


U(-1,j) = U(0,j)
U(i,-1) = U(i, 0)
U(MAX_I,j) = U(MAX_I-1,j)
U(i,MAX_J) = U(i, MAX_J-1)

I don't really have any information on who developed this algorithm or where it originated, but looking at the algorithm, it intuitively suggests that the water level (and velocity popogation) at any particular point on the surface varies with the variations of the water level at surrounding points. It seems to be a form of 2D discretization of the standard wave equation ∂2u/∂t2 = c22u