Daily Creative Coding

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

公転軌道上にあるカメラ

/**
* view on orbital path
* 
* @author aa_debdeb
* @date 2016/05/17
*/

ArrayList<Obj> objs;

void setup(){
  size(500, 500, P3D);
  objs = new ArrayList<Obj>();
  for(int i = 0; i < 200; i++){
    objs.add(new Obj());
  }
  noStroke();
}

void draw(){
  background(0);
  lights();
  float time = frameCount * 0.03;
  translate(width / 2, height / 2, 0);
  float toC = map(mouseX, 0, width, 1, 0);
  camera(300 * cos(time), 0, 300 * sin(time), 300 * cos(time + PI / 2) * toC, 0, 300 * sin(time + PI / 2) * toC, 0, 1, 0);   
  for(Obj obj: objs){
    obj.display();
  }
}

class Obj{
  
  PVector pos;
  float size;
  boolean isSphere;
  float g, b;
  
  Obj(){
    float radious = random(800);
    float radian1 = random(PI);
    float radian2 = random(TWO_PI);
    float x = radious * sin(radian1) * cos(radian2);
    float y = radious * sin(radian1) * sin(radian2);
    float z = radious * cos(radian1);
    pos = new PVector(x, y, z);
    size = random(10, 70);
    isSphere = random(1) < 0.5;
    g = random(255);
    b = random(255);
  }
  
  void display(){
    fill(255, g, b);
    pushMatrix();
    translate(pos.x, pos.y, pos.z);
    if(isSphere){
      sphere(size);
    } else {
      box(size);
    }
    popMatrix();
  }
}