Natural "first person like" turning/moving in P3D

Hey everyone,

I think I’m making something more difficult than it needs to be. To begin with I am simply trying to be able to turn the camera in the P3D environment so that when I then move forward, the camera moves in the direction it is facing - just like in a typical first person game.

I know there are libraries that handle this well, but for educational purposes, I’d like to achieve this effect as simply as possible. Here is a very simple version of the code. I’m not really sure how to describe what is happening. Turning (changing the angle of the camera) and moving forward seem to work, but if you watch the box as you turn, it does not behave as you would expect in real life.

What am I missing or not taking into consideration?

Thank you very much.

float rotX = 0;
float forward = height/2;

void setup() {
  size(640, 360, P3D);
}

void draw() {
  background(100);
  beginCamera();
  camera();
  translate(0,0,forward);
  rotateY(rotX);
  endCamera();
  translate(width/2, height/2, -100);
  stroke(0);
  box(200);

  //37=left,right=39, up=38, down=40

  if (keyCode == 37) {
    rotX = rotX - .1;
  }
  if (keyCode == 39) {
    rotX = rotX + .1;
  }
  if (keyCode == 38) {
    forward = forward + 36;
  }
  if (keyCode == 40) {
    forward = forward - 36;
  }    

}//draw


void keyReleased() { 
  keyCode = 0;
}

Allright, so I see that the camera rotates around the center of the scene.

I’d like to rotate the camera around it’s own center, no matter where I have moved it to. Then, when I go forward, it actually moves forward in the direction it is facing.

Still trying to figure out how to do that.

https://discourse.processing.org/t/extend-mousex-position-beyond-screen/

2 Likes