Daily Creative Coding

元「30 min. Processing」。毎日、Creative Codingします。

ランダム・加速度・ウォーカー

/**
* random acceleration walkers
*
* @author aa_debdeb
* @date 2016/03/02
*/

ArrayList<Particle> particles;

void setup(){
  size(640, 640);
  frameRate(60);
  
  particles = new ArrayList<Particle>();
  for(int i = 0; i < 100; i++){
    particles.add(new Particle());
  }
  
  background(255);
  //for Java Mode
//  stroke(0, 128);
//  strokeWeight(0.01);
  // for JavaScript mode
  stroke(0, 20);
  strokeWeight(1);
}

void draw(){
  for(Particle particle: particles){
    particle.update();
    particle.display();
  } 
}

class Particle{
  
  PVector pos, vel;
  PVector pPos;
  
  Particle(){
    pos = new PVector(random(width), random(height));
    float velSize = map(random(1), 0, 1, 1, 3);
    float velAng = random(TWO_PI);
    vel = new PVector(velSize * cos(velAng), velSize * sin(velAng));
  }
  
  void update(){
    float accSize = map(random(1), 0, 1, 0.1, 0.4);
    float accAng = random(TWO_PI);
    PVector acc = new PVector(accSize * cos(accAng), accSize * sin(accAng)); 
    vel.add(acc);
    vel.limit(5);
    pPos = pos;
    pos = PVector.add(pos, vel);
    if(pos.x < 0){
      pos.x += width;
      pPos.x += width;
    }
    if(pos.x >= width){
      pos.x -= width;
      pPos.x -= width;
    }
    if(pos.y < 0){
      pos.y += height;
      pPos.y += height;
    }
    if(pos.y >= height){
      pos.y -= height;
      pPos.y -= height;
    }
  }
  
  void display(){
    beginShape();
    vertex(pos.x, pos.y);
    vertex(pPos.x, pPos.y);
    endShape();
  }
  
}