7
\$\begingroup\$

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

\$\endgroup\$
3
  • \$\begingroup\$ 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. \$\endgroup\$ Commented Sep 2, 2011 at 11:57
  • \$\begingroup\$ 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. \$\endgroup\$
    – House
    Commented Sep 13, 2011 at 14:38
  • \$\begingroup\$ 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. \$\endgroup\$
    – House
    Commented Oct 2, 2011 at 5:37

2 Answers 2

7
\$\begingroup\$

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.

\$\endgroup\$
2
  • \$\begingroup\$ 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" :) \$\endgroup\$
    – House
    Commented Sep 13, 2011 at 14:25
  • \$\begingroup\$ @Byte56 I'll keep an eye on your blog then! \$\endgroup\$
    – Robin
    Commented Sep 15, 2011 at 17:55
1
\$\begingroup\$

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.

\$\endgroup\$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .