Particle System Tutorial on Android Part 3

Basic Fountain Particle System

First, no screenshot this time. I took a couple using ddms but found that it really didn’t represent what the application looks like. This looks way better in motion than in a single screen capture. I’ve changed the whole application to have all the particle system demos in the one app, selectable from a menu. The code is quite a bit different so I’ll only mention the highlights for the fountain particle system in this post. If anyone wants more information email me at bayninestudios@gmail.com

To simulate a fountain, we’ll need to add to the particle class itself. Each particle now has an individual velocity.

// velocity
public float dx, dy, dz;

Some changes need to be made to the particle system. Those massive triangles need to be made smaller, which is done from a simple PARTICLESIZE constant and changes to the vertex array

float[] coords = {
            -PARTICLESIZE,0.0f,0.0f,
            PARTICLESIZE,0.0f,0.0f,
            0.0f,0.0f,PARTICLESIZE};

Another addition to the code is a framerate modifier. Each time the particle system is updated we need to check how much time has passed. This is because on slower machines a particle system would appear to be moving slower. If we know how much time has passed between updates, we can make the particles move at the same apparent speed between different devices. I put this in the update routine but I’m undecided if the main ParticleSystemDemo class should handle framerate calculations or if the ParticleSystem class.

Each particle in the fountain has two forces moving them each update. Their own velocity, and gravity. What is done in the update method is to adjust the particle z velocity by gravity, update the particle’s location given it’s x,y,z velocities. Last, check if the particle hits a FLOOR value, and if so re-initialize it.

    // update the particle system, move everything
    public void update()
    {
        // calculate time between frames in seconds
        long currentTime = System.currentTimeMillis();
        float timeFrame = (currentTime - lastTime)/1000f;
        // replace the last time with the current time.
        lastTime = currentTime;

        // move the particles
        // first apply gravity to the z speed
        // second move the particle according to its speed and time frame
        // third re-init a particle if it has gone below the 'floor'
        for (int i = 0; i < PARTICLECOUNT; i++) {
	    // apply a gravity to the z speed, in this case 
            mParticles[i].dz = mParticles[i].dz - (GRAVITY*timeFrame);

            // move the particle according to it's speed
            mParticles[i].x = mParticles[i].x + (mParticles[i].dx*timeFrame);
            mParticles[i].y = mParticles[i].y + (mParticles[i].dy*timeFrame);
            mParticles[i].z = mParticles[i].z + (mParticles[i].dz*timeFrame);

            // if the particle hits the 'floor' respawn it
            if (mParticles[i].z < FLOOR) {
                 initParticle(i);
            }
        }
    }

Improvements on this?

  • Simulating partial steps between framerates. If the time between frames is large, I should update in some small timestep the particle system, so that the entire time does not move the particle in the one velocity.
  • Add time to live to the particles so that they can die in mid-air or stop them on the floor
  • The particles spawn all at the same time. It would be better to have them be spawned slowly or simulate the particle system for a few seconds before drawing

That's it. Again the code can be found on Google Code. The demo is also available on the Android Market, search for "Particle System"