Java-Gaming.org Hi !
Featured games (81)
games approved by the League of Dukes
Games in Showcase (513)
Games in Android Showcase (119)
games submitted by our members
Games in WIP (575)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  Particle Effects in libGDX  (Read 17729 times)
0 Members and 1 Guest are viewing this topic.
Offline Jimmt
« League of Dukes »

JGO Kernel


Medals: 136
Projects: 4
Exp: 3 years



« Posted 2013-05-05 01:50:31 »

Hello, this tutorial will teach you how to use libgdx's particle editor and the .p files it exports in your libgdx game. I can't remember where I downloaded my version of the particle editor, but I've uploaded it here
 (may be outdated by the time you read this).

To start off, we have the particle effect editor ui (double-click to launch):

Several things to note here. On the upper half of the left side, the preview, and fps/particle count, etc. Below that, a list of all particle effects you have running/editing, so you can overlay effects and see what it looks like. The checkbox changes whether the effect displays or not, up/down changes the order.
Starting Out
The particle editor requires an image to use as a base (particle), so upload that with the "open" button. Below that, you can see several attributes - described in detail here. The graph determines how that attribute it governs fluctuates throughout the lifecycle of the particle effect; left click to drag around points, and right click to add new points. Scroll down to "Tint", and the system is about the same. Left click to edit, right click to add.

You may notice these as you scroll down, I will explain them.
Additive - determines whether additive blending is used - basically, whether the colors "stack" up. Use this for more vibrant and glowy effects.
Attached - if disabled, the particles sway as you move the emitter location, else particles stay the same distance away from the emitter regardless of movement (harder to explain just play around with it).
Continuous - self-explanatory, sets the particle effect not to repeat
Aligned - if the particle image is aligned to the rotation of the effect
Behind - idk - if someone knows post here

Export
The particle effect creator exports .p particle effect files, which can be opened in any text editor and manually edited - they are very readable.
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
Untitled
- Delay -
active: false
- Duration -
lowMin: 60000.0
lowMax: 60000.0
- Count -
min: 2
max: 6
- Emission -
lowMin: 0.0
lowMax: 0.0
highMin: 10.0
highMax: 10.0

They will look something like this, I cut out a small part of an example .p file.

Using it in your game
Now comes the coding, how to display these particle effects in your game. libGDX comes with its own ParticleEffect class, and you can load your effect quite simply:
1  
2  
ParticleEffect p = new ParticleEffect();
p.load(Gdx.files.internal("effects/fire.p"), Gdx.files.internal("effects")); //files.internal loads from the "assets" folder

The first argument tells where the effect data is, the second tells where the base image is.
To display your effect, simply use:
1  
2  
3  
4  
public void draw(SpriteBatch batch, float parentAlpha){ //what this method is called may differ depending on what 
//system you're using; I am using stage/actor
particleEffect.draw(batch);
}

To change location simply use effect.setLocation(). You will also need to call
1  
particleEffect.update(delta);

to update the effect. It uses delta, so probably in the render() method.

Rotating a Particle Effect
There are many ways to manipulate a libgdx particle effect, I will show a basic one, rotating.
1  
2  
3  
4  
for (int i = 0; i < particleEffect.getEmitters().size; i++) { //get the list of emitters - things that emit particles
            particleEffect.getEmitters().get(i).getAngle().setLow(angle); //low is the minimum rotation
            particleEffect.getEmitters().get(i).getAngle().setHigh(angle); //high is the max rotation
         }

To broaden out the effect (more cone-like), just make there be more difference between the high and low.

Using ParticleEffects with the Stage/Actor Hierarchy
I use Stage/Actor in libgdx, which is very convenient, but it might be difficult to implement particle effects with this system for some. I explain it here...
Particle Effect Actor
Since we are using scene2d, we need an actor for the particle effect. What I do is just have a ParticleEffectActor class with a ParticleEffect object.
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
public class ParticleEffectActor extends Actor {
   ParticleEffect effect;

   public ParticleEffectActor(ParticleEffect effect) {
      this.effect = effect;
   }

   public void draw(SpriteBatch batch, float parentAlpha) {
      effect.draw(batch); //define behavior when stage calls Actor.draw()
   }

   public void act(float delta) {
      super.act(delta);
      effect.setPosition(x, y); //set to whatever x/y you prefer
      effect.update(delta); //update it
                effect.start(); //need to start the particle spawning
   }

   public ParticleEffect getEffect() {
      return effect;
   }
}

Now, you can just add these actors to the stage, and the particle effects will show up. I hope this tutorial was helpful, if you have any questions post here or PM me. Big thanks to badlogicgames for making such an awesome system and for libgdx in general (oh, and nate, too).
Offline Nate

JGO Kernel


Medals: 149
Projects: 4
Exp: 14 years


Esoteric Software


« Reply #1 - Posted 2013-05-05 08:57:49 »

Nice, linked to this from the wiki:
https://code.google.com/p/libgdx/wiki/ParticleEditor

Offline wreed12345

JGO Knight


Medals: 25
Projects: 2
Exp: 2 years


http://linebylinecoding.blogspot.com/


« Reply #2 - Posted 2013-05-05 15:01:01 »

this looks very helpful! Last time I tried using the particle effects I am pretty sure I got confused and quit, but you make it quite simple.

Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline Jimmt
« League of Dukes »

JGO Kernel


Medals: 136
Projects: 4
Exp: 3 years



« Reply #3 - Posted 2013-05-05 15:38:58 »

Forgot to change one thing, the last code snippet here
1  
2  
3  
   public Spell getEffect() {
      return effect;
   }

should be
1  
2  
3  
   public ParticleEffect getEffect() {
      return effect;
   }

>Spell was own custom class that I was using...wish you could edit tutorials
Offline Cero
« Reply #4 - Posted 2013-05-05 15:53:37 »

There is no mentioning of pools here.
doing new ParticleEffect loads texture and json from file each time.
You dont want that.

Offline Vermeer

JGO Coder


Medals: 16



« Reply #5 - Posted 2013-05-22 19:48:42 »

Nice tutorial. I have been doing some particles in LIbgdx recently and have had problem after problem trying to run the Partical editor.

I think the problem is that there is no support for 64 bit java. I tried everything to get it to work. The only way it will work for me is to use an old computer.

Has anyone had this issue, or solved this issue?
Offline Nate

JGO Kernel


Medals: 149
Projects: 4
Exp: 14 years


Esoteric Software


« Reply #6 - Posted 2013-05-22 19:49:57 »

I added a section on running it from the nightlies a week or so ago:
https://code.google.com/p/libgdx/wiki/ParticleEditor#Running_the_Particle_Editor

Offline Vermeer

JGO Coder


Medals: 16



« Reply #7 - Posted 2013-05-22 21:12:42 »

@nate.  It's an amazing program. I looked at that page 2 days ago, neither method would work. But as soon as I use an older pc it will. It is probably me not understanding something.  But I have latest eclipse, latest nightly, everything, is as new as can be, yet it will not compile or run.

I've downloaded source, imported into eclipse, tried everything. Can't find main .method.

I have a feeling I need to use the 64bit lwjgl jar.  I  apologise if I am being a newb and doing something silly, but I spent 2 hours trying to get it to work. And I do recognise that it does obviously work, so I'm missing something!

Anyhow, it's very useful, and I would like to use it more!
Offline wreed12345

JGO Knight


Medals: 25
Projects: 2
Exp: 2 years


http://linebylinecoding.blogspot.com/


« Reply #8 - Posted 2013-06-18 14:22:35 »

I don't see it anywhere on this tutorial but you need to call effect.start() before you can use your particle effect

Offline Jimmt
« League of Dukes »

JGO Kernel


Medals: 136
Projects: 4
Exp: 3 years



« Reply #9 - Posted 2013-06-18 15:40:14 »

Yeah, you do. Can't edit tho Angry
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline knepe

Junior Newbie


Projects: 1



« Reply #10 - Posted 2013-09-13 21:13:42 »

Hello

I'm using this for my game, thanks.

I had to modify it a bit though, because .setPosition(x, y) and .start() method are in the act() method, which is called all the time when using Stage and stage.act() in the game loop. So the effect never finished just starts over and over. Created a new method called start() with .setPosition() and .start() in it instead.
Offline Suseika

Innocent Bystander





« Reply #11 - Posted 2013-12-16 05:08:23 »

This is a great tutorial, but it should probably be updated. Firstly, libgdx recently changed from SpriteBatch to Batch in actor.draw(). And those who will try to launch the code from your example with the latest nightly will not get neither a compile-time error nor a particle system drawn on their screen. To produce a compile-time error the code should have contained an @Override at draw() in the first place. Though it may still be wise to add that override (as well as change SpriteBatch to Batch).
Offline Violet Hoze
« Reply #12 - Posted 2013-12-16 07:33:45 »

I finally made it work .. A life saver :p
Offline Jimmt
« League of Dukes »

JGO Kernel


Medals: 136
Projects: 4
Exp: 3 years



« Reply #13 - Posted 2013-12-17 04:58:50 »

Glad to be of help Smiley

@Suseika I can't find any post relating to that refactoring, can you link me?
Pages: [1]
  ignore  |  Print  
 
 
You cannot reply to this message, because it is very, very old.

 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

Longarmx (34 views)
2014-10-17 21:59:02

Norakomi (25 views)
2014-10-17 09:22:06

Norakomi (24 views)
2014-10-17 09:20:20

lcass (26 views)
2014-10-16 10:18:58

TehJavaDev (50 views)
2014-10-14 18:39:48

TehJavaDev (50 views)
2014-10-14 18:35:47

TehJavaDev (40 views)
2014-10-14 18:32:37

BurntPizza (63 views)
2014-10-12 17:24:42

BurntPizza (36 views)
2014-10-12 17:10:45

BurntPizza (74 views)
2014-10-12 16:30:10
Understanding relations between setOrigin, setScale and setPosition in libGdx
by mbabuskov
2014-10-10 16:35:00

Definite guide to supporting multiple device resolutions on Android (2014)
by mbabuskov
2014-10-03 16:36:02

List of Learning Resources
by Longor1996
2014-08-17 04:40:00

List of Learning Resources
by SilverTiger
2014-08-06 13:33:27

Resources for WIP games
by CogWheelz
2014-08-02 10:20:17

Resources for WIP games
by CogWheelz
2014-08-02 10:19:50

List of Learning Resources
by SilverTiger
2014-08-01 10:29:50

List of Learning Resources
by SilverTiger
2014-08-01 10:26:06
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!