2D stable Fluid Simulation

Hi all,

Recently been working on a dynamic fluid simulation for a project and thought i’d share a little interactive demo from it.

Its pretty cpu intensive so i’ve posted a video as well as the demo.

Its based on the work of Jos Stam (Real-Time Fluid Dynamics for Games) and currently implemented on the cpu, though i’m hoping it will be possible to convert it into running on the GPU in Unity.

The demo features basic density/paint and velocity method but also the ability to add an image into the density to create interesting effects. There are some basic instructions in the demo, but ultimately its all about playing around with the values.

The default setting is to use a 256x256 simulation grid, though you’ll need a fast cpu to run it. If its too slow, just drop down to 128x128, though the images will suffer.

Hope you enjoy it.

+1! I would be happy if you could post a link on some pages about Jos Stams work :). I’m impressed :smile:.

– David

thx. I’;ve added a link to the pdf in my original post.

Pretty cool. Wish this could be used to add boat wakes to one of the Unity ocean waters.

Very cool. Good work :slight_smile:

Really pretty but not much in terms of game applications? I saw a game of pong once that used fluid dynamics for effects but what else could you use it for?

Interesting question, I wonder if people said the same about physics :wink:

You’re right in many ways, it is mainly useful as a visual effect (especially in 2D), but don’t forget there are two aspects here, first is the fluid solver, then there is the display output. No reason why you couldn’t use just the fluid solver to move ‘particles’ (i.e anything from ducks to houses if you wanted) around, following the flow/current in the velocity map. This would not only provide better realism, it could easily be used as a specific game mechanic.

Of course it is somewhat limited by being 2D, the next thing on my list would be to investigate SPH which can give you realistic 3D fluid. However for the time being pretty visuals are just what I need :wink:

I’ve always wanted to implement Stam’s method into Unity, but never found time for it, good job!
It’s funny that this is called a fluid solver, but no one ever uses it for water:)
Or is it strictly for compressible fluids?
Could this be used to simulate flowing water, say, with a heightfield for obstacles, dams breaking, rivers flooding and similar stuff?

I think its just non-compressible, but frankly i’ve got so many terms and names floating around in my head now i’m not so sure :wink:

It could be used in the manor you say, although I don’t think with an actual height map as it just purely 2D and the algorithm doesn’t account for heights. It can be extended to 3D, but would not be optimal for what you want. However you can use stripped down NavierStokes equations for that.

This is an old NavierStokes Unity Demo (converted from an even older Director demo). Simply left click to interact with the water, which will react to the two obstacles. Actually there are more obstacles, i just haven’t placed objects in there, since the obstacles themselves are defined in a map/array of bools. I never finished this demo, which is why reflections and other stuff isn’t in. It was just to compare performance against Director as I was moving over to Unity.

The code came from a great ‘lecture’ by Yann L ( he had some of the best graphic threads on gamedev.net back in the day) which includes easy to use/follow source.

solver.h
solver.cpp
normals.cpp

http://forum.unity3d.com/threads/130887-Real-Time-Fluid-Dynamics-for-Games

very nice!

this is really cool

do you have any intentions to put this on the asset store I can think of couple things to use this for that could be valuable