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

Daily Creative Coding

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

成長

/**
* growth
*
* @author aa_debdeb
* @date 2016/04/06
*/

ArrayList<Bubble> bubbles;

void setup(){
  size(640, 480);
  background(255);
  fill(255);
  stroke(0);
  
  bubbles = new ArrayList<Bubble>();
}

void draw(){
  
  PVector target = new PVector(mouseX, mouseY);
  for(Bubble bubble: bubbles){
    bubble.update(target);
    bubble.draw();
  }
  
  ArrayList<Bubble> nextBubbles = new ArrayList<Bubble>();
  for(Bubble bubble: bubbles){
    if(!bubble.isTooSmall()){
      nextBubbles.add(bubble);
    }
  }
  bubbles = nextBubbles;
  
  if(random(1) < 0.2){
    bubbles.add(new Bubble());
  }
    
}

class Bubble {
  
  PVector pos;
  float radious;
  
  Bubble(){
    radious = random(30, 200);
    pos = new PVector(random(-radious, width + radious), height + radious * 2);
  }
  
  void update(PVector target){
    PVector vel = PVector.sub(target, pos);
    vel.limit(2);
    pos.add(vel);
    radious *= sq(0.99);
  }
  
  void draw(){
    ellipse(pos.x, pos.y, radious * 2, radious * 2);
  }
  
  boolean isTooSmall(){
    if(radious < 1){
      return true;
    } else {
      return false;
    }
  }
}