Subscribe to GameFromScratch on YouTube Support GameFromScratch on Patreon


3. November 2014

 

 

Now we are going to look at using particles in LibGDX.  This is a somewhat complex subject, so I am going to break it over a couple posts.  In this first example, we are going to create a 2D particle system in the editor.  Then we are going to write the code to run the particle system.  This process can be somewhat confusing, especially running the Particle Editor, so I have also done a video version of this tutorial.  Don’t worry, I will always continue to do text versions of all LibGDX tutorials, I am just going to mix in video when I think it will help.

 

So if you prefer, this topic is available in the video below.  Click directly on Youtube to see it in full resolution.  Once again, this video covers exactly the same content the text below does.

 

In this tutorial we are going to use one of the tutorials included with LibGDX, the particle editor.  In order to follow along you need to install the gdx-tools in your project.  Fortunately this is a pretty easy process.  When you run the setup utility to create your project, make sure you set tools to true.

 

image

 

This will result in the particle editor being included in your project dependencies.  Now it’s a matter of running the ParticleEditor, which isn’t as obvious as you would think.

 

You can run it from the command line, but this presents a certain special challenge, since LibGDX moved to a Gradle based setup…  so, where exactly is gdx.jar these days?  That’s a good question, and the answer is very very well hidden.

 

First make sure you’ve built your project at least once, so then all of the dependencies have been resolved ( AKA, Gradle downloaded LibGDX ).  Now LibGDX will be located somewhere in your Maven repository, which is a hidden folder.  The actual directory it will be in is random thanks to a GUID key.  On my computer on Windows its:

C:\Users\Mike\.gradle\caches\modules-2\files-2.1\com.badlogicgames.gdx\gdx-tools\1.4.1\SOMEREALLYLONGGUID

 

Obviously on your computer this will be somewhere else, and it will change with each version.  Now that you located it, you can run it.  Fortunately there is a much easier option for running the particle editor if you use either Eclipse or IntelliJ IDEA.  I will cover each in turn below ( and also show both in the video above ):

 

IntelliJ

 

Once you’ve loaded your project into IntelliJ IDEA.  Now in your Projects window, locate gdx-tools.jar ( there will be a version number too, currently 1.4.1 ).

 

image

 

Expand it until you find particleeditor, then locate the class ParticleEditor.

 

Now right click ParticleEditor and select Run ParticleEditor… main():

 

image

 

The first time you need to set some configuration values:

image

 

Really, all you need to do is set the Use classpath value to your desktop module.  Now click Run:

image

 

One working Particle Editor.

 

Eclipse

 

Eclipse is actually much simpler… if it works, which is a big if.

Simply import your project.  Then right click your desktop project, select Run As or Debug As->Java Application.

 

image

 

It should now prompt you to select which Java Application to run:

image

Select ParticleEditor – com.badlogic.gdx.tools.particleeditor and click OK.

The Particle Editor will now load.

 

Your First Particle Effect

 

Now that you’ve got the particle editor open, let’s save an effect to work with. 

In this particular tutorial we are just going to go with the default as it is already configured, which is a flame looking effect.  Locate and click the save button:

image

We want to save the resulting file to our assets folder.  If you have an Android project it will be located at [project]/android-proj/assets otherwise it will be in [project]core-proj/assets.  Save the file there, the extension doesn’t matter.  For this code example I am using particles.party.

 

Code Time

 

Now let’s take a look at the code required to display this effect:

 

package com.gamefromscratch;

import com.badlogic.gdx.ApplicationAdapter;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.GL20;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.ParticleEffect;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;

public class Particles extends ApplicationAdapter {
   SpriteBatch batch;
   ParticleEffect pe;

   @Override
   public void create () {
      batch = new SpriteBatch();

      pe = new ParticleEffect();
      pe.load(Gdx.files.internal("Particles.party"),Gdx.files.internal(""));
      pe.getEmitters().first().setPosition(Gdx.graphics.getWidth()/2,Gdx.graphics.getHeight()/2);
      pe.start();
   }

   @Override
   public void render () {
      Gdx.gl.glClearColor(0, 0, 0, 1);
      Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);

      pe.update(Gdx.graphics.getDeltaTime());
      batch.begin();
      pe.draw(batch);
      batch.end();
      if (pe.isComplete())
         pe.reset();
   }
}

 

When you run this app you should see:

 

Results

 

It’s pretty consistent with the way most other systems work in Android.  You create the ParticleEffect then load it from file at the root of your assets folder.  We then position the emitter at the center of the screen.  Particle emitters are the source of particles and are often used to control over all behavior.  We then start the particle system running.

 

In render, just like with physics systems, we need to advance the particle effect using update().  This causes the simulation to calculation the position, colour, etc… of all the particles it controls.  Next we draw the ParticleEffect using draw() and drawing to our sprite batch.   Finally we check to see if the effect is complete, and if it is, we restart it.

 

Obviously we glossed over the Particle Editor tool, as well as 3D particle effects, so expect more tutorials soon.

 

Programming ,

blog comments powered by Disqus

Month List

Popular Comments

Troubleshooting IntelliJ/LibGDX/RoboVM/Gradle issues on Mac OS
Subscribe to GameFromScratch on YouTube Support GameFromScratch on Patreon


Home > >

3. April 2014

The move to Gradle has been for the most part a positive experience for me, except when it comes to IntelliJ on Mac OS.  There I ran into a few problems and figured I would share the troubleshooting I took.  Fortunately there was a happy ending for me.

 

The main problem I had was this:

NewImage

 

Before we continue much further, you will need to have a few things installed on your machine.

  • Java 7 JDK
  • Recent ( post April 2014 ) LibGDX nightly
  • IntelliJ 13.1 or higher

If you don’t, please install and configure them now.  Also make sure to set your JAVA_HOME variable after you install the JDK.

 

This error occurred when importing the gradle project into IntelliJ as described in this post.  Generally the most common cause of this error is a lack of Java 7, which RoboVM requires.  It’s important to note, this is the version of Java that IntelliJ needs to run using.  You can check the Java version of IntelliJ via the menu IntelliJ IDEA->About IntelliJ IDEA.  Like so:

IntelliJ About Dialog

 

As you can see, I am running JVM 1.7.0.51, but there is a very good chance that you aren’t even if you have it installed.  JetBrains currently have this to say on the subject:

At the moment all our products require Apple JDK 1.6 to be installed in order to run on Mac. JDK 1.7 from Oracle is not officially supported yet and has known problems that stop us from using it by default. Oracle JDK 1.7.0_40 has added support for Retina and works much better than previous versions on Mac. You are welcome to give it a try in case you have any problems with Apple JDK.

To force running under JDK 1.7 edit /Applications/<Product>.app/Contents/Info.plist file, change JVMVersion from 1.6* to 1.7*

 

So, your first step is to enable Java 1.7 ( Java 7 == Java 1.7, Java 6 == Java 1.6 BTW ). If you are unsure how to access the plist file, in Finder open Applications, locate IntelliJ, right click and select Show Package Contents. If you have Xcode install, you can simply double click the pList file. Edit it like so:

PListEditing

 

Now restart IntelliJ and check the about dialog, you should now be running Java 7.

 

Still not working?  Here are a few quick things to check.

Make sure your JDK is configured correctly.  In a terminal window type:

java -version

The results should look like:

NewImage

If you java version is showing as 1.6, but you’ve installed Java 1.7, chances are your JAVA_HOME variable isn’t set correctly.

 

Next check what gradle sees, change into your project root directory and type:

gradlew -version

The results should look like:

NewImage

Once again, verify that gradle is using the proper JVM.  Once again, I believe this value is driven by JAVA_HOME, but I could be wrong.

 

Finally ( and this was the ultimate problem for me ).  Load IntelliJ and make sure there are no Java 1.6 SDKs defined.  Warning, this will break any old projects that require Java 6 as this is a global setting.

 

In IntelliJ with a Project loaded, right click the project and select Open Module Settings

NewImage

 

First verify that the project SDK is set to 1.7 and then select SDKs from the side bar.

NewImage

 

If a 1.6 SDK exists.  Select it the click the - button.

NewImage

 

Now try the import again and IntelliJ should work just fine.

 

If it still doesn’t work, try running gradle from the command line.  If you get errors there, it’s not an IntelliJ error.   Also, it’s beyond my ability to help you, as troubleshooting Gradle problems is beyond my abilities ( or more accurately desires ).

 

So if you ran into problems getting RoboVM to work with IntelliJ, hopefully this guide proved of some help.

 

 

 

blog comments powered by Disqus

Month List

Popular Comments