I'm making a libGDX game that uses particle effects for explosions. This runs quite well on new phones but on older phones (tested on the HTC Desire) when there are more than a few explosions on screen fps lags quite significantly.
There are two options I'm currently considering:
There are a lot of particles per effect so reducing that count may be an option but I would rather not for consistency and aesthetic reasons.
Using an animation generated from running the particle effect and saving each frame. This would look nice and would be consistent. There may be issues with image size. I also can't see how this can be done with the libGDX particle implementation.
Any ideas?
EDIT: Some snippets of the code: After loading the effect in the Assets class:
particleFire.load(Gdx.files.internal("explosion.p"),Gdx.files.internal(""));
I load the particle effect in the projectile object's constructor. I'm doing this object creation in the main rendering loop which I know is a candidate for optimization.
explosion = new ParticleEffect(Assets.particleFire);
In the rendering thread this method is called to draw the effects.
private void drawBlasts(float delta){
for(Missile projectile : world.missiles){
if (projectile.missileState == State.EXPLODING){
if(projectile.stateTime==0.0f) {
projectile.explosion.start();
continue;
}
projectile.explosion.setPosition(projectile.position.x, projectile.position.y);
projectile.explosion.draw(spriteBatcher, delta);
}
}
}
A snippet of explosion.p
- Delay -
active: false
- Duration -
lowMin: 1000.0
lowMax: 1000.0
- Count -
min: 0
max: 100
- Emission -
lowMin: 0.0
lowMax: 0.0
highMin: 250.0
highMax: 250.0
relative: false
scalingCount: 1
scaling0: 1.0
timelineCount: 1
timeline0: 0.0
- Life -
lowMin: 800.0
lowMax: 800.0
highMin: 1000.0
highMax: 1000.0
relative: false
scalingCount: 3
scaling0: 1.0
scaling1: 1.0
scaling2: 1.0
timelineCount: 3
timeline0: 0.0
timeline1: 0.66
timeline2: 0.9931507
EDIT 2: These effects are additive so there is a good bit of blending going on too.
Looking at both of your ideas, they would both work. The first could be solved by making the particles slightly bigger and using fewer of them. The fewer particles that the phone has to process the better as each particle moves dynamically and thus many calculations per frame tick.
Your second option I feel would be better, as there is no/little processing time (simply calculating how far through the "explosion" it was and selecting the appropriate frame. However there is also the trade off that all your explosions will look very similar. If you could post some of your code (how many particles you're using, how often explosions happen, roughly how many explosions are happening simultaneously) that would be helpful. I had a similar issue a while back, but with a much simpler particle effect and I solved it by just rendering one particle effect and moving it to everywhere that it was being shown each frame
Edit 1: Ok, so I think what you are looking for is a particle effects pool. You basically allocate a set number of particle effects beforehand instead of creating them as needed as I believe you may have. By reusing objects from a fixed pool instead of allocating and freeing them individually you can save a lot of memory and processing. I tried what I believe you've done on my Desire HD and got about 22 fps with 12, 1 second 100 odd particle effects on screen at once. Then pooling them I got about 25. It's not exactly shockingly good but every little helps. Also reducing particle count even to about 75% isn't all that noticeable and saves you A LOT of frames!
Particle Effect Pool Libgdx JavaDoc
Sorry I couldn't help more!
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With