Difference between revisions of "FSOSS 2010/processing.js/psys1"

From CDOT Wiki
Jump to: navigation, search
(Created page with '<source lang="JavaScript"> FSOSS 2010 Andor Salga: import processing.opengl.*; class Particle{ PVector position; PVector velocity; float age; float lifeTime;…')
 
 
Line 1: Line 1:
 
<source lang="JavaScript">
 
<source lang="JavaScript">
 
/*
 
/*
 +
  Andor Salga
 
   FSOSS 2010
 
   FSOSS 2010
   Andor Salga
+
   Example of Particle System
 
*/
 
*/
 
 
import processing.opengl.*;
 
import processing.opengl.*;
  
 +
/*
 +
  A single particle
 +
*/
 
class Particle{
 
class Particle{
   PVector position;
+
   private PVector position;
   PVector velocity;
+
   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(){
 
   Particle(){
     position = new PVector(0, 0, 0);
+
     reset();
    velocity = new PVector(1.5, 1.2, 2.0);
 
 
 
    age = 0;
 
    lifeTime = 0;
 
 
   }
 
   }
  
 
   void reset(){
 
   void reset(){
     age = 0;  
+
    position = new PVector(0, 0, 0);
 +
    velocity = new PVector(0, 0, 0);
 +
     age = 0.0f;
 +
    lifeTime = 0.0f;
 
   }
 
   }
 
   
 
   
Line 36: Line 38:
 
   void setLifeTime(float l){lifeTime = l;}
 
   void setLifeTime(float l){lifeTime = l;}
 
   
 
   
 +
  // !!! Pass in elapsed time
 
   void update(){
 
   void update(){
     age += 0.1; //fix
+
 
 
+
     age += 0.1; // !!! fix me
     velocity.y += 0.1;
+
     velocity.y += 0.1; // !!! fix me
 
 
 
     position.add(velocity);
 
     position.add(velocity);
 +
 +
    // !!! update color
 +
    // !!! update size
 
   }
 
   }
 
   
 
   
 
   void draw(){
 
   void draw(){
     strokeWeight(5);
+
     strokeWeight(10);
     stroke(128, 0, 0);
+
     stroke(128, 0, 0, 50);
 
     point(position.x, position.y, position.z);
 
     point(position.x, position.y, position.z);
 
   }
 
   }
Line 53: Line 58:
 
int NUM_PARTICLES = 500;
 
int NUM_PARTICLES = 500;
  
 +
/*
 +
  Class that manages the particles
 +
*/
 
class ParticleSystem{
 
class ParticleSystem{
 
   ArrayList p;
 
   ArrayList p;
Line 59: Line 67:
 
     p = new ArrayList();
 
     p = new ArrayList();
 
     for(int i = 0; i < NUM_PARTICLES; i++){
 
     for(int i = 0; i < NUM_PARTICLES; i++){
      Particle particle = new Particle();
+
       p.add(new Particle());
       p.add(particle);
 
      resetParticle(i);
 
 
     }
 
     }
 
   }
 
   }
Line 70: Line 76:
 
    
 
    
 
     particle.setPosition( new PVector(mouseX, mouseY, 0));
 
     particle.setPosition( new PVector(mouseX, mouseY, 0));
     particle.setVelocity( new PVector( random(0,2), random(0,2) , 0 ) );
+
     particle.setVelocity( new PVector(random(0,2), random(0,2), 0) );
 
     particle.setLifeTime(random(1,15));
 
     particle.setLifeTime(random(1,15));
    particle.setAge(0);
 
 
   }
 
   }
 
   
 
   
Line 96: Line 101:
  
 
void setup(){
 
void setup(){
   size(500,500,OPENGL);
+
   size(500, 500, OPENGL);
 
   psys = new ParticleSystem();
 
   psys = new ParticleSystem();
 
}
 
}
Line 102: Line 107:
 
void draw(){
 
void draw(){
 
   background(0);
 
   background(0);
  stroke(255);
 
 
   psys.update();
 
   psys.update();
 
   psys.draw();
 
   psys.draw();
 
}
 
}
 
</source>
 
</source>
[http://studio.sketchpad.cc/MAMxiAxlxH Run me]
+
[http://studio.sketchpad.cc/EtmcGMo6zi Run me]

Latest revision as of 16:00, 27 October 2010

/*
  Andor Salga
  FSOSS 2010
  Example of Particle System
*/
import processing.opengl.*;

/*
  A single particle
*/
class Particle{
  private PVector position;
  private PVector velocity;
 
  private float age;
  private float lifeTime;
  // !!! color Add me!
  // !!! size Add me!

  Particle(){
    reset();
  }

  void reset(){
    position = new PVector(0, 0, 0);
    velocity = new PVector(0, 0, 0);
    age = 0.0f;
    lifeTime = 0.0f;
  }
 
  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;}
 
  // !!! 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(10);
    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;

  ParticleSystem(){
    p = new ArrayList();
    for(int i = 0; i < NUM_PARTICLES; i++){
      p.add(new Particle());
    }
  }
 
  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));
  }
 
  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);
  psys.update();
  psys.draw();
}

Run me