My goal is to have the camera looking perpendicularly at one face of a cube, no matter how the cube rotates. I'm using FirstPersonControls to control the cube. I'm using the pointer lock API so the cube rotates with my mouse movements as a character would in a 3rd person shooter.
I'm having problems keeping the camera a fixed distance behind the cube and always looking at the cube at the same angle.
To set the position of the camera, I'm doing something like this:
camera.position.x = mesh.position.x + Math.sin(mesh.rotation.y) * 400;
camera.position.y = 450;
camera.position.z = mesh.position.z + Math.cos(mesh.rotation.y) * 400;
camera.lookAt(mesh.position);
There's two problems with this: One is I'm able to keep aligned behind the cube for a rotation of only 180 degrees, then the cube will do a full spin before the camera re-aligns again. I suspect this is because, when controlled by FirstPersonControls, mesh.rotation.y takes on values from -Pi/2 to Pi/2 and then starts returning to -Pi/2 even though I keep turning in the same direction. So the camera is not able to do a full spin as it would if mesh.rotation.y kept increasing indefinitely as I kept moving the mouse right.
The other problem is I'm not sure how to calculate camera.position.y with respect to the mesh rotation. I also want the camera to move on the y-axis while looking at the cube, rather than being hard-coded at 450. But that is a problem I think I should solve after fixing the first issue.
My goal is to have the camera looking perpendicularly at one face of a cube, no matter how the cube rotates. I'm using FirstPersonControls to control the cube. I'm using the pointer lock API so the cube rotates with my mouse movements as a character would in a 3rd person shooter.
I'm having problems keeping the camera a fixed distance behind the cube and always looking at the cube at the same angle.
To set the position of the camera, I'm doing something like this: camera.position.x = mesh.position.x + Math.sin(mesh.rotation.y) * 400; camera.position.y = 450; camera.position.z = mesh.position.z + Math.cos(mesh.rotation.y) * 400;
camera.lookAt(mesh.position);
There's two problems with this: One is I'm able to keep aligned behind the cube for a rotation of only 180 degrees, then the cube will do a full spin before the camera re-aligns again. I suspect this is because, when controlled by FirstPersonControls, mesh.rotation.y takes on values from -Pi/2 to Pi/2 and then starts returning to -Pi/2 even though I keep turning in the same direction. So the camera is not able to do a full spin as it would if mesh.rotation.y kept increasing indefinitely as I kept moving the mouse right.
The other problem is I'm not sure how to calculate camera.position.y with respect to the mesh rotation. I also want the camera to move on the y-axis while looking at the cube, rather than being hard-coded at 450. But that is a problem I think I should solve after fixing the first issue.
Would anyone be able to offer some advice?