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

Daily Creative Coding

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

溢れるパーティクル

パーティクル インタラクション
/**
* spilled particles
*
* @author aa_debdeb
* @date 2016/11/26
*/

float MAX_VEL = 15;
float MAX_SIZE = 10.0;
float GRAVITY = 1.0;

ArrayList<Particle> particles;

void setup(){
  size(640, 640);
  particles = new ArrayList<Particle>();
}

void draw(){
  fill(0, 100);
  rect(0, 0, width, height);
  noStroke();
  fill(255);
  ArrayList<Particle> nextParticles = new ArrayList<Particle>();
  for(Particle p: particles){
    p.update();
    p.display();
    if(p.size > 0.1){
      nextParticles.add(p);
    }
  }
  particles = nextParticles;
  
  if(mousePressed){
    for(int i = 0; i < random(50); i++){
      particles.add(new Particle());
    }
  } else {
    for(int i = 0; i < random(3); i++){
      particles.add(new Particle());
    } 
  }
}

class Particle {
  
  PVector loc, vel;
  float size;
  float decay;
  
  Particle(){
    loc = new PVector(mouseX, mouseY);
    float velSize = 0.0;
    for(int i = 0; i < 5; i++){
      velSize += random(MAX_VEL);
    }
    velSize /= 5.0;
    float velAng = random(TWO_PI);
    vel = new PVector(velSize * cos(velAng), velSize * sin(velAng));
    size = 0.0;
    for(int i = 0; i < 5; i++){
      size += random(MAX_SIZE);
    }
    size /= 5.0;
    
    decay = random(0.9, 0.97);
  }
  
  void display(){
    ellipse(loc.x, loc.y, size, size);
  }
  
  void update(){
    vel.y += GRAVITY;
    loc.add(vel);
    if(loc.y >= height - size / 2){
      vel.y *= -0.5;
      loc.y += vel.y;
    }
    size *= decay;
  }
  
}
f:id:aa_debdeb:20161120143144j:plain