Following up his popular recent article, Neversoft co-founder Mick West explains the technical details - including source code - of creating dynamic fluid systems such as smoke for video games.
West suggests that programmers think of pressure at a particular node as being the difference in density between a cell and its neighbors -- the water pressure will be the same throughout the density field since it is, for the most part, incompressible:
“If we think of a node as having a series of inputs and outputs during the advection process, then in an incompressible field, the sum of input is equal to the sum of outputs (see Figure 1A). When we move the water along its incompressible velocity field, the density at each node remains constant, and hence the pressure remains constant.
On the other hand, if the velocity field happens to be structured in such a way that for some cells more is going into them than is coming out, the velocity field is compressible (see Figure 1B). When the density of the fluid is advected across a compressible velocity field, the density in individual cells will increase or decrease.
If we simply keep advecting the density, all the matter will eventually be compressed into the cells of the velocity field that have a net gain of input over output. If we were not performing accounting in our advection step (as explained last month), then there would be a net loss in density (the field is not mass conserving).”
With his provided code for applying pressure, West notes that the logic is slightly unintuitive considering that physics programmers are used to Newtonian principle that every action has an equal and opposite reaction. In the code, after adding a force, there’s neither an opposing force or a force subtracted from anywhere else:
“The reason is clear if you consider what's actually happening. We're not dealing with Newtonian mechanics. The force comes from the kinetic energy of the molecules of the fluid, which are randomly traveling in all directions (assuming the fluid is above absolute zero), and the change to the velocity field actually happens evenly across the gradient between the two points. In effect, we're applying the resultant force from a pressure gradient to the area it covers (which is two cells here), and we divide it between them.”
You can read the full technical article, which includes source code, on creating dynamic fluid systems, such as ink and smoke, for video games (no registration required, please feel free to link to this feature from external websites).