Lesson 3: Fun with particles

A particle system is a technique in which we can create specials effects such as fire, snow, rain, and explosion.  You can read more about particle systems at http://en.wikipedia.org/wiki/Particle_system.

Simply put, a particle system is a specialize simulation of individual objects.  Each object is a particle and has the following attributes: location, direction, speed, color, and lifetime.  Each attribute is specified by a range of values instead of a single value.  For example, a particle could go from red to yellow and has a lifetime that is between 10 to 100 frames.

In Env3D, we can create a particle system using the EnvParticle class, as follows:

import env3d.advanced.EnvAdvanced;
import env3d.advanced.EnvParticle;
import env3d.Env;
  
public class Game
{
    public Game() {        
    }
      
    public void play() {        
        // Create a blank environment
        Env env = new EnvAdvanced();
        
        // Create a particle system with 1000 initial particles
        EnvParticle particle = new EnvParticle(1000);
        
        // add the particle to the environment. 
        // The default center point is 0, 0, 0.
        env.addObject(particle);
        
        // position the camera properly
        env.setCameraXYZ(0, 0, 10);
        // Exit when the escape key is pressed
        while (env.getKey() != 1) {
            env.advanceOneFrame();
        }
        env.exit();
    }
    
    public static void main(String [] args) {
        (new Game()).play();
    }
}

The EnvParticle class produces a large flare by default. Try moving the particle object around and see the result.

The default behavior of EnvParticle, while interesting, is very limited. To create more elaborate effects, we'll need to change the parameters of individual particles within the particle system. We can achieve this by accessing the internal JavaMonkey Engine.


JavaMonkey 3D Engine

Env3D uses the JavaMonkey engine for it's 3D rendering.  JavaMonkey is a very powerful modern 3D engine and has lots of built-in features to achieve special effects such as particle systems.  The EnvParticle class is simply a wrapper for the underlying JavaMonkey's ParticleEmitter class.  You can get access to the ParticleEmitter object by calling the EnvParticle.getJmeParticleSystem() method. 

We can make it snow using the following code:

import env3d.advanced.EnvAdvanced;
import env3d.advanced.EnvParticle;
import env3d.Env;

import com.jme3.math.Vector3f;
import com.jme3.math.ColorRGBA;
   
public class Game
{
    public Game() {        
    }
       
    public void play() {        
        // Create a blank environment
        Env env = new EnvAdvanced();
         
        // Create a particle system with 10000 particles
        EnvParticle particle = new EnvParticle(10000);

        // emit in the negative y direction
        particle.getJmeParticleSystem().getParticleInfluencer().setInitialVelocity(new Vector3f(0, -5, 0));
        
        // size of each particle
        particle.getJmeParticleSystem().setStartSize(0.05f);
        particle.getJmeParticleSystem().setEndSize(0.01f);
        // how long does each particle last        
        particle.getJmeParticleSystem().setLowLife(100);
        particle.getJmeParticleSystem().setHighLife(10000);
        // The start and end color, color is (r, g, b, alpha).  
        // alpha = 1 means visible, alpha = 0 means transparent
        particle.getJmeParticleSystem().setStartColor(new ColorRGBA(1, 1, 1, 1));
        particle.getJmeParticleSystem().setEndColor(new ColorRGBA(1, 1, 1, 0.8f));

        // Sets the texture to be a snowfake 
        particle.setTexture("textures/particle/snowflake.png");        
        // put the particle system much higher than the camera
        particle.setY(50);
         
        // add the particle to the environment. 
        // The default center point is 0, 0, 0.
        env.addObject(particle);
                 
        // position the camera to be lower than the particle
        // source
        env.setCameraXYZ(0, 0, 0);
        
        // Exit when the escape key is pressed
        while (env.getKey() != 1) {
            env.advanceOneFrame();
        }
        env.exit();
    }
     
    public static void main(String [] args) {
        (new Game()).play();
    }
}

Run this program and wait a few seconds, you'll see a particles flowing down from a single emission source.  Because of the random nature of each particle, and that each particle has the snow flake texture, the result is what looks like snow to the user.


Exercises

  1. Create a complete outdoor enviroment similar to the following: