From fc82c9cde8ef25f94607a8204d645213316de7b9 Mon Sep 17 00:00:00 2001 From: Robert Osfield Date: Fri, 17 Sep 2010 15:39:53 +0000 Subject: [PATCH] Add clamping of the maximum number of particles per frame to avoid too many particles being introduced at once when the particle system comes back on screen. --- include/osgParticle/RandomRateCounter | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) diff --git a/include/osgParticle/RandomRateCounter b/include/osgParticle/RandomRateCounter index 763528437..96d211929 100644 --- a/include/osgParticle/RandomRateCounter +++ b/include/osgParticle/RandomRateCounter @@ -53,11 +53,22 @@ namespace osgParticle inline int RandomRateCounter::numParticlesToCreate(double dt) const { - _np += dt * getRateRange().get_random(); + // compute the number of new particles, clamping it to 1 second of particles at the maximum rate + float numNewParticles = std::min(static_cast(dt * getRateRange().get_random()), getRateRange().maximum); + + // add the number of new particles to value carried over from the previous call + _np += numNewParticles; + + // round down the number of particles. int n = static_cast(_np); + + // take away the number of rounded number of particles leaving the decimal place + // this is done so that two frames of 0.5's will results in first frame 0 new particles, second frame 1 _np -= n; + + // return the rounded number of particles to be created return n; - } + } }