gamvas.ParticleEmitter

Description

A particle emitter

The particle emitter allows you to achive many special effects like smoke, fire, rain and similar.  It can emitt images or animations.

The emitter extends the gamvas.Actor class and can therefor be added with gamvas.State.addActor for automatic drawing

Contructor

new gamvas.ParticleEmitter(name, x, y, img, anim);

Parameters

namea unique name
x/ythe position of the emitter
imga gamvas.Image or gamvas.Animation instance
animtrue/false, if img is a gamvas.Animation instead of gamvas.Image

Note

The emitter provides a number of function named set{value} with a corresponding set{value}Range function.  The range specifies a range of possible values around the non range value.  This allows you to bring variance in the particle emission and therefor realism.

For example, you could use emitter.setParticleSpeed(150); with emitter.setParticleSpeedRange(50); This would end up with the particles being generated with speeds between 125 to 175 pixels per second.

Example

myEmitter = gamvas.ParticleEmitter.extend({
    onParticleEnd: function(pos, rot, scale, vel) {
        concole.log('rest in peace, particle at '+pos.x+','+pos.y);
    }
});

myState = gamvas.State.extend({
   init: function() {
      var em = new myEmitter('smoke', 0, 0, new gamvas.Image(this.resource.getImage('smoke.png')));
      em.setParticleRate(20);
      em.setRotationRange(Math.PI*0.1);
      this.addActor(em);
   },
});
Summary
gamvas.ParticleEmitterA particle emitter
Variables
nameThe name of the particle emitter
lifetimeThe time the emitter is running
emittedThe amount of emitted particles
positionA gamvas.Vector2D object with the position information
centerA gamvas.Vector2D object with the center offset of the particle emitter
rotationThe rotation of the particle emitter
scaleFactorThe scale factor of the object
Functions
setImageSet a gamvas.Image as Particle
setAnimationSet a gamvas.Animation as Particle
setAnimationLifeTimeSet if the animation should be played once for the lifetime
setRotationSet the rotation of the nozzle
setRotationRangeSet the angle around rotation how particles are emitted from the nozzle
setParticleRateSet the rate the particles are emitted
getParticleRateGet the current particle emission rate
setParticleRateRangeSet the rate range particles are emitted
setParticleLimitSet the maximum number of particles that will be emitted
alignParticleToPathIf true, align particle orientation along their movement
setParticleRotationSet the particles starting rotation
setParticleRotationRangeSet the range around particles starting rotation
setParticleRotationVelocityThe rotational velocity of created particles
setParticleRotationVelocityRangeThe range of the rotational velocity
setParticleScaleSet the scale of newly created particles
setParticleScaleRangeSet the range of scale
setParticleSpeedSet the initial speed of created particles
setParticleSpeedRangeSet the range around the initial speed of created particles
setParticleLifeTimeSet the lifetime of new particles
setParticleLifeTimeRangeSet the range around the lifetime of new particles
setParticleVelocityDampingSet how much the velocity is slowed down over time
setParticleVelocityDampingRangeSet the range around velocity damping
setParticleRotationDampingSet how much the rotation is slowed down over time
setParticleRotationDampingRangeSet range around the rotation damping
setParticleStartPositionRangeSets the area where particles are created
setGravitySet the gravity that affects the particle emitter
setScaleTableSet the scale over lifetime
setAlphaTableSet the alpha (transparency) over lifetime
setSpeedScaleTableSet the scale depending on particle speed
drawDraw the particle emitter
resetReset the particle emitter
onParticleEndOverwrite this function to do something everytime a particle is removed
setRotationSet certain rotation of the particle emitter in radians
rotateRotate the particle emitter
setPositionSet the position of a particle emitter
moveMove the particle emitter
setScaleSet a certain scale factor
scaleScale a object

Variables

name

The name of the particle emitter

lifetime

The time the emitter is running

emitted

this.emitted

The amount of emitted particles

position

center

A gamvas.Vector2D object with the center offset of the particle emitter

See

<gamvas.ParticleEmitter.setCenter>

rotation

scaleFactor

The scale factor of the object

Functions

setImage

setImage: function(img)

Description

Set a gamvas.Image as Particle

setAnimation

setAnimation: function(anim)

Description

Set a gamvas.Animation as Particle

setAnimationLifeTime

setAnimationLifeTime: function(yesno)

Description

Set if the animation should be played once for the lifetime

If set to true, the Animation plays once from start to end until the particle dies, otherwise the animation plays with whatever it has set as FPS

Paramters

yesnotrue/false, true = play once over lifetime, false = play as specified by FPS

setRotation

Description

Set the rotation of the nozzle

Parameters

rthe rotation in radians

setRotationRange

setRotationRange: function(r)

Description

Set the angle around rotation how particles are emitted from the nozzle

Parameters

rthe range around the center (half plus, half minus)

setParticleRate

setParticleRate: function(r)

Description

Set the rate the particles are emitted

Parameters

rthe rate in particles per second

getParticleRate

getParticleRate: function()

Description

Get the current particle emission rate

setParticleRateRange

setParticleRateRange: function(r)

Description

Set the rate range particles are emitted

Parameters

rthe range around the center (half plus, half minus)

setParticleLimit

setParticleLimit: function(l)

Description

Set the maximum number of particles that will be emitted

Parameters

lthe particle limit

alignParticleToPath

alignParticleToPath: function(yesno)

Description

If true, align particle orientation along their movement

Parameters

yesnotrue/false if to set the particle orientation along its path

setParticleRotation

setParticleRotation: function(r)

Description

Set the particles starting rotation

Parameters

rthe rotation in radians

setParticleRotationRange

setParticleRotationRange: function(r)

Description

Set the range around particles starting rotation

Parameters

rthe range of variance around the particles rotation

setParticleRotationVelocity

setParticleRotationVelocity: function(r)

Description

The rotational velocity of created particles

Defines how much they continue to rotate while moving

Parameters

rthe rotational velocity in radians per second

setParticleRotationVelocityRange

setParticleRotationVelocityRange: function(r)

Description

The range of the rotational velocity

Parameters

rthe range around the rotational velocity in radians per second

setParticleScale

setParticleScale: function(s)

Description

Set the scale of newly created particles

Parameters

sthe scale factor (1 = original, < 1 = smaller, > 1 = bigger)

setParticleScaleRange

setParticleScaleRange: function(s)

Description

Set the range of scale

Parameters

sthe range around the scale factor

setParticleSpeed

setParticleSpeed: function(s)

Description

Set the initial speed of created particles

Parameters

sthe speed in pixels per second

setParticleSpeedRange

setParticleSpeedRange: function(s)

Description

Set the range around the initial speed of created particles

Parameters

sthe range around the speed in pixels per second

setParticleLifeTime

setParticleLifeTime: function(l)

Description

Set the lifetime of new particles

Parameters

lthe lifetime in seconds

setParticleLifeTimeRange

setParticleLifeTimeRange: function(l)

Description

Set the range around the lifetime of new particles

Parameters

lthe range around the lifetime in seconds

setParticleVelocityDamping

setParticleVelocityDamping: function(v)

Description

Set how much the velocity is slowed down over time

Parameters

vthe damping value, between 0 (none) and 1 (immediate stop)

setParticleVelocityDampingRange

setParticleVelocityDampingRange: function(v)

Description

Set the range around velocity damping

Parameters

vthe velocity damping range

setParticleRotationDamping

setParticleRotationDamping: function(r)

Description

Set how much the rotation is slowed down over time

Parameters

rthe damping value, between 0 (none) and 1 (immediate stop)

setParticleRotationDampingRange

setParticleRotationDampingRange: function(r)

Description

Set range around the rotation damping

Parameters

rthe range around the rotation damping

setParticleStartPositionRange

setParticleStartPositionRange: function(s)

Description

Sets the area where particles are created

By default every particle is created exactly at the position of the particle emitter, by setting a start position range, you can define a rectangle where the particles are created

Parameters

sthe starting position range as gamvas.Vector2D

setGravity

setGravity: function(g)

Description

Set the gravity that affects the particle emitter

Parameters

sthe gravity in pixels per second as gamvas.Vector2D

Note

Gravity is in pixels per second, other then in real life, if you want your particles to fall down, you specify a positive Y value

setScaleTable

setScaleTable: function(scaleTable)

Description

Set the scale over lifetime

Parameters

scaleTablea array with scale factors over the particles lifetime

The array has to be made the following way

It has a index (first value) between 0 and 1 where 0 is when the particle is created and 1 is when it is destroyed.  You can add values between them feely, but you have to ensure, that there is a 0 and 1 index.

The following would be a table that scales from 0 to original size at the half, and then back to 0 at end:

[ [0, 0], [0.5, 1], [1, 0] ]

See

gamvas.ParticleEmitter.setSpeedScaleTable

setAlphaTable

setAlphaTable: function(alphaTable)

Description

Set the alpha (transparency) over lifetime

Parameters

alphaTablea array with alpha values over the particles lifetime

The array has to be made the following way

It has a index (first value) between 0 and 1 where 0 is when the particle is created and 1 is when it is destroyed.  You can add values between them feely, but you have to ensure, that there is a 0 and 1 index.

The following would be a table that alpha from 0 to original size, then quite early it gets full opacity, and then fades out slowly to the end:

[ [0, 0], [0.1, 1], [1, 0] ]

setSpeedScaleTable

setSpeedScaleTable: function(spdScaleTable)

Description

Set the scale depending on particle speed

Parameters

spdScaleTablea array with scale factors for x/y depending on particle speed

The array has to be made the following way

It has a index (first value) which is the speed of the particle in pixels per second, you should have at least a speed of 0 and some speed bigger then 0

Following the index, there are two values representing the x and y scale factor where 1 is original size, smaller then 1 is smaller and higher then 1 is bigger

The following would be a table that scales the particle very small when it is slow, then when moving at medium speed, it scales it to original size, wen moving fast it streches it by reducing the x scale and significantly increasing the y scale.

[ [0, 0.1, 0.1], [30, 1, 1], [100, 0.2, 3] ]

Note

This is usually used with gamvas.ParticleEmitter.alignParticleToPath, as x and y are regarding to the particles original orientation

See

gamvas.ParticleEmitter.setScaleTable

draw

draw: function(t)

Description

Draw the particle emitter

Paramters

tthe time since last redraw in seconds

Note

Particle emitter extends gamvas.Actor and therefor can be added via gamvas.State.addActor for automatic drawing

reset

reset: function(kill)

Description

Reset the particle emitter

Parameters

killtrue/false if old particles should be destroyed (optional)

onParticleEnd

onParticleEnd: function(pos,
rot,
scale,
vel)

Description

Overwrite this function to do something everytime a particle is removed

Parameters

posthe position of the destroyed particle as gamvas.Vector2D
rotthe rotation in radians
scalethe scale of the particle
velthe velocity of the particle as gamvas.Vector2D

setRotation

Description

Set certain rotation of the particle emitter in radians

Parameters

rthe rotation in radians

See

gamvas.ParticleEmitter.rotate http://en.wikipedia.org/wiki/Radians

rotate

Description

Rotate the particle emitter

Parameters

rthe amount to rotate the particle emitter in radians

See

gamvas.ParticleEmitter.setRotation http://en.wikipedia.org/wiki/Radians

setPosition

Description

Set the position of a particle emitter

Parameters

x/ythe position of the particle emitter in pixels

See

gamvas.ParticleEmitter.move

move

Description

Move the particle emitter

Parameters

x/ythe pixels to move the particle emitter

See

gamvas.ParticleEmitter.setPosition

setScale

Description

Set a certain scale factor

Parameters

sthe scale value (1 = no scale, < 1 = smaller, > 1 = bigger)

See

gamvas.ParticleEmitter.scale

scale

Description

Scale a object

Note

Parameters

sthe scale factor (< 0 = shrink, > 0 = enlarge)

See

gamvas.ParticleEmitter.setScale

this.emitted
The amount of emitted particles
A 2D vector class
setImage: function(img)
Set a gamvas.Image as Particle
A plain image with methods to move, rotate and zoom.
setAnimation: function(anim)
Set a gamvas.Animation as Particle
Class for animated sprites
setAnimationLifeTime: function(yesno)
Set if the animation should be played once for the lifetime
setRotationRange: function(r)
Set the angle around rotation how particles are emitted from the nozzle
setParticleRate: function(r)
Set the rate the particles are emitted
getParticleRate: function()
Get the current particle emission rate
setParticleRateRange: function(r)
Set the rate range particles are emitted
setParticleLimit: function(l)
Set the maximum number of particles that will be emitted
alignParticleToPath: function(yesno)
If true, align particle orientation along their movement
setParticleRotation: function(r)
Set the particles starting rotation
setParticleRotationRange: function(r)
Set the range around particles starting rotation
setParticleRotationVelocity: function(r)
The rotational velocity of created particles
setParticleRotationVelocityRange: function(r)
The range of the rotational velocity
setParticleScale: function(s)
Set the scale of newly created particles
setParticleScaleRange: function(s)
Set the range of scale
setParticleSpeed: function(s)
Set the initial speed of created particles
setParticleSpeedRange: function(s)
Set the range around the initial speed of created particles
setParticleLifeTime: function(l)
Set the lifetime of new particles
setParticleLifeTimeRange: function(l)
Set the range around the lifetime of new particles
setParticleVelocityDamping: function(v)
Set how much the velocity is slowed down over time
setParticleVelocityDampingRange: function(v)
Set the range around velocity damping
setParticleRotationDamping: function(r)
Set how much the rotation is slowed down over time
setParticleRotationDampingRange: function(r)
Set range around the rotation damping
setParticleStartPositionRange: function(s)
Sets the area where particles are created
setGravity: function(g)
Set the gravity that affects the particle emitter
setScaleTable: function(scaleTable)
Set the scale over lifetime
setAlphaTable: function(alphaTable)
Set the alpha (transparency) over lifetime
setSpeedScaleTable: function(spdScaleTable)
Set the scale depending on particle speed
draw: function(t)
Draw the particle emitter
reset: function(kill)
Reset the particle emitter
onParticleEnd: function(pos,
rot,
scale,
vel)
Overwrite this function to do something everytime a particle is removed
The actor class is the most important class in gamvas.
addActor: function(act)
Add a gamvas.Actor to the state.
Set the position of a particle emitter
Move the particle emitter
Set the rotation of the nozzle
Rotate the particle emitter
Scale a object
Set a certain scale factor
Close