XNA Fluid simulation
Andrew over at RenderHeads brings news of a new a new XNA video demonstrating the use of fluid dynamics in a very smooth simulation.
So basically it’s a fluid simulator (doh!).. It was actually written in C++ using DirectX9 about a year ago, but I decided to port it to C# and XNA to see how it would perform and because I didn’t really want to touch C++ again
I was amazed at the performance. The physics code in C# is actually runs faster than my original! I’m sure the C++ version could have been optimised, but I was expecting the C# version to always be much slower.. Pleasantly surprising!
Once I had it in XNA I was also able to optimise the rendering lots using hardware instancing. I could have done this in DX9 but it’s just so much easier with the XNA API
And the post-processing is also so much easier – hooray! It took about a day to port it, but then I was also cleaning up the code loads as I went along.
So for the physics it’s using SPH approach. Basically around 1000 particles simulated using a simplified version of the Navier-Stokes fluid equation. There is a volume hashing system to group neighbouring particles to cut down on the number of inter-particle operations. Then there are two rendering methods – blurred spheres and marching cubes. Marching cubes is the fairly standard polygonisation of a field. The blurred spheres version renders each particle as a sphere (this is where the instancing comes in). It renders to 4 seperates textures using MRT – Colour, Normals, Depth, ViewDirection. The normals texture is then blurred using Kawase’s blurring method. This blurring is what makes the sphere particles look like they are glooped together. Then we use deferred rendering to composite it all together. Rendering a fullscreen quad and lighting the scene using the previously rendered textures.
Thats it
We also have collision detection with simple shapes like planes, spheres and cylinders.
In theory we could have a lot more particles. The slowness comes when there are a lot of particles very close to eachother (lots of inter-particle calculations). So if they were more spread out we could handle lots of particles..something for the next demo perhaps
![]()
We will be releasing the .EXE very soon.
Hope you like it
Andrew/RenderHeads

No comments
Jump to comment form | comments rss [?] | trackback uri [?]