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

Daily Creative Coding

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

変形を繰り返すボックス #2

/**
* transforming boxes #2
*
* @author aa_debdeb
* @date 2016/12/16
*/

int LOOP = 300;
float MAX_BLOCK_WIDTH = 50;

ArrayList<Block> blocks;

void setup(){
  size(500, 500, P3D);
  blocks = new ArrayList<Block>();
  float gap = 50;
  int num = 30;
  for(int x = 0; x <= num; x++){
    for(int y = 0; y <= num; y++){
      PVector center = new PVector(x * gap - num * gap / 2, y * gap - num * gap / 2, 0.0);
      blocks.add(new Block(center));
    }
  }
}

void draw(){
  background(0, 0, 28);
  fill(0, 0, 56);
  stroke(255, 69, 0);
  translate(width / 2, height / 4, -200);
  rotateX(HALF_PI / 2);
  for(Block b: blocks){
    b.display();
  }
}

class Block{

  PVector center;
  int loopOffset;
  ArrayList<Target> targets;
  
  Block(PVector _center){
    center = _center;
    targets = new ArrayList<Target>();
    float time = 0.0;
    PVector firstSize = new PVector(random(MAX_BLOCK_WIDTH), random(MAX_BLOCK_WIDTH), random(MAX_BLOCK_WIDTH));
    PVector firstPos = new PVector(random(-MAX_BLOCK_WIDTH / 2 + firstSize.x / 2, MAX_BLOCK_WIDTH / 2 - firstSize.x / 2), random(-MAX_BLOCK_WIDTH / 2 + firstSize.y / 2, MAX_BLOCK_WIDTH / 2 - firstSize.y / 2), firstSize.z / 2);
  
    targets.add(new Target(0.0, firstPos, firstSize));
    do{
      float nextTime = time + 0.2;
      if(nextTime >= 1.0){
        targets.add(new Target(1.0, firstPos, firstSize));
      } else {
        PVector size = new PVector(random(MAX_BLOCK_WIDTH), random(MAX_BLOCK_WIDTH), random(MAX_BLOCK_WIDTH));
        PVector pos = new PVector(random(-MAX_BLOCK_WIDTH / 2 + size.x / 2, MAX_BLOCK_WIDTH / 2 - size.x / 2), random(-MAX_BLOCK_WIDTH / 2 + size.y / 2, MAX_BLOCK_WIDTH / 2 - size.y / 2), size.z / 2);
        targets.add(new Target(nextTime, pos, size));      
      }
      time = nextTime;
    } while(time < 1.0);
    loopOffset = int(random(LOOP));
  }
  
  void display(){
    float time = float((frameCount + loopOffset) % LOOP) / LOOP;
    int targetIdx = 0;
    while(targets.get(targetIdx).time < time && targets.get(targetIdx + 1).time <= time){targetIdx++;}
    Target t1 = targets.get(targetIdx);
    Target t2 = targets.get(targetIdx + 1);
    pushMatrix();    
    translate(center.x + map(time, t1.time, t2.time, t1.pos.x, t2.pos.x),
              center.y + map(time, t1.time, t2.time, t1.pos.y, t2.pos.y),
              center.z + map(time, t1.time, t2.time, t1.pos.z, t2.pos.z));
    box(map(time, t1.time, t2.time, t1.size.x, t2.size.x),   
        map(time, t1.time, t2.time, t1.size.y, t2.size.y),   
        map(time, t1.time, t2.time, t1.size.z, t2.size.z));     
    popMatrix();
  }
  
}

class Target{
  float time;
  PVector pos, size;

  Target(float _time,PVector _pos,PVector _size){
    time = _time;
    pos = _pos;
    size = _size;
  }  
}
f:id:aa_debdeb:20161210221645j:plain