How would you program a liquid?

Started by
17 comments, last by Platinum314 20 years, 9 months ago
This has been bugging me lately. I want some physics engine where I would be able to simulate a liquid. I want to be able to have the liquid "flow" through paths of least resistance, take the shape of the container, splash & drip, and so on. Unfortunately the only way I have thought up so far is to use a particle engine with millions of particles. I am not sure if this would be the best or most effecient way. Any ideas? PS. This is my first post, I am excited to be learning game programming (with graphics).
The sentence below is true.The sentence above is false.And by the way, this sentence only exists when you are reading it.
Advertisement
Fluid dynamics simulations are incredibly processor-intensive. They run on supercomputers, for days at a time, with hundreds of processors and terabytes of RAM. It is not a good thing to try to do in a game engine.

How appropriate. You fight like a cow.
That''s what I thought.

It is kind of interesting that it is so difficult to program some stuff that are very common.

Of course it wasn''t that long ago that people thought that dynamic realistic shadows would be impossible in a game, now it seems like they are wide spread.

The sentence below is true.The sentence above is false.And by the way, this sentence only exists when you are reading it.
A cheap trick to simulate waves is to graph several sinus waves with varying amplitude and frequency. An example:
http://www.flatface.net/~eighty/files/flash/sinus.html (flash mx)

The ring waves you get from throwing a rock into the middle of a pond can also easily be simulated with the use of sinus waves. Example (3D, animated):
http://www.flatface.net/~eighty/files/java/Applet3D.htm (java 2)

What you're asking for is quite tricky though. Single effects can be achieved rather easily but you want to do quite a lot of things with the same engine, for which I don't know any other solution than to use a particle engine.

[edited by - eighty on July 7, 2003 5:59:38 PM]
>Unfortunately the only way I have thought up so far
>is to use a particle engine with millions of particles.
>I am not sure if this would be the best or most
>effecient way.

It''s most probably not a fast way, because you really need a huge amount of particles inside the liquid, which is a great waste (an animated implicit surface does better with large liquid volumes, possibly enhanced with surface particles).

You didn''t say the dimension of your problem. I remember seeing some simple two-dimensional "pixel water" effects in old DOS games. Of course they''re far from being physically correct and they don''t easily extend to three dimensions (because the resolutions in 3d are usually much smaller than those in 2d).

Another problem with particles is that the dynamics between the particles isn''t going to be easy to program. There is a method called Smoothed Particle Hydrodynamics (SPH) for it, though. Hacks through mass-spring systems should be possible as well.

To really make a physically correct liquid simulator, you''d need to study the field of computation fluid dynamics. "Stable Fluids", a paper by Jos Stam (1999), gives a nice introduction and presents a basic simulator (which is far from incorporating water, though).

>PS. This is my first post, I am excited to be
>learning game programming (with graphics).

If you really want to learn game programming, then trust me on one thing: forget the idea of seriously studying fluid dynamics and other boring crap

- Mikko Kauppila
quote:Original post by uutee

>PS. This is my first post, I am excited to be
>learning game programming (with graphics).

If you really want to learn game programming, then trust me on one thing: forget the idea of seriously studying fluid dynamics and other boring crap


Who said I was planning to use fluid dynamics in what I am programming now. I am just interested in the subject, and how I could possibly do it if I wanted to.

AFAIK, all physics engines split up the different behaviours of fluids and use a different model for each.
Surfaces are treated with either superimposed waves or Navier-Stokes equations, while for following the path of least resistance that path is calculated, rather than simulated.
Splashes and drips are almost always cheated, it is much easier to treat water in a container as a surface and go 2.5D.

Speaking of Navier-Stokes, though I haven''t tried it, I should immagine these would work the best for a non-intensive, all-round simulator. The actual equations are a set of integrations, but with a lot of approximations it still looks like water (they only apply to non-compressable fluids, which is generally what people want).

I took a brief look at the paper mentioned above, and that seems all about them too.
I''ve given this problem some thought lately. I think it would be fun to create a realistic (but not necessarily physically perfect, mind you) liquid simulation. But since I don''t own a supercomputer some clever tricks would be in order. My current approach (should I ever get the time to do it) would be to create a variant on your typical particle system. But instead of a bunch of free-wheeling particles, I''d build "molecules" and connect them to one another as a precomputation step. Then in real time I would allow the bonds between attraction sites to rotate, and if the tensional force applied to a bond exceeds some value I set for the viscosity, the bond would break. Bring two attraction sites close enough together, and you form a bond. At the same time, maintain a hull (not necessarily convex) around all the discrete molecule clusters. This is what you would render. Spatial partitioning could and should be heavily employed for optimization. Note that these molecules would NOT be to scale. Certainly the smaller the more realistic, but also the slower. That would be a tweakable variable.

Anyhow, that''s my idea. I''m not sure how much processor power you''d have left over if you made a bunch of this.

Something just popped into my head. You ought to be able to apply a LOD scheme. No point in using resources if they won''t be appreciated. Have a bunch of molecules close to the viewer and few far away. You''d want a dynamic thinning/un-thinning algorithm.

Then if you really want to get complicated, you could simulate phase transitions. If you want to go from liquid to solid, make the bonds MUCH harder to break in tension but allow breaks in rotation. To go to a gas, simply break all bonds and never reform them. Of course this makes the gas problematic to render. I guess you''d want point-sprites on all molecules with alpha blending. A gas would be more computationaly expensive (and solid far less because if they''re stiff they don''t tend to do much moving and you can wait around for physical event such as an impact to occur).

As you can tell physical simulation is one of my interests. One of these days I''ll get the time to get started actually creating these things.
Here''s two (hopefully) interesting links for you:

OpenGL Fluid & Gel Modelling
Liquid Animation
How do I set my laser printer on stun?
In one of those "indie game jams" somebody made a 3d fluid particle system that looked somewhat like water on a very small scale (or super viscous goop on larger scale). I think it used a metaball algo to wrap a surface around the particles which were much larger than the 2d pixel water stuff someone mentioned and thus more workable in 3d...

This topic is closed to new replies.

Advertisement