Problem reported here: http://hub.jmonkeyengine.org/groups/effects/forum/topic/changing-particlessec-ingame/
I think this patch solves the problem but would really like someone else to look also.
Attached a slightly modified test case based on the one @nagper wrote.
[patch]
Index: ParticleEmitter.java
— ParticleEmitter.java Base (BASE)
+++ ParticleEmitter.java Locally Modified (Based On LOCAL)
@@ -1000,6 +1000,7 @@
}
// Spawns particles within the tpf timeslot with proper age
-
if (particlesPerSec > 0f) {<br />
float interval = 1f / particlesPerSec;
tpf += timeDifference;
while (tpf > interval){
@@ -1015,6 +1016,7 @@
}
}
timeDifference = tpf;
-
}<br />
BoundingBox bbox = (BoundingBox) this.getMesh().getBound();
bbox.setMinMax(min, max);[/patch]
[java]
package jme3test.effect;
import com.jme3.app.SimpleApplication;
import com.jme3.app.StatsAppState;
import com.jme3.effect.ParticleEmitter;
import com.jme3.effect.ParticleMesh.Type;
import com.jme3.material.Material;
/**
* Test for the case where particles per second is set to X, then to 0 and then back to X.
*
* The problem was reported
* <a href="http://hub.jmonkeyengine.org/groups/effects/forum/topic/changing-particlessec-ingame">here</a>.
*
* It seemed that when going back to X from 0 the particles that had not yet died were reused and
* had their lifespan extended a bit before dying.
*/
public class TestChangingParticlePerSecond extends SimpleApplication {
private ParticleEmitter emit;
private boolean on = true;
private float time = 0;
public static void main(String[] args) {
TestChangingParticlePerSecond app = new TestChangingParticlePerSecond();
app.start();
}
public TestChangingParticlePerSecond() {
super(new StatsAppState());
}
@Override
public void simpleInitApp() {
emit = new ParticleEmitter("Emitter", Type.Triangle, 3000);
emit.setGravity(0, 1, 0);
emit.setParticlesPerSec(2);
emit.setLowLife(2);
emit.setHighLife(4);
emit.getParticleInfluencer().setVelocityVariation(0.5f);
Material mat = new Material(assetManager, "Common/MatDefs/Misc/Particle.j3md");
emit.setMaterial(mat);
rootNode.attachChild(emit);
}
@Override
public void simpleUpdate(float tpf) {
time += tpf;
//increase the number of emitted particles once / 5 seconds
if (time > 2) {
on = !on;
if (on) {
emit.setParticlesPerSec(0);
} else {
emit.setParticlesPerSec(2);
}
time = 0;
}
fpsText.setText("getNumVisibleParticles: " + emit.getNumVisibleParticles());
}
}
[/java]