Daily Creative Coding

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

球の中心にあるカメラ

/**
* camera in sphere
*
* @author aa_debdeb
* @date 2016/05/14
*/

ArrayList<PVector> points;

void setup(){
  size(500, 500, P3D);
  points = new ArrayList<PVector>();
  float r = width / 2;
  for(int i = 0; i < 300; i++){
    float radian1 = random(PI / 2);
    float radian2 = random(TWO_PI);
    float x = r * cos(radian1) * cos(radian2);
    float y = r * cos(radian1) * sin(radian2);
    float z = -r * sin(radian1);
    points.add(new PVector(x, y, z));
  }
  noStroke();
}

void draw(){
  background(0);
  lights();
  float r = width / 2;
  float x = map(mouseX, 0, width, -width / 2, width / 2);
  float y = map(mouseY, 0, height, -height / 2, height / 2);
  float z;
  if(sqrt(sq(x) + sq(y)) > r){
    z = 0;
  } else {
    z = -sqrt(sq(r) -sq(x) - sq(y));
  }
  translate(width / 2, height / 2, 0);
  camera(0, 0, 0, x, y, z, 0, 1, 0);
  fill(122, 204, 184);
  stroke(138, 230, 207);
  sphere(r);
  fill(204, 122, 143);
  stroke(204, 20, 66);
  for(PVector p: points){
    pushMatrix();
    translate(p.x, p.y, p.z);
    box(10);
    popMatrix();
  }
}