Daily Creative Coding

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

都市の上を飛ぶ

f:id:aa_debdeb:20161018072538j:plain

fly above city - OpenProcessing

/**
* fly above city
*
* @author aa_debdeb
* @date 2016/10/26
*/

float speed = 20;

ArrayList<Block> blocks;
color c1, c2, c3;

void setup(){
//  fullScreen(P3D);
  size(640, 640, P3D);
  rectMode(CENTER);
  noStroke();
  mousePressed();
  blocks = new ArrayList<Block>();
  int num = getAddBlockNum(width  *  height * 4);
  for(int i = 0; i < num; i++){
    blocks.add(new Block());
  }
}

int getAddBlockNum(float area){
  return int(area * map(noise(frameCount * 0.002), 0, 1, 0.0001, 0.0004));
}

void mousePressed(){
  c1 = color(random(255), random(255), random(255)); 
  c2 = color(random(255), random(255), random(255)); 
  c3 = color(random(255), random(255), random(255)); 
}



void draw(){
  background(c1);
  translate(width / 2, height * 3.0 / 4, -height / 2);
  lights();
  rotateX(PI / 2);
  rotateX(map(mouseY, 0, height, -PI / 12, 0));
  rotateZ(map(mouseX, 0, width, -PI / 16, PI / 16));
  fill(c2);
  rect(0, 0, width * 4, height * 4);
  fill(c3);
  ArrayList<Block> nextBlocks = new ArrayList<Block>();
  for(Block block: blocks){
    block.display();
    block.update();
    if(!block.canDelete()){
      nextBlocks.add(block);
    }
  }
  blocks = nextBlocks;
  int num = getAddBlockNum(speed * width);
  for(int i = 0; i < num; i++){
    PVector shape = new PVector(random(10, 50), random(10, 50), random(30, 300));
    PVector loc = new PVector(random(-width, width), random(-height, -height + speed), shape.z / 2);
    blocks.add(new Block(loc, shape));
  }
}

class Block{
  
  PVector loc, shape;
  float angle;  

  Block(){
    shape = new PVector(random(10, 50), random(10, 50), random(30, 300));
    loc = new PVector(random(-width, width), random(-height, height), shape.z / 2);
    angle = random(TWO_PI);
  }
  
  Block(PVector _loc, PVector _shape){
    loc = _loc;
    shape = _shape;
    angle = random(TWO_PI);
  }
  
  void display(){
    pushMatrix();
    translate(loc.x, loc.y, loc.z);
    rotateZ(angle);
    box(shape.x, shape.y, shape.z);
    popMatrix();
  }
  
  void update(){
    loc.y += speed; 
  }
  
  boolean canDelete(){
    return loc.y > height;
  }
  
}