Simple 2D fluid simulation

Hi lads!

I’ve just begun coding a new game, and I arrived at a point where I need to make a simple fluid simulation.
I don’t want anything complicated for now (it’s the first time I attempt fluid simulations). What I need is, I have a gui in which I’d like to place water. The water should not react to any external events, just gently wave back and forth at the bottom of my gui. I already made a reflexion effect, but now I need the wave, which will be added on top of the reflexion with alpha transparency. I also think it would be better to have it painted smoothly with different blue tones.

Any ideas about how to do that? Would you recommend particles or a vectorial grid? :persecutioncomplex:
I found a lot of resources on the net, but they were always about making fluid that reacts with the environment, which is not what I want, so I guess what it will look like will be simpler :wink:

Thank you for any help ;D

J0

Excuse my English please ::slight_smile:

if you think as a “2D side view” of a water surface … like (random google search)

http://blog.char95.com/images/post/simple-2d-water-surface-simulation/surfaceDemo.png

… i would do it in a full-screen-quad like texture shader - using value-noise FBM. very simple. just a fragment shader, noise + XY tex-coords.

actually, if it doesn’t need to react to anything but itself i would do a “2D top view” of water like that too.

I was fooling around with some fluid simulations, and came up with the following a while back. I don’t know if it will give you what you want. (I’m reacting to the graphic that basil_ posted.) The amount of activity in it is determined by the starting positions of the y values.

  1. create float array, with a length = x pixels (width that you wish to fill) and pre-fill with y values that match the water height in pixels of your starting wave shape.
  2. create a second array to hold the differences in levels between all the elements in the first array.

Algorithm:
(a) fill second array with differences.
(b) add 1/2 of those differences to each corresponding side.

Example:
water[0] = 7
water[1] = 8
water[2] = 9
water[3] = 9
water[4] = 8
water[5] = 7
water[6] = 6
water[7] = 5

populate:
delta[0] = 1 // between water[0] and water[1]
delta[1] = 1
delta[2] = 0

delta[6] = -1 // between water[6] and water[7]

then add back to original:
water[0] += 1/2 delta[0] // note, each end only has one delta contributor
water[1] += 1/2 delta[0] + 1/2 delta[1]
water[2] += 1/2 delta[1] + 1/2 delta[2]

I hope I am remembering this correctly. The amount of fluid stays the same, and the turbulence continues indefinitely. There is no “friction” component.

Thanks to you two :smiley:
Would you like credit in my game for that help? :slight_smile:

J0

hell no! :wink:

Ikr haha :wink:

Just let me know if the algorithm ends up being useful, and a link to the game when you have a working demo would be nice. It would be neat to see in action.

Sure! The game is far from being finished tho, and I prefer completely finishing it before posting it in showcase, but I will when I’m done and I’ll add a reminder here (if I manage to find this post :persecutioncomplex: )