This is a follow-up to a previous question (“Apply new camera world direction”) I asked last week. User tfoller explained some stuff to me that I thought I understood, but something is still missing in my understanding. In a sample code, my camera was at (500, 500, 3200). Because the camera is initially pointing in the (0, 0, -1) direction, I believe I can find a lookat point in the xy-plane (i.e., 3200 units away from the camera along z) as follows:
const forward = new THREE.Vector3(0, 0, -3200).applyQuaternion(camera.quaternion);
const lookat = new THREE.Vector3().copy(camera.position).add(forward);
Or, more generally,
const forward = new THREE.Vector3(0, 0, -camera.position.z).applyQuaternion(camera.quaternion);
const lookat = new THREE.Vector3().copy(camera.position).add(forward);
Indeed, doing that calculation returns the lookat to be (500, 500, 0), as expected.
Now let me move the camera by 500 units in the +x direction to (1000, 500, 3200). Then recalculating the lookat point returns (1000, 500, 0), also as expected.
But I want to now rotate the camera to be looking at the original lookat point, so I call
const original_lookat = THREE.Vector3(500, 500, 0);
camera.lookAt (original_lookat);
If I then recalculate the actual lookat using the code shown earlier, instead of the desired (500, 500, 0) I get (500.99, 500, 38.36). These differences in x and z seem to be more than just round-off error. And if I move the camera again, the errors accumulate. I think I don’t quite understand how to calculate the lookat point correctly. Can tfoller or anyone help my poor little brain figure this out?
Thanks.