Currently, I am working on a demo to check player controls for a FPS game. The camera rotation is controlled by the mouse, and the player can move using W-A-S-D keys. However, I am facing an issue with implementing movement left and right relative to the direction of the camera.
I am utilizing THREE.JS along with PhysiJS (Physics engine) in my project.
Below is a snippet of the code I am currently using...
// Direction the camera is pointing
var cameraLookVector = this.controls.getDirection(this.vDirection);
// Player Movement
this.v = new THREE.Vector3(0,0,0);
if(keyboard.pressed("W")) { // Move forward
this.v.x -= cameraLookVector.x * (Player.SPEED * delta * this.q); // Working perfectly
this.v.z -= cameraLookVector.z * (Player.SPEED * delta * this.q);
}
if(keyboard.pressed("A")) { // Move left
// Currently setting position based on world coordinates instead of camera direction
this.v.x -= Player.SPEED * delta * this.q;
/* Tried alternatives without success
this.v.x -= Math.cos(cameraLookVector * (Math.PI/180)) * (Player * delta * this.q);
this.v.z -= Math.sin(cameraLookVector * (Math.PI/180)) * (Player * delta * this.q);
*/
}
if(keyboard.pressed("S")) { // Move backward
this.v.x -= cameraLookVector.x * (Player.SPEED * delta * this.q); // Works fine
this.v.z -= cameraLookVector.z * (Player.SPEED * delta * this.q);
}
if(keyboard.pressed("D")) { // Move right
// Currently setting position based on world coordinates instead of camera direction
this.v.x += Player.SPEED * delta * this.q;
}
this.bodyMesh.setLinearVelocity(this.v);
The current implementation of left and right controls updates the player's position in relation to the world, not the camera direction. This results in the player moving in different directions based on camera rotation. I aim to update the player's left and right movement relative to the camera direction so that they always appear to be strafing left or right from the player's perspective.
Any assistance provided would be greatly appreciated!