Within my app, I have implemented a camera:
camera = new THREE.PerspectiveCamera( 75, window.innerWidth / window.innerHeight, 0.1, 1000 );
camera.position.z = 1;
camera.position.y = -5;
camera.rotateOnAxis(new THREE.Vector3(1, 0, 0), degInRad(90));
camera.up = new THREE.Vector3(0, 0, 1);
The code inside the render
function is supposed to rotate the camera as I press certain keys:
if (leftPressed) {
camera.rotateOnAxis((new THREE.Vector3(0, 1, 0)).normalize(), degInRad(1));
} else if (rightPressed) {
camera.rotateOnAxis((new THREE.Vector3(0, 1, 0)).normalize(), degInRad(-1));
}
if (upPressed) {
camera.rotateOnAxis((new THREE.Vector3(1, 0, 0)).normalize(), degInRad(1));
} else if (downPressed) {
camera.rotateOnAxis((new THREE.Vector3(1, 0, 0)).normalize(), degInRad(-1));
}
The camera rotates, but not in the manner I desire. I want the camera rotation to mimic that of a first-person shooter game on a plane. Refer to the image below for better understanding of what I mean...
I attempted to use sin(1)
and cos(1)
, but I am struggling to comprehend how the rotateOnAxis
method works, as the translate
functions work seamlessly and move the camera in the direction it is facing.
P.S.
Check out the documentation for three.js
here. Maybe it can provide some insights.
For handling keyboard events, I have utilized KeyboardJS.
Here is the degInRad
function:
function degInRad(deg) {
return deg * Math.PI / 180;
}
Link to the JSFiddle
O
- position of camera
O-O1
- current camera direction
R1
- current rotation direction
R
- desired rotation direction
Apologies for the simplistic illustration.