I've gotten a lot of questions about how the water system works in DwarfCorp recently, so I thought I'd write a post about it.
Simulating the Water
Water in the real world is made up of millions of tiny particles that flow past each other and form temporary atomic bonds. These little interactions between particles follow simple rules based on electrostatic repulsion and attraction. The net result of all these millions of interactions causes interesting effects from water pouring out of a cup to flowing rivers and waves in oceans. Unfortunately, modeling water at this level is still beyond reach of modern computers running in real-time. So we have to make serious simplifications to make something that looks "okay". Like in Minecraft or Dwarf Fortress, the water in DwarfCorp is a "cellular automata" (CA) based system. A "cellular automaton" is a kind of program where cells (or in my case, voxels on the scale of about a meter) have values which are modified by simple rules. These simple rules can lead to some cool behaviors, but doesn't even come close to simulating real-world water. CA-based water ranges from extraordinarily complex to fairly simple. In DwarfCorp, the rules are very, very simple. By just having a few of these easy rules, the engine is able to simulate lots and lots of water extremely quickly. We store a grid of "Water Cells" for each voxel in a small "chunk" of the world. Each cell has a few bits of information: the water level (a byte from 0-255), a "flow" vector, Â and the fluid type (currently just water and lava). Each iteration, we loop through all of the water cells which have any water in them and perform the following rules:
Â Â Â
That's it! For lava, we just move a slightly smaller amount to each adjacent cell in step 3. This leads to some pretty cool effects, like flowing rivers, waterfalls, basins which fill and empty, and underground channels. Unfortunately, the water in DwarfCorpÂ does not currently support a pressure model (as in Dwarf Fortress), so water never moves *upward*. We're planning on adding this soon, however. A lot of people have asked how I render the water, as opposed to simulating it. Water in DwarfCorp features reflections and refraction, waves, Â subtle texturing, and shorelines. All of this is eye-candy implemented in shaders.
Rendering The Water
The water in DwarfCorpÂ is rendered as a surface mesh that's generated by averaging the heights together between adjacent cells, and culling away faces that are covered by water or obstacles. If we just rendered this mesh as a textured surface, it would look okay, but we wouldn't be able to get the kind of effects you see in DwarfCorp screenshots. To do this, we use pixel and vertex shaders. There are a few effects that are layered on top of each other in DwarfCorp:
That's it! Here's a video of the liquid simulation system in action!
or here: http://www.dwarfcorp.com/Vids/WaterTests.mp4 You can play with a prototype of the water simulation in 2D here: http://www.dwarfcorp.com/Prototypes/Watersim/