Daily Creative Coding

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

キューブの崩壊

/**
* destruction of cube
*
* @author aa_debdeb 
* @date 2016/07/31
*/

int blockNum = 10;
float blockSize = 30;
ArrayList<Block> blocks; 

void setup(){
  size(500, 500, P3D);
  noStroke();
  initialize();
}

void initialize(){
  blocks = new ArrayList<Block>();
  float offset = blockNum / 2.0 * blockSize;
  for(int x = 0; x < blockNum; x++){
    for(int y = 0; y < blockNum; y++){
      for(int z = 0; z < blockNum; z++){
        PVector loc = new PVector(x * blockSize - offset, y * blockSize - offset, z * blockSize - offset);
        blocks.add(new Block(loc));
      }
    }
  }
}

void draw(){
  background(255);
  lights();
  translate(width / 2, height / 2, -200);
  float camX = map(mouseX, 0, width, -1000, 1000);
  float camY = map(mouseY, 0, height, -500, 500);
  camera(camX, camY, 700, 0, 0, 0, 0, 1, 0);
  float alpha = map(frameCount % 1000, 0, 1000 - 1, 255, 0);
  fill(255, 165, 0, alpha);
  for(Block block: blocks){
    block.display();
    block.update();
  }
  if(frameCount % 1000 == 1000 - 1){
    initialize();
  }
}

class Block{
  
  PVector loc;
  
  Block(PVector _loc){
    loc = _loc;
  }
  
  void display(){
    pushMatrix();
    translate(loc.x, loc.y, loc.z);
    box(blockSize);
    popMatrix();
  }
  
  void update(){
    loc.add(new PVector(random(-3, 3), random(-3, 3), random(-3, 3)));
  }
}
f:id:aa_debdeb:20160725194231j:plain