Open main menu

CDOT Wiki β

Changes

FSOSS 2010/processing.js/psys1

123 bytes added, 16:00, 27 October 2010
no edit summary
<source lang="JavaScript">
/*
Andor Salga
FSOSS 2010
Andor SalgaExample of Particle System
*/
 
import processing.opengl.*;
/*
A single particle
*/
class Particle{
private PVector position; private PVector velocity; float age; float lifeTime; // opacity (add me!) // size (add me!)
private float age;
private float lifeTime;
// !!! color Add me!
// !!! size Add me!
 
Particle(){
position = new PVectorreset(0, 0, 0); velocity = new PVector(1.5, 1.2, 2.0); age = 0; lifeTime = 0;
}
void reset(){
position = new PVector(0, 0, 0); velocity = new PVector(0, 0, 0); age = 0.0f; lifeTime = 0.0f;
}
void setLifeTime(float l){lifeTime = l;}
// !!! Pass in elapsed time
void update(){
  age += 0.1; //!!! fix me velocity.y += 0.1; // !!! fix me
position.add(velocity);
 
// !!! update color
// !!! update size
}
void draw(){
strokeWeight(510); stroke(128, 0, 0, 50);
point(position.x, position.y, position.z);
}
int NUM_PARTICLES = 500;
/*
Class that manages the particles
*/
class ParticleSystem{
ArrayList p;
p = new ArrayList();
for(int i = 0; i < NUM_PARTICLES; i++){
Particle particle = new Particle(); p.add(particlenew Particle(); resetParticle(i);
}
}
particle.setPosition( new PVector(mouseX, mouseY, 0));
particle.setVelocity( new PVector( random(0,2), random(0,2) , 0 ) );
particle.setLifeTime(random(1,15));
particle.setAge(0);
}
void setup(){
size(500,500,OPENGL);
psys = new ParticleSystem();
}
void draw(){
background(0);
stroke(255);
psys.update();
psys.draw();
}
</source>
[http://studio.sketchpad.cc/MAMxiAxlxH EtmcGMo6zi Run me]
1
edit