Sunday, October 10, 2010

Particles on iPad : part IV

I should note that I'm not doing standard particles (with emitters, colour change, limited life span, etc.) as described by Reeves in 1983 - for that I suggest reading [Reeves83].

What I'm looking at is stably modelling, with a decent frame-rate, massive system of interacting small spherical objects - which I loosely call particles.  My initial attempt was to use smoothed particle hydrodynamics in order to imbue the masses of particles with the appearance of fluid flow (see [Monaghan92]).  That didn't work as I'd had hoped.  So I went to modelling particles using a finite element method.

The issue with the finite element method is best explained with a column of a dozen particles high falling and colliding with the ground.  Assume the column is perfectly aligned so that these spherical objects do not slide to the side.  Consider the time of impact.  On the first frame, the bottom particle will hit the ground and a repulsive force will be applied.  On the second frame that repulsive force will be applied to that particle and the one above it.  However the particle above it still has quite a bit of speed and due to the collision is applying a bit of a downward force.  In the end, it can take a few hundred frames before the system starts to show a stable result.  In the meantime the user is presented with particles unrealistically entering each other's physical space.

That led me to search for solutions that would simultaneously solve for all the collisions - some sort of magic way to prevent the visual oddities while the system stabilizes itself.  A way to more quickly reach a stable state if you prefer.

So I ended up writing the equations that solved for the collisions and see how to solve them as a single set of equations; not treating each individually.  The obvious method, I believed, was to write it out as a matrix.  Then, inverting and solving the system would lead to the desired solution (of course, I'd be forced to use a relaxation scheme...).

Then came the issue:  let's say I'm solving for "k" - a value indicating how far a particle should move.  I have at least 4 equations (one per colliding particle) used to determine each "k".  I could solve for "k" locally for each equation easily - however which equation would give me the "better" answer so that I could obtain a solution through relaxation?  Averaging the results should be satisfactory...

However, what am I computing?  A form of pressure.  Pressure accumulates where there are plenty of particles.  Taking the gradient of the pressure would give me a force pointing towards the best direction for the particles to avoid collisions.  A bit of doodling on paper revealed that pressure would be a function of density (how close particles are to each other).

Doesn't this sound awfully familiar?  Yes - a fluid simulation using smoothed particle hydrodynamics would do exactly that.  I've just started to re-invent the wheel.  So; let's stop reinventing and see what went wrong.

My particles were overly compressing each other.  As though they were not being forced apart - as though pressure was never strong enough to repel them.  Another observation is that the system would not stabilize...

Now; let's take another step back.  Let's consider pressure in more detail.  Pressure in an incompressible system to be precise.  Pressure should build-up as the system forces itself into a corner / against an obstacle.  That is, pressure is not local to a small set of particles.  Specifically, the force of pressure will want to minimize pressure.

Imagine a local system, whose gradient points downward to a set of particles with little/no pressure in comparison.  Now these particles form the liquid in a glass of water.  These particles below are tightly packed, so they should equally repel - not be the destination for - a new particle.

So I'm going to review the equations, better plan how I'll model the particles on paper.  Then implement a new version in the coming weeks.  Maybe a few posts on my thoughts may appear in the meanwhile...

No comments:

Post a Comment