# Particle Emmitter Settings

You cannot create a 3D model for delicate things like fire, smoke, or explosions. Particle Emitters are quite an efficient solution to create these kinds of effects: The emitter renders a series of flat orthogonal images and manipulates them in a way that creates the illusion of a anything from a delicate smoke cloud to individual flames, etc.

Creating an effect involves some trial and error to get the settings just right, and it's worth exploring the expressiveness of the options described below. Tip: Use the Scene Editor in the jMonkeyPlatform to design and preview effects.

## Create an Emitter

1. Create one emitter for each effect:

       ParticleEmitter explosion = new ParticleEmitter(
       "My explosion effect", ParticleMesh.Type.Triangle, 30);

2. Attach the emitter to the rootNode and position it in the scene:

       rootNode.attachChild(explosion);
       explosion.setLocalTranslation(bomb.getLocalTranslation());

3. Trigger the effect by calling

       explosion.emitAllParticles()

4. End the effect by calling

       explosion.killAllParticles()

Choose one of the following mesh shapes

- ParticleMesh.Type.Triangle
- ParticleMesh.Type.Point

## Configure Parameters

Not all of these parameters are required for all kinds of effects. If you don't specify one of them, a default value will be used.

Parameter | Method | Default | Description
--- | --- | --- | ---
number | `setNumParticles()` |  | The maximum number of particles visible at the same time. Specified by user in constructor.
emission rate | `setParticlesPerSec()` | 20 | Density of the effect, how many new particles are emitted per second. Set to zero to control the start of the effect. Set to a number for a constantly running effect.
size | `setStartSize()`, `setEndSize()` | 0.2f, 2f | Set both to same value for constant size effect. Set to different values for shrink/grow effect.
color | `setStartColor()`, `setEndColor()` | gray, darkgray | Set both to the same color for single-colored effects (e.g. fog). Set both to different colors for a gradient effect (e.g. fire).
velocity/direction | `setInitialVelocity()` | Vector3f(0,0,0) | A vector specifying how fast or slow particles fly, and it which direction.
randomness | `setVelocityVariation()` | 0.2f | How much the direction/speed (`setInitialVelocity()`) can vary. 1 = Maximum variation (particles emit in random directions) 0 = No variation (particles fly straight with start velocity only).
direction | `setFacingVelocity()` | false | true = Flying particles pitch in the direction they're flying (e.g. missiles). false = Particles keep flying rotated the way they started (e.g. debris).
direction | `setRandomAngle()` | false | true = Flying particle should face at a random angle (e.g. explosion). false = Flying particle flies straight.
direction | `setFaceNormal()` | Vector3f.NAN | Vector3f = Flying particles face in the given direction. Vector3f.NAN = Flying particles face the camera.
lifetime | `setLowLife()` | 3f | Minimum time period before particles fade
lifetime | `setHighLife()` | 7f | Maximum time period before particles fade
rotation | `setRotateSpeed()` | 0f | 0 = Flying particles don't spin. > 0 = How fast particle spins while flying.
gravity | `setGravity()` | 0.1f | >0 = Particles fall "down" (e.g. debris, sparks). 0.0f = Particles keep flying (e.g. flames, zero g explosion.)

Build up you effect by specifying one parameter after the other. If you change several parameters at the same time, it's difficult to tell which of the values caused which outcome.

## Create an Effect Material

Use the common Particle.j3md Material Definition to specify the shape of the particles. The shape is only limited by the texture you provide and can be anything – debris, flames, smoke, mosquitoes, leaves, butterflies… be creative.

        Material mat_flash = new Material(
            assetManager, "Common/MatDefs/Misc/Particle.j3md");
        mat_flash.setTexture("Texture",
            assetManager.loadTexture("Effects/Explosion/flash.png"));
        flash.setMaterial(debris_mat);
        flash.setImagesX(2); // columns
        flash.setImagesY(2); // rows
        flash.setSelectRandomImage(true);

The effect texture can contain Sprite animations – a series of different pictures in equally spaced rows and columns.

- Specify the number of rows and columns
- Specify whether you want to play the series in order or at random.

Have a look at the following default textures and you will see that you can easily create your own Sprite textures after the same fashion.

### Default Particle Textures

The Material is used together with grayscale texture: The black parts will be transparent and the white parts will be opaque.

The following effect textures are available by default from `test-data.jar`. You can also load your own textures from your assets directory.

Texture Path | Dimension | Preview
--- | --- | ---
Effects/Explosion/Debris.png | 3*3 |
Effects/Explosion/flame.png | 2*2 |
Effects/Explosion/flash.png | 2*2 |
Effects/Explosion/roundspark.png | 1*1 |
Effects/Explosion/shockwave.png | 1*1 |
Effects/Explosion/smoketrail.png | 1*3 |
Effects/Explosion/spark.png | 1*1 |
Effects/Smoke/Smoke.png | 1*15 |

Tip: Use the `setStartColor()`/`setEndColor()` settings described above to colorize the textures.

## Usage Example

        ParticleEmitter fire = new ParticleEmitter("Emitter", ParticleMesh.Type.Triangle, 30);
        Material mat_red = new Material(assetManager, "Common/MatDefs/Misc/Particle.j3md");
        mat_red.setTexture("Texture", assetManager.loadTexture("Effects/Explosion/flame.png"));
        fire.setMaterial(mat_red);
        fire.setImagesX(2); fire.setImagesY(2); // 2x2 texture animation
        fire.setEndColor(  new ColorRGBA(1f, 0f, 0f, 1f));   // red
        fire.setStartColor(new ColorRGBA(1f, 1f, 0f, 0.5f)); // yellow
        fire.setInitialVelocity(new Vector3f(0, 2, 0));
        fire.setStartSize(1.5f);
        fire.setEndSize(0.1f);
        fire.setGravity(0);
        fire.setLowLife(0.5f);
        fire.setHighLife(3f);
        fire.setVelocityVariation(0.3f);
        rootNode.attachChild(fire);

Browse the full source code of all effect examples here.

See also: Effects Overview

view online version