読者です 読者をやめる 読者になる 読者になる

Daily Creative Coding

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

ピョンピョン跳ねるパーティクル

/**
* hopping particles
*
* @author aa_debdeb
* @date 2016/12/07
*/

float MAX_VEL = 10;
ArrayList<Particle> particles;

void setup(){
  size(640, 640);
  particles = new ArrayList<Particle>();
  for(int i = 0; i < 50; i++){
    particles.add(new Particle());
  }
}

void draw(){
  background(0);
  noStroke();
  fill(255);
  for(Particle p: particles){
    p.display();
    p.update();
  }
}

class Particle {
  
  PVector loc, vel;
  
  Particle(){
    loc = new PVector(random(width), random(height));
    setVel();
  }
  
  void setVel(){
    float velSize = random(MAX_VEL / 2, MAX_VEL);
    float velAng = random(TWO_PI);
    vel = new PVector(velSize * cos(velAng), velSize * sin(velAng));  
  }
  
  void display(){
    pushMatrix();
    translate(loc.x, loc.y);
    rotate(atan2(vel.y, vel.x));
    float ellipseX = map(vel.mag(), 0, MAX_VEL, 15, 30);
    float ellipseY = map(vel.mag(), 0, MAX_VEL, 15, 5);
    ellipse(0, 0, ellipseX, ellipseY);
    popMatrix();
  }
  
  void update(){
    if(random(1) < 0.02){
      setVel();
    } else {
      vel.mult(0.98);
    }
    loc.add(vel);
    if(loc.x < 0) loc.x += width;
    if(loc.x > width) loc.x -= width;
    if(loc.y < 0) loc.y += height;
    if(loc.y > height) loc.y -= height;  
  }
  
}
f:id:aa_debdeb:20161201071748j:plain