Very simple liquids simulation

A simple game, 100x100 “map”, each cell holds the wall status (yes/no) and liquid volume (volume of liquid present, 0=air). From the top of the screen falls water and go to bottom as ordered by gravity and obstacles.

How to simulate this liquid (not necessarily water, I’m not sure what it will be, right now it behaves like a mix of water, gell and sand :D)?

Right now I have an algorithm like that:

  • if the neighbour is WALL, do nothing in that direction
  • move 3% of liquid volume down (if no obstacle), can’t move down if target cell’s volume is double volume of current cell
  • move 2% of liquid volume right (if no obstacle and the volume of liquid in the target cell is lower)
  • move 2% of liquid volume left (if no obstacle and the volume of liquid in the target cell is lower)
  • move 1% of liquid volume up (if no obstacle and the volume of liquid in the target cell is lower), also only if current cell liquid volume is >1000 (so the air gives some pressure and the water does not pop up into the very top unless there is an extreme liquid pressure/volume)

I’m also wondering about more elements (like gas or oil), which makes it tricky if we make an assumption that only one element can occupy a single cell (best for the display and clarity to the player). But that’s for later.

This is in AS3 (Flashpunk), but I’m sure you could port it to java:
Flashpunk water demo (Click to add water): http://dl.dropbox.com/u/3527200/watery/index.html
Flashpunk water source: http://pastebin.com/zFJLYjpK

I actually might try porting it if I have the time a little later

EDIT: Actually, I think the flash version was also ported from this original Processing version: http://w-shadow.com/blog/2009/09/01/simple-fluid-simulation/

http://knucklecracker.com/creeperworld2/cw2.php?

What’s stopping you from just adding more than one liquid variable in each cell? The interactions shouldn’t be that difficult.

I will post some link too so I’m not worse :slight_smile:


(BTW, have you noticed how few “liquid” games are there?)

You have similar problem to mine of the water behaving like sand (triangle) if you put enough volume. Althrough, it look very well under mediocre-low volumes.

[quote]What’s stopping you from just adding more than one liquid variable in each cell? The interactions shouldn’t be that difficult.
[/quote]
I’m worried about how to present it to the player (display). Mixing colours is the only solution I could think of and I’m not sure it is so great one…
Plus, if we make things like oil on top of water then incoming new water from the top should not mix with oil but push it saidways or something.

I added several liquids/gases per cell, it looks OK.

Now, how do I make oil? On one hand it is a liquid so it should go down (the bottom parts with higher pressure), on the other it should float on top of water.
I was thinking that it should occupy the same cell as water, but if there is another cell of water above it should try to go up until it is on top. It should also rather fast move sideways (spread on top of the water table). This all would work well if there was no pressure, but with the current pressure based physics there might be required to have more than one row of oil cells on top of water (so not all oil can occupy the very top of water table). Not sure how to make an algorithm for this.

Just do it as it is in reality. Calculate a pressure/density value based on the fluids/gases occupying the current and neighboring cells, move fluids depending on the pressure and fluid characteristics.

I doubt I have a processing power that would allow it (for gases I need 30-50 cycles per second minimum to make it look cool, liquids are less restrictive maybe 3-5 per second would be enough) and even if I had it I would probably do better for gameplay purpose to enlarge the map (20,000 cells make an absolute minimum map) than introduce perfect physics.

But well, maybe this would give some ideas, so, how the real one liquids behave exactly?

I don’t know the exact math, but you should know that oil floats up to the surface of water because it has a lower density. Gasses also behave like fluids with very low densities. Again I don’t know exactly how it works, but I don’t see how adding density calculations to your current system is a very big step performance wise.

Realistic 3D gasses/liquids require much more advanced simulations to be physically accurate, but they still are possible to do in realtime. For example it was done for Stalker: Clear Sky: http://www.youtube.com/watch?v=nfW_b3c00kM

I believe your problem is that you’re using too small cells. You should be able to get good results without 20 000 cells and some clever rendering to reduce the blockiness. Seriously, if you want performance, just port it to OpenCL or even an OpenGL shader.

What’s the difference between pressure and density? Right now I have volume (pressure) of each liquid/gas per cell (except plain air).

Renderer is not a problem at all, only part of the map (10000 cells) is displayed. I meant the algorithm that run all the cells regardless of visibility.
As for “blockiness” it’s the opposite, I want that effect :slight_smile: It is not to be 3D realistic thing but kind or pixelated/retro/voxel thing. Also, I want the cells to affect the gameplay (like redirecting the fluids via some pipes, getting burn if exposed to steam), not as merely visual effects.

Density = a property of a fluid/gas
Pressure = something you calculate based on the fluids/gases occupying a cell

Pressure depends on density and temperature of gases.

There, FTFY.

You can’t compress fluids (unless you attempt to recreate an environment similar to a neutron star).

You can compress gases, influencing their density and temperature (temporarily).

(Pressure * Temperature)/Volume

Density = Mass / Volume

You don’t need the conditions of a neutron star for compressing a liquid: it’s about 1000 bars at the botton of the Marianas trench, which will compress water a whopping 5% (or not quite, since it’s salt water).

For anything remotely practical though, you can treat liquid as incompressible. And for a game where you’re talking about propagating it into tiles, I say forget fluid dynamics entirely and fake it til it looks good.

Water increases pressure by 1 atmosphere every 10 meters (that’s why spaceships have muche more flagile hulls than submarines since the peressure difference in space is 1 atmosphere while there is 10 atmospheres difference at the 100m depth).

Also, an interesting observation, the pressure of air almost no change with depth (if you enter 100m building you don’t start suffocating also if you fall into a deep hole you don’t feel being crushed by all the additional air above you), completely unlike water (100m depth is around the max you can enter without tricky equipment).


Pressure of liquids adds soooo many fancy stuff (vulcanos, fountains) that it is a completely different quality. Without pressure liquids could be merely aestetic things, not gameplay things.

I’m no physics expert, but I’m pretty sure you can model pressure without requiring compressibility. In fact it should be easier, since then it’s just a matter of propagating force through the medium without “absorbing” any of it.

I linked that in the 3rd post of this thread… ::slight_smile: