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;
}