Class ParticleEmitter

java.lang.Object
com.codename1.gaming.ParticleEmitter

public class ParticleEmitter extends Object

Camera facing particles for 3D effects: explosions, sparks, dust, magic. Every particle of an emitter is drawn in one call as a soft round billboard that brightens in, fades out and changes size over its life.

An emitter is a pool of at most maxParticles live particles. burst spawns a batch at a point; update moves them and turns them toward the camera. Add it to a view with addTo(GameView) and keep it for the life of the scene -- an emitter with no live particles draws nothing visible.

import com.codename1.gaming.*;
import com.codename1.gpu.*;
import com.codename1.ui.*;
import com.codename1.ui.layouts.BorderLayout;

class Campfire extends GameView {
    private ParticleEmitter fire;

    protected void onSetup(GraphicsDevice device) {
        fire = new ParticleEmitter(device, 64)
                .setColor(0xffff8a2a).setAdditive(true)
                .setSpeed(1.5f, 4f).setLifetime(0.3f, 0.7f).setSize(0.6f, 1.6f)
                .setGravity(-2f);
        fire.addTo(this);
    }

    protected void update(double deltaSeconds) {
        if (fire != null) {
            fire.burst(0f, 0.5f, 0f, 2);
            fire.update((float) deltaSeconds, getCamera());
        }
    }
}

Form f = new Form("Campfire", new BorderLayout());
f.add(BorderLayout.CENTER, new Campfire());
f.show();

Additive emitters suit light (fire, sparks, magic); alpha blended ones suit matter (smoke, dust, debris).

  • Constructor Details

    • ParticleEmitter

      public ParticleEmitter(GraphicsDevice device, int maxParticles)

      Creates an emitter able to hold maxParticles live particles.

      Parameters
      • device: the device that will draw it; uploads the particle texture

      • maxParticles: the pool size; a burst beyond it reuses the oldest

  • Method Details

    • getModel

      public Model getModel()
      The model that draws the particles.
    • addTo

      public void addTo(GameView view)
      Adds the emitter to view.
    • removeFrom

      public void removeFrom(GameView view)
      Removes the emitter from view.
    • setColor

      public ParticleEmitter setColor(int argb)
      Sets the particle color. The alpha channel scales the whole effect.
    • setAdditive

      public ParticleEmitter setAdditive(boolean additive)
      True to add light (fire, sparks), false to alpha blend (smoke, dust).
    • setSpeed

      public ParticleEmitter setSpeed(float min, float max)
      The initial speed range in world units per second.
    • setLifetime

      public ParticleEmitter setLifetime(float min, float max)
      The lifetime range in seconds.
    • setSize

      public ParticleEmitter setSize(float start, float end)
      The billboard size at birth and at death, in world units.
    • setGravity

      public ParticleEmitter setGravity(float gravity)
      Acceleration along y in units per second squared; negative falls, positive rises (smoke, embers).
    • setDrag

      public ParticleEmitter setDrag(float drag)
      The fraction of velocity lost per second.
    • setDirection

      public ParticleEmitter setDirection(float x, float y, float z, float coneDegrees)
      Emits within coneDegrees of the direction (x, y, z); 180 is every direction, 0 a straight jet.
    • getLiveCount

      public int getLiveCount()
      The number of live particles.
    • isActive

      public boolean isActive()
      True while any particle is alive.
    • clear

      public void clear()
      Removes every live particle.
    • burst

      public void burst(float x, float y, float z, int count)
      Spawns count particles at (x, y, z). When the pool is full the oldest particles are replaced.
    • update

      public void update(float deltaSeconds, GameCamera camera)
      Moves the particles by deltaSeconds, retires the expired ones and turns the rest toward camera.