Question

I'm new to LibGDX...I'm trying to attach particle effect to bullet objects. I have player, which shoots multiple bullets, and I want to add some smoke and flames that follows fired bullets.

The problem is that I don't get the same effect every time. Initially first bullet effect looks like I want, every other bullet has a shorter trail behind it. Like there is no particles to draw.

I want if possible to use one particle emitter object. Don't want to have multiple instances for every bullet object.

I've tried to use reset() method, after drawing each bullet, but it doesn't look the same again. Only first one is fine, every other looks bad for a little bit.

Is there a way to do this?

Help!

This is the code snippet:

Init:

    bulletTexture = new Texture("data/bullet.png");
    bulletTexture.setFilter(TextureFilter.Linear, TextureFilter.Linear);

    // Load particles
    bulletFire = new ParticleEmitter();

    try {
        bulletFire.load(Gdx.files.internal("data/bullet_fire_5").reader(2048));
    } catch (IOException e) {
        e.printStackTrace();
    }

    // Load particle texture
    bulletFireTexture = new Texture("data/fire.png");
    bulletFireTexture.setFilter(TextureFilter.Linear, TextureFilter.Linear);

    // Attach particle sprite to particle emitter
    Sprite particleSprite = new Sprite(bulletFireTexture);
    bulletFire.setSprite(particleSprite);
    bulletFire.start();

In render method:

    // Draw bullets

    bulletIterator = bullets.iterator();

    while (bulletIterator.hasNext()) {

        bullet = bulletIterator.next();

        bulletFire.setPosition(bullet.getPosition().x + bullet.getWidth() / 2,
                bullet.getPosition().y + bullet.getHeight() / 2);
        setParticleRotation(bullet);


        batch.draw(bulletTexture, bullet.getPosition().x,
                bullet.getPosition().y, bullet.getWidth() / 2,
                bullet.getHeight() / 2, bullet.getWidth(),
                bullet.getHeight(), 1, 1, bullet.getRotation(), 0, 0,
                bulletTexture.getWidth(), bulletTexture.getHeight(), false,
                false);


        bulletFire.draw(batch, Gdx.graphics.getDeltaTime());

    }
Was it helpful?

Solution

As the comments state, the issue is that you are using a single effect for multiple bullets. I suspect it is set to non-continuous, so has a limited duration. As time goes by, the effect runs out.

I would suggest creating a particle effect pool for the effect, and obtain() from / free() to the pool. Attach a single effect to each bullet. This allows you to run multiple effects while limiting garbage collection (due to the pooling) as well as avoiding a load of the .p file for each new effect. The pool is created with a template that is copied each time obtain() is called. Note the cast to PooledEffect on the call to free().

import com.badlogic.gdx.graphics.g2d.ParticleEffect;
import com.badlogic.gdx.graphics.g2d.ParticleEffectPool;
import com.badlogic.gdx.graphics.g2d.ParticleEffectPool.PooledEffect;

...

ParticleEffect template = new ParticleEffect();
template.load(Gdx.files.internal("data/particle/bigexplosion.p"),
            ..texture..);
ParticleEffectPool bigExplosionPool = new ParticleEffectPool(template, 0, 20);

...

ParticleEffect particle = bigExplosionPool.obtain();

... // Use the effect, and once it is done running, clean it up

bigExplosionPool.free((PooledEffect) particle);

Conversely, you could skip the pools and make the smoke particle effect loop (continuous). This may not won't get you exactly what you're looking for, and might be considered a kludge, but might work in a pinch.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top