1
\$\begingroup\$

I have a particle system where instead of tracking the time each particle is alive, I just track the time of the oldest particle and the time of the youngest particle. Basically, the oldest particle is guaranteed to die first, so once the time since it spawned has passed, the particle is 'killed off' and that time is subtracted by the time between particle spawn (1 / rate). And for spawning particles, the time since the spawn of the youngest particle is tracked. Once this time exceeds the time between particle spawn (1 / rate), then a new particle is spawned and this time is subtracted by the time between particle spawn (1 / rate). However, there seems to be some bug in my code that I can't seem to find. The emitter seems to spawn only a few particles which move for the duration of their lifespan and then just stop and stay visible. The visibility of the particles is dependent on the index of the particle being within the range of the particleStartIndex and the liveParticleCount (These do wrap around and have been accounted for). I am testing with an emitter with 100 max particles. Here is my code:

inline void Update(float t)
{
    timeSinceFirstLiveParticle += t;
    timeSinceLastSpawn += t;

    //Kill Live Particles
    while (liveParticleCount > 0 && timeSinceFirstLiveParticle >= lifespan)
    {
        particleStartIndex = (particleStartIndex + 1) % maxParticleCount;
        liveParticleCount--;
        timeSinceFirstLiveParticle -= timeBetweenFire;
    }

    //Update Live Particles
    for (int i = 0; i < liveParticleCount; i++)
    {
        int idx = (i + particleStartIndex) % maxParticleCount;
        particles[idx] = particles[idx] + direction * speed * t;
    }

    //Spawn New Particles
    while (timeSinceLastSpawn >= timeBetweenFire)
    {
        int idx = (particleStartIndex + liveParticleCount) % maxParticleCount;
        timeSinceLastSpawn -= timeBetweenFire;
        particles[idx] = position + direction * speed * timeSinceLastSpawn;
        liveParticleCount++;
    }

    valid = false;
}

Any ideas?

\$\endgroup\$
  • \$\begingroup\$ Are you sure the bug isn't in your Draw() method instead? \$\endgroup\$ – congusbongus Oct 16 '14 at 3:34
  • \$\begingroup\$ What are the particle count before and after each loop. \$\endgroup\$ – Surt Oct 28 '14 at 18:09

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.