Win an Ipad
  Developer   Documents   Particle System

Particle System

Particle system is a graphic library which can be used in games to achieve nice effects like fire, sparks, waterfalls, clouds, etc.

A particle system is a collection of independent objects, often represented by a simple shape or dot. It can be used for modeling many irregular types of natural phenomena, such as explosions, fire, smoke, sparks, waterfalls, clouds, fog, petals, grass, bubbles, and so on. In a system, each particle has its own set of properties related to its behavior (for example, velocity, acceleration, etc.) as well as its look (for example, color, shape, image, etc.).

The most important thing to be defined is an Emitter. Emitter has position and velocity defined as a Vector with X and Y coordinates, particles spread and emission rate. Particle attributes include particle size, particle life and maximum amount of particles defined in Emitter instance.

If parameter particleLife is set to -1, particle never dies.


Let’s have an example of emitter with particles thrown from the middle of the screen to the side.


var emitter = new Emitter({
    position:new Vector({x:System.width/2, y:System.height/2}),
    velocity:new Vector({x:2, y:0}),
    size:8, particleLife: 100, spread:Math.PI/10, emissionRate:4, maxParticles:1000
});

 

Emitter by default represents the particle as a dot. Particle can be any class extended from Game2D library.


Particle class:

Following example shows that particle can be a bitmap with some nice effects. First we define class MyParticle, where bitmap and Moscrif paint instance for particle decoration is defined and own draw method.

include "lib://game2d/particle/particle.ms" //class MyParticle - used by cloud effect emitter
class MyParticle : Particle
{
    var paint = null;
    var img = Bitmap.fromFile("app://fire.png");     function init()
    {         super.init();             MyParticle.paint = new Paint();
            MyParticle.paint.alpha = 80;
            MyParticle.paint.colorFilter = ColorFilter.lighting(0x00ffff00, 0x00000000);             //set particle scale randomly, it makes nice effect
            this._scale = 0.5 + rand(200) / 100.0;
            this._matrix = new Matrix();
            this._matrix.setScale(this._scale, this._scale);
    }     function draw(canvas)
    {
        this._matrix.translateX = this._position.x;
        this._matrix.translateY = this._position.y;
        canvas.drawBitmapMatrix(MyParticle.img, this._matrix, MyParticle.paint);
    }
}
//particle system emiter
var emitter = new Emitter({
    position:new Vector({x:System.width/2, y:System.height/2}),
    velocity:new Vector({x:5, y:0}),
    size:7,
    particleLife: -1,
    spread:Math.PI/2.0,
    emissionRate:4,
    maxParticles:250,
    particleClass: MyParticle
}); //particle system field
var field = new Field({
    position:new Vector({x:System.width/2, y:System.height/2}),
    mass:200
});

 Scene Transitions   Particle System   Application 
Write a Comment (0)
Subject
Please complete this mandatory field.
HTML Tags Not Allowed!
Comment
Please complete this mandatory field.