# Water flow

This topic is 4885 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

Hiya, I'm hoping someone can answer a question I have. Imagine a cave with a 3x3 grid filled with water. Below it, a solid piece of rock, below that, a vertical chasm of 9 units high. Knock out the seperating rock, water goes down and fills up the chasm. No probs there. But what if the chasm is 5 units? Where do the other 4 units of water go? If I had 3 chasms, one 5 units high, and 2 at 2 units high, you could say all 3 chasms would get full with water, if they were on the same level could you not? Is there any type of mathematical formula or perhaps some pseudo-code that I can implement this please? I've got some idea in my head on how to do it, but I don't know if I'm looking at it too closely. Appreciate any feedback, thanks.

##### Share on other sites
You should really be thinking about this in terms of volume instead of area, but yes, they all should be filled if they are all on the same level as you suggest.

##### Share on other sites
Quote:
 Original post by WhiteRoserBut what if the chasm is 5 units? Where do the other 4 units of water go?

The other 4 units remain in the 3x3 grid.

Quote:
 Original post by WhiteRoserIf I had 3 chasms, one 5 units high, and 2 at 2 units high, you could say all 3 chasms would get full with water, if they were on the same level could you not?

Yes.

Quote:
 Original post by WhiteRoserIs there any type of mathematical formula or perhaps some pseudo-code that I can implement this please?

You mean other than simple addition and subtraction?

##### Share on other sites
Quote:
 Original post by WhiteRoserIs there any type of mathematical formula or perhaps some pseudo-code that I can implement this please?

water_volume_left = water_volume_total - chasms_volume_totalif (water_volume_left == 0){  // all chasms completely filled with water  // no water left in the grid}else if (water_volume_left < 0){  // chasms partially filled with water  // water level in chasms relative to the grid = water_volume_left / (3 * 3),  a negative number}else if (water_volume_left > 0){  // chasms completely filled with water and some water left in the grid   // water level in the grid = water_volume_left / (3 * 3)}

And I hope I haven't done your homework here..

##### Share on other sites

Rest assured, there's no homework involved, at 34 I haven't done homework for about 18 years! LOL.

I appreciate it's down to simple addition/subtraction, but what I was trying to get at was also the fact that if a 3x3 grid of water (9 units) leaked into a horizonal chamber of 9 units, that horizontal chamber would fill.

But imagine a slope, with a 2 unit chasm. Sure the 9 units of water would fill the 2 unit chasm, and thus 7 units would continue to flow, again as you said, simple mathematics. The thing that has me stumped is 'handling' the water and showing it as a steady trickle (or fast flow) as opposed to simply there one minute and gone the next.

The "look at it in volume, not area" comment has helped a lot, so thanks for that one.

##### Share on other sites
Quote:
 Original post by WhiteRoserRest assured, there's no homework involved, at 34 I haven't done homework for about 18 years! LOL.

[lol]
Quote:
 Original post by WhiteRoserBut imagine a slope...

I amagine it would look something like this?
          /         /        /       /       |    .~~| <-- water level   /|  |  / |  | /  |  |/   .--.

As long as the water flowing at a relatively slow pace, gravity would force it to fall into the chasm.
Once the chasm is completely filled, the water would start to flow over the water in the chasm. In reality, new water would be pushed into the chasm and water already in the chasm would be forced out, but for a simple simulation that doesn't really matter.

If there is more than one chasm (imagine one higher up the slope in the drawing), the topmost chasm would get completely filled first, than the second one would. Note that in 2D the slope causes the chasm to fill one by one, instead of simultaneously like in the very first example. In 3D (a water 'plane' instead of a water 'line') it's a little more complicated, chasms on the same 'level' would fill up simultaneously, but if the chasms have different volumes, the point in time where they start flooding would be different, having an effect on the moment when chasms below them start filling up.

When the water flow is really strong, it would 'beat' gravity, and water may fly over the chasms completely. Than you would have many more parameters to take into account, so I won't into that scenario right now.

In pseudo code it would imagine it to look something like this:

There is a drop of water falling down...
If it encounters a chasm, see if the volume of the water in it equals the volume chasm. If not, fall into the chasm. If it is, continue

flowing down until it finds another chasm and perform the same check again.
O if you don not want to deal with individual drops:
volume_per_time_unit = 1     // amount of water passing by every time unitnumber_of_chasms = 3chasms_volume[3] = {2, 4, 4} // three chasms from top to bottomwhile (end_of_simulation)   // end_of_simulation couls be anything, maybe when you run out of water{  time_unit++; // advance in time  for (i = 0; i < number_of_chasms)  {    if (chasms_volume.water_volume < chasms_volume.chasm_volume)    {     chasms_volume.water_volume += volume_per_time_unit;     break; // don't check the rest of the chasms    }  }}

Not that it's pseudo code, don't expect it to be an example of good coding.

• ### What is your GameDev Story?

In 2019 we are celebrating 20 years of GameDev.net! Share your GameDev Story with us.

• 15
• 14
• 46
• 22
• 27
• ### Forum Statistics

• Total Topics
634046
• Total Posts
3015224
×