giovedì 28 giugno 2012

Processing Particle System

Today I'm posting about a really simple particle system made using processing, here is the code:

Particle particle;

void setup(){
  size(400, 400);
  particle = new Particle(70000, width/2, height/2, color(0, 0, 255));
  particle.generate();
  }

void draw(){
  background(255);
  particle.setDir(mouseX, mouseY);
  particle.update();
  }

//here is the particle system
class Particle{
  int qnt; //Particles count
  float dirx, diry;
  float[] xpos;
  float[] ypos;
  color c;
  Particle(int q, int dx, int dy, color col){ 
    qnt = q;
    dirx = dx;
    diry = dy;
    xpos = new float[q+1];
    ypos = new float[q+1];
    c = col;
    }
  void generate(){                            
    for(int i = 0; i<= qnt; i++){
      xpos[i] = random(0, width);
      ypos[i] = random(0, height);
      stroke(c);
      point(xpos[i], ypos[i]);
      }
    }
  void update(){                            
    for(int i = 0; i<= qnt; i++){
      float spd = random(0, 20);
      float rnd = random(0, 20);            
      if(xpos[i] < dirx-rnd){
        xpos[i] += spd;
        }
      if(xpos[i] > dirx+rnd){
        xpos[i] -= spd;
        }
      if(ypos[i] < diry-rnd){
        ypos[i] += spd;
        }
      if(ypos[i] > diry+rnd){
        ypos[i] -= spd;
        }
      stroke(c);
      point(xpos[i], ypos[i]);
      }
    }
  void setDir(float xd, float yd){
    dirx = xd;
    diry = yd;
    }
  }


As you can see it's really simple (no physics)  but it's also simple to edit it so I think it should be useful.
Here is the link for downloading the code(.zip)  and here is the link for the webdemo(processing.js).

I hope you like this post!

Bye, Dami

Nessun commento:

Posta un commento