I am working with a camera that needs to rotate around a specific target position within the scene. Despite attempts to implement a pivot concept, as suggested in this discussion: https://github.com/mrdoob/three.js/issues/1830, I have come up with my own solution using the code below:
var rotationY = new THREE.Matrix4();
var rotationX = new THREE.Matrix4();
var translation = new THREE.Matrix4();
var translationInverse = new THREE.Matrix4();
var matrix = new THREE.Matrix4();
function rotateCameraAroundObject(dx, dy, target) {
// defining up and right vectors based on camera perspective
camComponents.up = rotateVectorForObject(new THREE.Vector3(0,1,0), camera.matrixWorld);
camComponents.right = rotateVectorForObject(new THREE.Vector3(1,0,0), camera.matrixWorld);
matrix.identity();
rotationX.makeRotationAxis(camComponents.right, -dx);
rotationY.makeRotationAxis(camComponents.up, -dy);
translation.makeTranslation(
target.position.x - camera.position.x,
target.position.y - camera.position.y,
target.position.z - camera.position.z);
translationInverse.getInverse(translation);
matrix.multiply(translation).multiply(rotationY).multiply(rotationX).multiply(translationInverse);
camera.applyMatrix(matrix);
camera.lookAt(target.position);
}
The challenge lies in not wanting to utilize lookAt due to its reorientation effect. The goal is to achieve the rotation without relying on this function.
When the code above is executed without lookAt, the camera rotates around the point but does not maintain focus on it. Ideally, the camera's perspective should align with its physical rotations, yet this does not happen. I would appreciate any insights into what could be causing this discrepancy.
EDIT: Revising the original post and code for better clarity regarding my query.
The approach involves translating to the origin (the target position), rotating to the desired degree, and then returning to the initial position. This process is expected to result in a new orientation towards the origin due to the rotation applied.
Currently, I am conducting tests without incorporating the translation matrices, which simplifies the matrix multiplication line to:
matrix.multiply(rotationY).multiply(rotationX);
Yet, the behavior remains consistent. Many thanks for all the previous assistance!
ONE MORE THING! An additional complexity arises when the camera encounters issues near the poles, specifically the north and south points of the scene. To achieve a more versatile and unrestricted movement, I am seeking recommendations for enhancing the camera control system.