Archived

This topic is now archived and is closed to further replies.

How would you program a liquid?

This topic is 5271 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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]

Share this post


Link to post
Share on other sites
>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

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
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...

Share this post


Link to post
Share on other sites
I think the modelling using particles that represent volume would be very problematic to get running at any sort of real-time speed. The other alternative could be to instead model the surface of the water. The surface would take the form of a web of particles. For practical applications the number of particles would be variable. Now some of the surface is static and is up against the walls of a container. Other surfaces will be dynamic, assuming equilibrium isn''t reached.

The volume will be predetermined and constant. So each frame the volume is calculated. I haven''t put much thought into this, but if there isn''t a better way, an octree will work fine. If the volume is too small, then a force is exerted on the dynamic surfaces to increase the volume, this is in addition to the force due to gravity.

This should allow the water to flow. If some surfaces come into contact with each other, different shapes could be formed, such as one equivalent to a donut and not a sphere. If it breaks apart completely, a new mass of water is formed.

This is the best approach I can think of. It''s complicated and above my programming level, but I think it should work. If it is a viable model, then I''m sure someone will have documented it somewhere, if not then there''s probably a better way.

Share this post


Link to post
Share on other sites
I think a question needs to be answered before the "right" approach can be arrived at: What do you want the fluid simulation to be able to do?

Will it be Y-simple (restricted to height-map like shapes)? Will it be able to crest up into waves? Can it build up? Can it spray or splash? Will objects be able to push the liquid? Will it handle temperature? Will you be able to handle viscosity? Will it work regardless of the terrain/environment or will you need to restrict it to a simple terrain? What kind of system specs will it run on, how much of the CPU will be free at that point, how much RAM, how fast does it NEED to run?

These are the kinds of questions that you will need to answer before you can decide on an appropriate model. The model I suggested handles all of these because that''s what I''m interested in. But what do you want to do with it?

Share this post


Link to post
Share on other sites
Thanks for all of the tips and links.

I was thinking about the idea of only using particles for the surfaces, but that doesn''t solve all the problems, such as if I wanted to fill a cup with a liquid and then dump it somewhere else. I was also thinking of using real-time transforming multiplying polygons, which is probably my best fit so far.

Anyway I wasn''t really planning to program any of this into a game, unless it is a limited liquid simulator in a predefined enviroment.

Share this post


Link to post
Share on other sites
risking that my post is useless I´ll tell you what I´d try:

assume you liquid has a uniform density (you won´t be able to send sound waves through it besides some other problems that might occur). The behavior of the liquid should be entirely given by it´s surface (since for every point within the surrounding looks equal). Start with a sphere (form of least surface tension for a given volume) of liquid and lay a grid of equidistant points over it. Then apply the physical laws of your world to each of the gridpoints and take into account the forces applied to a point by it''s neighbours (surface tension). You should be able to fill a cup of coffee this way though the calculations propably won´t be simple. Problems that might occur is that when your fluid splits up in two entities you have to rearrange your neighbours.

Share this post


Link to post
Share on other sites
quote:
Original post by Sneftel
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.


At the extremes, where you''re calculating shock/boundary-layer interactions in turbomachines for aircraft engines, or for fighter-jets at the extremes of their flight envelopes, yes. But as with every other type of physics there are a huge number of simplifications ranging from things that run on supercomputers for days at a time to things that take hours on a normal PC to minutes on a PC to fractions of seconds on low-powered machines.

Case in point. Jos Stam of Alias Wavefront two years ago at the SIGGRAPH conference demonstrated real-time interactive fluid dynamics running on a Compaq pocket PC (XScale processor). I repeat, 2 *YEARS* ago on a PDA. Probably only achieved 10fps, and it was a 2D only simulation but that''s a far cry from days on a supercomputer, n''est-ce pas?

Jos'' work is a good place to start, as he has a stable, simple-to-code algorithm that works in 2D and 3D. It has limitations, but its a good place to start. I''d also highly recommend looking at Jeff Lander''s work. He developed a similarly simple technique that allows you to simulating flowing rivers that, with some work, can overflow their banks, create splashes, etc. In real-time.

Some reference material:

see the "Taming a Wild River" link

Jos Stam''s SIGGRAPH 2001 paper that contains a demo of real-time fluids on a Pocket PC



Graham Rhodes
Senior Scientist
Applied Research Associates, Inc.

Share this post


Link to post
Share on other sites
Thanks for the really neat stuff. That link to the Pocket PC Real-time fluids.

The hardest part about liquids I think is the fact that they can split or combine into different volumes. Yet this is one of the most important properties.

Share this post


Link to post
Share on other sites