Daily Creative Coding

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

水面から飛び出るパーティクル

/*
* particles from water 
*
* @author aa_debdeb
* @date 2016/12/26
*/

float gravity = 0.15;
ArrayList<Particle> particles;
color c1 = color(173, 216, 230);
color c2 = color(100, 149, 237);

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

void draw(){
  background(c1);
  float[][] values = new float[width][height];
  for(int w = 0; w < width; w++){
    for(int h = 0; h < height; h++){
      if(h < 290){
        values[w][h] = 0;
      } else if(h >= 290 && h <= 310){
        values[w][h] = map(h, 290, 310, 0, 1);
      } else {
        values[w][h] = 1.0;
      }
      for(Particle p: particles){
        float dist = sqrt(sq(p.loc.x - w) + sq(p.loc.y - h));
        values[w][h] += p.radius / dist;
      }
    }
  }
  for(int w = 0; w < width; w++){
    for(int h = 0; h < height; h++){
      if(values[w][h] > 0.9){
      fill(map(constrain(values[w][h], 0.9, 1.0), 0.9, 1.0, red(c1), red(c2)),
             map(constrain(values[w][h], 0.9, 1.0), 0.9, 1.0, green(c1), green(c2)),
             map(constrain(values[w][h], 0.9, 1.0), 0.9, 1.0, blue(c1), blue(c2)));
      rect(w, h, 1, 1);
      }
    }
  }
  
  ArrayList<Particle> nextPs = new ArrayList<Particle>();
  for(Particle p: particles){
    p.update();
    if(p.loc.y < height + 100){
      nextPs.add(p);
    }
  }
  particles = nextPs;
}

void mousePressed(){
    particles.add(new Particle());

}

class Particle{
  
  PVector loc, vel;
  float radius;
  
  Particle(){
    loc = new PVector(random(width), height + 40);
    float velSize = 10;
    float velAng = random(PI / 3, PI / 3 * 2) + PI;
    vel = new PVector(velSize * cos(velAng), velSize * sin(velAng));
    radius = 10;
  }
  
  void update(){
    vel.y += gravity;
    loc.add(vel);
  }
  
}
f:id:aa_debdeb:20161223061032j:plain