Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Particle Emitter cannot emit particles if its emissions per second is updated every frame #1113

Closed
richardTingle opened this issue Jun 6, 2019 · 4 comments

Comments

@richardTingle
Copy link
Contributor

commented Jun 6, 2019

Within ParticleEmitter if the setParticlesPerSec is called every frame then no particle will ever be emitted. This is because setting ParticlesPerSec resets timeDifference. I can see this was done (in commit 07a6ca9) to solve the problem that after ParticlesPerSec has been zero for a long time timeDifference becomes huge.

The reason someone may want to call setParticlesPerSec every tick is to produce a smooth change in intensity, e.g. a slowly dying fire,

Test case (if reflection is used to just set the parameter it works fine, using the setter doesn't)

public class Main extends SimpleApplication {

ParticleEmitter fire;
float particlesPerSecond = 10;
boolean useReflectionHack = false;

@Override
public void simpleUpdate(float tpf) {
    try {
        particlesPerSecond -= tpf;
        if (particlesPerSecond < 0) {
            particlesPerSecond = 5;
        }

        if(useReflectionHack){
            Field f = fire.getClass().getDeclaredField("particlesPerSec");
            f.setAccessible(true);
            f.set(fire, particlesPerSecond); //Works fine (in this use case)
        }else{
            fire.setParticlesPerSec(particlesPerSecond); //no particles ever emitted :(
        }
    } catch (NoSuchFieldException | IllegalAccessException e) {
        e.printStackTrace();
    }
}

public static void main(String[] args){
    Main main = new Main();
    main.start();
}

@Override
public void simpleInitApp() {

    fire =
            new ParticleEmitter("Emitter", ParticleMesh.Type.Triangle, 30);
    Material mat_red = new Material(assetManager,
            "Common/MatDefs/Misc/Particle.j3md");
    mat_red.setTexture("Texture", assetManager.loadTexture(
            "Effects/Explosion/flame.png"));
    fire.setMaterial(mat_red);
    fire.setImagesX(2);
    fire.setImagesY(2); // 2x2 texture animation
    fire.setEndColor(  new ColorRGBA(1f, 0f, 0f, 1f));   // red
    fire.setStartColor(new ColorRGBA(1f, 1f, 0f, 0.5f)); // yellow
    fire.getParticleInfluencer().setInitialVelocity(new Vector3f(0, 2, 0));
    fire.setStartSize(1.5f);
    fire.setEndSize(0.1f);
    fire.setGravity(0, 0, 0);
    fire.setLowLife(1f);
    fire.setHighLife(3f);
    fire.getParticleInfluencer().setVelocityVariation(0.3f);
    fire.setParticlesPerSec(particlesPerSecond);
    rootNode.attachChild(fire);
 }

}

Instead I think timeDifference should be capped when setParticlesPerSec is called such that at most it is just about to emit a particle. So the new setParticlesPerSec would be

public void setParticlesPerSec(float particlesPerSec) {
    this.particlesPerSec = particlesPerSec;
    timeDifference = Math.min(timeDifference,1f / particlesPerSec); //prevent large accumulated timeDifference from causing a huge number of particles to be emitted
}

Notes:
Original commit discussed here https://hub.jmonkeyengine.org/t/particleemitter-particlespersecond-set-to-0-accumulates-timediff/23432

Original commit 07a6ca9

Thread https://hub.jmonkeyengine.org/t/particle-emitter-cannot-emit-particles-if-its-emissions-per-second-is-updated-every-frame/41930

I plan on fixing this myself

@richardTingle

This comment has been minimized.

Copy link
Contributor Author

commented Jun 6, 2019

PR for this issue #1114

@stephengold

This comment has been minimized.

Copy link
Contributor

commented Jun 9, 2019

@richardTingle Please verify that the issue is fixed in master branch.

@stephengold stephengold added this to the v3.3.0 milestone Jun 19, 2019

@richardTingle

This comment has been minimized.

Copy link
Contributor Author

commented Jul 5, 2019

I've pulled the most recent version of master and used it in my test case. I can confirm the issue is now solved

@stephengold

This comment has been minimized.

Copy link
Contributor

commented Jul 5, 2019

Thank you!

@stephengold stephengold modified the milestones: v3.3.0, v3.2.4 Jul 8, 2019

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
2 participants
You can’t perform that action at this time.