Particle System Tutorial on Android Part 5

You can notice from the first couple of fountains that all the particles get generated at the start and then spawn after the time to live is up. This results in a massive blast at the start and then things start to get more evenly distributed. In this tutorial, the particles will get slowly released in to the system. This is actually fairly easy to do with only a new variable called activeParticles (set at zero) and few lines of code in the update method in ParticleSystem.java

// add the particles slowly
if (activeParticles < PARTICLECOUNT)
{
    // calculate how many particles per frame.  I have set
    // the particles per second at 100
    int addParticleCount = new Float(100f * timeFrame).intValue();
    // always be adding at least one particle
    if (addParticleCount < 1)
    {
    	addParticleCount = 1;
    }
    activeParticles = activeParticles + addParticleCount;
    if (activeParticles > PARTICLECOUNT)
    {
    	activeParticles = PARTICLECOUNT;
    }
}

The other feature added is a bounce when the particles hit the FLOOR. So instead of just stopping all speeds, we keep the x and y speeds intact and invert the Z speed. As well, cut the Z speed in half to have the particle not bounce as high as the previous height. The time to live can be extended to give that rolling look.

if (mParticles[i].z <= FLOOR)
{
    mParticles[i].z = FLOOR;
    // invert the Z speed and cut it in half so the bounce is not as high
    mParticles[i].dz = mParticles[i].dz * -.5f;
}

Again the code can be found on Google Code. The demo is also available on the Android Market, search for "Particle System" or scan the QR code below