Particle System libGDX

Alex_Hyzer_Kenoyer picture Alex_Hyzer_Kenoyer · Mar 5, 2012 · Viewed 13.3k times · Source

Can anyone give me a good example of where to start with making a particle system in libGDX? I have looked at the test example in the libGDX source but I am still having trouble getting my head around it. Maybe just a good explanation of it will help. I'm thinking I want to make some sort of explosion with a lot of colorful particles. Any help is greatly appreciated!

Answer

Dominik Bucher picture Dominik Bucher · Mar 5, 2012

Define a particle effect in your game class:

public ParticleEffect particleEffect;

Initialize it:

    particleEffect = new ParticleEffect();
    particleEffect.load(Gdx.files.internal("data/particleEffect.p"), 
            Gdx.files.internal("data"));

In your render() method, position it at the place you want particles to be emitted (explosion location):

    particleEffect.setPosition(world.effectX, world.effectY);

And draw it finally (also within render()):

    particleEffect.draw(spriteBatch, delta);

That's it, pretty simple and straightforward.

Another thing, the effect itself, have a look at the Particle Editor by Nate, http://libgdx.googlecode.com/svn/jws/particle-editor.jnlp. Using the editor you should be able to create nice effects. Otherwise, copy the particle file from the examples and modify it.