Class ParticleEmitter
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 Summary
ConstructorsConstructorDescriptionParticleEmitter(GraphicsDevice device, int maxParticles) Creates an emitter able to holdmaxParticleslive particles. -
Method Summary
Modifier and TypeMethodDescriptionvoidAdds the emitter toview.voidburst(float x, float y, float z, int count) Spawnscountparticles at(x, y, z).voidclear()Removes every live particle.intThe number of live particles.getModel()The model that draws the particles.booleanisActive()True while any particle is alive.voidremoveFrom(GameView view) Removes the emitter fromview.setAdditive(boolean additive) True to add light (fire, sparks), false to alpha blend (smoke, dust).setColor(int argb) Sets the particle color.setDirection(float x, float y, float z, float coneDegrees) Emits withinconeDegreesof the direction(x, y, z); 180 is every direction, 0 a straight jet.setDrag(float drag) The fraction of velocity lost per second.setGravity(float gravity) Acceleration along y in units per second squared; negative falls, positive rises (smoke, embers).setLifetime(float min, float max) The lifetime range in seconds.setSize(float start, float end) The billboard size at birth and at death, in world units.setSpeed(float min, float max) The initial speed range in world units per second.voidupdate(float deltaSeconds, GameCamera camera) Moves the particles bydeltaSeconds, retires the expired ones and turns the rest towardcamera.
-
Constructor Details
-
ParticleEmitter
Creates an emitter able to hold
maxParticleslive 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
The model that draws the particles. -
addTo
Adds the emitter toview. -
removeFrom
Removes the emitter fromview. -
setColor
Sets the particle color. The alpha channel scales the whole effect. -
setAdditive
True to add light (fire, sparks), false to alpha blend (smoke, dust). -
setSpeed
The initial speed range in world units per second. -
setLifetime
The lifetime range in seconds. -
setSize
The billboard size at birth and at death, in world units. -
setGravity
Acceleration along y in units per second squared; negative falls, positive rises (smoke, embers). -
setDrag
The fraction of velocity lost per second. -
setDirection
Emits withinconeDegreesof 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) Spawnscountparticles at(x, y, z). When the pool is full the oldest particles are replaced. -
update
Moves the particles bydeltaSeconds, retires the expired ones and turns the rest towardcamera.
-