Open main menu

CDOT Wiki β

FSOSS 2010/processing.js/psys1

Revision as of 13:19, 27 October 2010 by Asalga (talk | contribs) (Created page with '<source lang="JavaScript"> FSOSS 2010 Andor Salga: import processing.opengl.*; class Particle{ PVector position; PVector velocity; float age; float lifeTime;…')
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
/*
  FSOSS 2010
  Andor Salga
*/

import processing.opengl.*;

class Particle{
  PVector position;
  PVector velocity;
 
  float age;
  float lifeTime;
  // opacity (add me!)
  // size (add me!)
 
  Particle(){
    position = new PVector(0, 0, 0);
    velocity = new PVector(1.5, 1.2, 2.0);
   
    age = 0;
    lifeTime = 0;
  }

  void reset(){
    age = 0; 
  }
 
  float getAge(){return age;}
  float getLifeTime(){return lifeTime;}
 
  void setAge(float a){age = a;}
  void setPosition(PVector pos){position = pos;}
  void setVelocity(PVector vel){velocity = vel;}
  void setLifeTime(float l){lifeTime = l;}
 
  void update(){
    age += 0.1; //fix
   
    velocity.y += 0.1;
   
    position.add(velocity);
  }
 
  void draw(){
    strokeWeight(5);
    stroke(128, 0, 0);
    point(position.x, position.y, position.z);
  }
}

int NUM_PARTICLES = 500;

class ParticleSystem{
  ArrayList p;

  ParticleSystem(){
    p = new ArrayList();
    for(int i = 0; i < NUM_PARTICLES; i++){
      Particle particle = new Particle();
      p.add(particle);
      resetParticle(i);
    }
  }
 
  void resetParticle(int i){
    Particle particle = (Particle)p.get(i);
    particle.reset();
   
    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 update(){
    for(int i = 0; i < NUM_PARTICLES; i++){
      Particle particle = (Particle)p.get(i);
      particle.update();
      if(particle.getAge() > particle.getLifeTime()){
        resetParticle(i);
      }
    }
  }
 
  void draw(){
    for(int i = 0; i < NUM_PARTICLES; i++){
      Particle particle = (Particle)p.get(i);
      particle.draw();
    }
  }
}

ParticleSystem psys;

void setup(){
  size(500,500,OPENGL);
  psys = new ParticleSystem();
}

void draw(){
  background(0);
  stroke(255);
  psys.update();
  psys.draw();
}

Run me