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 )?

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.

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 )?

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.

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.

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.

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.

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 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.

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).

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.

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.

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.

java-gaming.org is not responsible for the content posted by its members, including references to external websites,
and other references that may or may not have a relation with our primarily
gaming and game production oriented community.
inquiries and complaints can be sent via email to the info‑account of the
company managing the website of java‑gaming.org