Daily Creative Coding

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

無重力空間で回転移動するオブジェクト

f:id:aa_debdeb:20161015204927j:plain

objects with zero gravity - OpenProcessing

/**
* objects with zero gravity
*
* @author aa_debdeb
* @date 2016/10/22
*/

ArrayList<Block> blocks;

void setup(){
  size(640, 640, P3D);
  blocks = new ArrayList<Block>();
  for(int i = 0; i < 60; i++){
    blocks.add(new Block());
  }
}

void draw(){
  background(255);
  translate(width / 2, height / 2, 100);
  lights();
  for(Block b: blocks){
    b.display();
    b.update();
  }
}

class Block{
  
  PVector loc;
  PVector vel;
  PVector shape;
  PVector angle;
  color c;
  
  Block(){
    loc = new PVector(random(-width / 2, width / 2),
                      random(-height / 2, height / 2),
                      random(-height / 2, height / 2));
    vel = new PVector(random(-1, 1), random(-1, 1), random(-1, 1));
    vel.normalize();
    vel.mult(3);
    shape = new PVector(random(50, 100), random(5, 20), random(5, 20));
    angle = new PVector(random(TWO_PI), random(TWO_PI), random(TWO_PI));
    c = color(random(255), random(255), random(255));
  }
  
  void display(){
    fill(c);
    noStroke();
    pushMatrix();
    translate(loc.x, loc.y, loc.z);
    rotateX(angle.x);
    rotateY(angle.y + frameCount * 0.1);
    rotateZ(angle.z);
    box(shape.x, shape.y, shape.z);
    popMatrix();
  }
  
  void update(){
    loc.add(vel);
    if(loc.x < -width / 2) loc.x += width;
    if(loc.y < -height / 2) loc.y += height;
    if(loc.z < -height / 2) loc.z += height;
    if(loc.x > width / 2) loc.x -= width;
    if(loc.y > height / 2) loc.y -= height;
    if(loc.z > height / 2) loc.z -= height;
  }
  
}