[SOLVED] Particle Emitter freeze

Hi. My project is freezing on startup (about 50% of the time - black screen won’t respond). I have found what I wrote to cause it.

I create a bunch of particleEmitters, add them, no problems.

If I run them through this method:

public void vary(ParticleEmitter p)
    {
        float f = r.nextFloat();
        Vector3f cols;
        if(f<0.333f)//red
        {
            cols = new Vector3f(0.6f+(r.nextFloat()*0.4f),r.nextFloat()*0.5f,r.nextFloat()*0.5f);
        }
        else if(f<0.666f)//blue
        {
            cols = new Vector3f(r.nextFloat()*0.5f,r.nextFloat()*0.5f,0.6f+(r.nextFloat()*0.4f));
        }
        else
        {
            cols = new Vector3f(r.nextFloat()*0.5f,0.6f+(r.nextFloat()*0.4f),r.nextFloat()*0.5f);
        }
        
        ColorRGBA start = new ColorRGBA(cols.x,cols.y,cols.z,p.getStartColor().a);
        ColorRGBA end = new ColorRGBA(cols.x-0.05f + (r.nextFloat()*0.1f),cols.y-0.05f + (r.nextFloat()*0.1f),cols.z-0.05f + (r.nextFloat()*0.1f),p.getEndColor().a);
        p.setStartColor(start);
        p.setEndColor(end);

        int v = -4 + r.nextInt(9);
        p.setStartSize(p.getStartSize()+v);
        p.setEndSize(p.getEndSize()+v);
        
        p.setParticlesPerSec(p.getParticlesPerSec()-1 + r.nextInt(3));//+-1
    }

The freezes start. If I remove vary(emitter); then all is fine (at least I’ve run it 30 times in a row without a crash).
The method was just a quicky I made to vary a few things like color and so on. Have I done something terrible?

Here is the profiler in case it’s any use:

If you pause your debugger when it is frozen, where does it stop? It sounds like you may have some sort of infinite loop or something in your code but you aren’t showing the relevant code. Where is this method being called from and how?

1 Like

NextRandomInt is zero inclusive and limit exclusive. It may be that at some likely point, your setParticlesPerSec is turning negative.

p.setParticlesPerSec(p.getParticlesPerSec()-1 + r.nextInt(3));//+-1

getParticlesPerSec could equal zero, minus 1, plus zero, for example.

2 Likes

Good catch! I’m positive that’s going to be it because I have some really slow emitters. Thank you

1 Like

Shall we mark this as resolved?

Right you are, done

1 Like