# Water simulation in Isometric game

I'm creating a game in Flash AS3 in which the player needs to modify land in order to direct the water in the right direction.

However, water simulation is a new topic for me and I'm kinda stuck. It doesn't have to be like realistic water, with ripples and stuff, but it has to flow, and if there's enough water, it needs to rise.

I've thought up two different types of water:

• A Spring: Infinite source of water. Used for simulating seas and stuff.
• Water block: Just one unit of water.

My current implementation shows how I'd like the water to spread, but it doesn't rise, and doesn't allow for finite water. Also, the spreading isn't accounting for any amount of water, it just duplicates instead of actually moving units of water.

I'm curious to how you guys would solve this problem. Any examples/pseudo-code is always appreciated.

Current version: http://dl.dropbox.com/u/319897/ProjectWater.swf You can manipulate the land by pressing left mouse. And can simulate one step of water at a time by pressing A.

Source of the water part: http://pastebin.com/Js2kYt4y

• For overflowing and pressure, which you'd need (an approximation of) to handle things like U-bends, consider simulating water as having a (potentially more than one block high) height, and merge blocks which are directly above each other. Obviously, you'd need to transform that back into blocks for display and other systems, and save the "head room" above each floor block. – Martin Sojka Sep 2 '11 at 11:57
• You'll want to add some information to your water blocks. A lot of information can be gained if you know who the block's parent is. You can get direction of flow, and follow parents back up to find the maximum height water can go. Then if you can't flow out, check your parents to see if you can flow up. Additionally you may want to add "water level" to your blocks, so that when a single non-source cube is flowing, it can divide its self to flow. See my comment on Nick's answer as well. – MichaelHouse Sep 13 '11 at 14:38
• I wrote a blog post on this and posted a video of how my flow looks. I didn't go into a lot of specific detail, as that would be boring for most readers. Feel free to ask any questions about specifics. Additionally, you should mark Nick's answer as accepted. – MichaelHouse Oct 2 '11 at 5:37

You need to check out cellular automata, and possibly fluid dynamics. Tarn Adams implemented a cell based fluids system in Dwarf Fortress, and those are the tools he claimed to use in an interview I read some time back (and also in 3 dimensions as per your use case). I think for your purposes, cellular automata (self-replication/propagation of water cells) will be enough. Cellular automata are basically what you see in the classic game of Life by John Conway.

The roguelike development community has a ton of information on this sort of stuff, because those games are inherently cell-based. I believe there is a rec.games.roguelike mailing list somewhere on teh webz where you could get some in depth info.

Also suggest you check out the Voxeltron videos on youtube, one of them (there are only about 2 or 3) shows liquid lava in motion. If you were to use small enough subcubes within your main cubic grid, you could get pretty cool non-blocky water going, using sine waves and the like for surface motion. Once again, for propagation you'd be using cellular automata techniques.

For an introduction to CAs, check this out.

• I believe this is the article mentioned (water is near the end). It's interesting, but cuts off rather abruptly. I'm working on this same problem, and have much of the problem solved, but haven't settled on a final solution. I'm using something similar to cellular automata. So I believe this is your answer. I'll do a post on my blog once I have water more "flushed out" :) – MichaelHouse Sep 13 '11 at 14:25
• @Byte56 I'll keep an eye on your blog then! – Robin Sep 15 '11 at 17:55

A good approach might be a flood fill.

A time dimension can be easily added by tracking the nominal quantity of water in each cell and only allowing the transfer of a certain proportion of water with each timestep of the simulation. This proportion could be divided equally among water-based neighbouring cells in a breath-first fill. This simple approach would mean that water fills out over plains and fills basins and such.

If you want to play with carving canons or rivers as a side-effect of high water transfer rates and such you can build a whole fun psuedo-physics sim on the side that will be pleasing for the players.

Artistically, this would give you the opportunity to put in waterfall blocks if water is transferring in terrain height and such.

You could make it so that there are sinks as well as springs, and perhaps the edge of the board is a natural sink, otherwise a spring would eventually flood the map.