Water flow simulation

Hi, guys, it’s been a while :slight_smile:

Please take a look at https://www.openprocessing.org/sketch/845420
Please be sure to run it not from the browser, because it’s slow.

In shortly, what it does:

  • Creates a hightmap via diamond square algorithm;
  • Assigns each tile (cell) an amount of water;
  • Tries to calculate where the water would flow;
  • and show it every tick of time.

After some time waiting it should look like this:

The solid ground is shown in brown/orange: the higher, the lighter;
The water above the ground is shown in blueish color, the darker means more water.

  • no water erosion envolved yet (I’m not gonna do it until the simplest water flow works as I expect)
  • no precipitation/evapouration either so far

It does pretty well what I meant it to do, but

  • it’s waaaayyyyy tttooooo ssslllooowwww (no, I mean, I use the ‘write directly into video memory’ technique I used back in those days when the video memory was a segment at 0A000h. These days it’s called loadPixels(); pixels[whereINeed] = color(what, I, need); updatePixels();
  • and it’s slow not only because it has to draw every iteration. Any suggestions to improve the frame rate?
  • I guess I miss something like the water’s resistanse to flow, so that it should stop waving that much finding the equilimrium?

Anyways, any feedback is welcome!


I would try to remove cell-class with an array. There are seven calls to cell class in flow function after test

if (cells[i][j].w>0) {

And that test is eighth. Accessing array should be faster.

And I could be completely wrong. I didn’t go through the code thoroughly


thank’s, I’ll give it a try

1 Like

It might be interesting to look at how flow is modeled in eg PixelFlow – that is a very large library, but part of it is focused on high-performance fluid dynamics.

I don’t like using libraries because of the “not invented here” problem :slight_smile: I always try to try to figure out how it works reading tutorials and sources whenever available and rewriting the task with my own code, be it slow and simplified, but mine :slight_smile: and still there are things that I got running but not knowing how…

I’ll get to the library you mentioned, thanks.


I’ve changed things a little bit. now it works better. still slow, but the water finds major equilibrium in a reasonable watching time (about 10000 iterations), though still flows in and out.

1 Like