Wednesday, 11 January 2012

Two Dimensional Water Simulation

This is my first blog post in a while so I thought I'd try something interesting. A few years ago I took a look at some discrete cell methods for simulating water in 2D. I decided to take a new approach and use a particle based method to simulate particles interacting. The image below shows the simulation running with 6000 particles at a reasonable framerate. All the particles in the scene repulse one another, and are accelerated earthward by gravity. If a naive approach were taken, the number of tests performed on each iteration of the program would be 6000 * 6000 = 3600000 distance calculations. This would be a large number of square roots for even the best CPU. In order to get around this I make use of some simple image filters to approximate the same result. Each particle is rendered to a bitmap buffer as a single pixel (I took this approach for a previous experiment). These pixel values are then blurred and the gradients at each point on the bitmap calculated using Sobel operators. At this stage calculating particles updated trajectories is just a case of subtracting the value of the gradient at each point from their velocity. Because the blur filter naturally falls off the repulsive force is proportional to the kernal used in the filtering process.

As usual just click to try out the demo!

The source code for all projects from now on will be added to a single flash project which can be found at:


  1. I think this is one of the most interesting articles I’ve read on this subject
    i like play game clickjogos online free and play game juegos de pou

  2. thank you what you did
    play game juegos de kizi online and play game jogos de friv