Daily Creative Coding

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

柔らかいタワー

/**
* soft towers
*
* @author aa_debdeb 
* @date 2016/11/19
*/

ArrayList<Tower> towers;
float towerWidth = 25;
float minHeight = 0;
float maxHeight = 400;
float maxAcc = 50;

void setup(){
  size(500, 500, P3D);
  towers = new ArrayList<Tower>();
  for(float w = -width / 2; w <= width / 2; w += towerWidth){
    for(float h = -height / 2; h <= height / 2; h += towerWidth){
      towers.add(new Tower(w, h));
    }
  }
}

void draw(){
  background(230);
  lights();
  fill(0, 139, 139);
  stroke(0, 139, 139);
  translate(width / 2, height / 2 + 150, -500);
  rotateX(PI / 3);
  rotateZ(PI / 5);
  for(Tower tower: towers){
    tower.display();
    tower.update();
  }
}

void mousePressed(){
  for(Tower tower: towers){
    tower.setTargetZ();
  }
}

class Tower {

  PVector loc, shape;
  float targetZ, vel;
  
  Tower(float w, float h){
    loc = new PVector(w, h);
    shape = new PVector(towerWidth, towerWidth, random(minHeight, maxHeight));
    vel = 0.0;
    setTargetZ();
  }  
  
  void setTargetZ(){
    vel = random(-100, 100);
    targetZ = random(minHeight, maxHeight);
  }
  
  void display(){
    pushMatrix();
    translate(loc.x, loc.y, shape.z / 2);
    box(shape.x, shape.y, shape.z);
    popMatrix();
  }
  
  void update(){
    vel *= 0.9;
    float acc = constrain(targetZ - shape.z, -maxAcc, maxAcc);
    vel += acc;
    shape.z += vel;
  }
}
f:id:aa_debdeb:20161113124036j:plain