Daily Creative Coding

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

camera関数の使い方チェック

カメラ位置

  • x方向にマイナス:a
  • x方向にプラス:d
  • y方向にマイナス:w
  • y方向にプラス:x
  • z方向にマイナス:↑
  • z方向にプラス:↓

javascriptモードだとキーボード入力がうまくいかない?

/**
* 3D Camera Check 1
*
* @author aa_debdeb
* @date 2015/09/23
*/

float eyeX;
float eyeY;
float eyeZ;

void setup(){
  size(500, 500, P3D);
  smooth();
  noFill();
  frameRate(24);
  
  eyeX = width/2.0;
  eyeY = height/2.0;
  eyeZ = (height/2.0) / tan(PI*30.0 / 180.0);
}

void draw(){
  background(255);
  
  pushMatrix();
  stroke(255, 0, 0);
  translate(width/2 - 150, height/2);
  box(50);
  popMatrix();
  
  pushMatrix();
  stroke(0, 255, 0);
  translate(width/2, height/2);
  box(50);
  popMatrix();
  
  pushMatrix();
  stroke(0, 0, 255);
  translate(width/2 + 150, height/2);
  box(50);
  popMatrix();
  
  if(keyPressed){
    switch(key){
    case 'a':
      eyeX -= 1.0;
      break;
    case 'd':
      eyeX += 1.0;
      break;
    case 'w':
      eyeY -= 1.0;
      break;
    case 'x':
      eyeY += 1.0; 
      break;
    }
    if(keyCode == UP){
      eyeZ -= 1.0;
    } else if(keyCode == DOWN){
      eyeZ += 1.0;
    }
    camera(eyeX, eyeY, eyeZ, width/2.0, height/2.0, 0, 0, 1, 0);
  }
}

カメラの中心位置

  • x方向にマイナス:a
  • x方向にプラス:d
  • y方向にマイナス:w
  • y方向にプラス:x
  • z方向にマイナス:↑
  • z方向にプラス:↓
/**
* 3D Camera Check 2
*
* @author aa_debdeb
* @date 2015/09/23
*/

float centerX;
float centerY;
float centerZ;

void setup(){
  size(500, 500, P3D);
  smooth();
  noFill();
  
  centerX = width/2.0;
  centerY = height/2.0;
  centerZ = 0;
}

void draw(){
  background(255);
  
  pushMatrix();
  stroke(255, 0, 0);
  translate(width/2 - 150, height/2);
  box(50);
  popMatrix();
  
  pushMatrix();
  stroke(0, 255, 0);
  translate(width/2, height/2);
  box(50);
  popMatrix();
  
  pushMatrix();
  stroke(0, 0, 255);
  translate(width/2 + 150, height/2);
  box(50);
  popMatrix();
  
  if(keyPressed){
    switch(key){
    case 'a':
      centerX -= 1.0;
      break;
    case 'd':
      centerX += 1.0;
      break;
    case 'w':
      centerY -= 1.0;
      break;
    case 'x':
      centerY += 1.0; 
      break;
    }
    if(keyCode == UP){
      centerZ -= 1.0;
    } else if(keyCode == DOWN){
      centerZ += 1.0;
    }
    camera(width/2.0, height/2.0, (height/2.0) / tan(PI*30.0 / 180.0), centerX, centerY, centerZ, 0, 1, 0);
  }
}