I'm struggling to determine the exact rotation of my object around any given axis.
Thus far, I've managed to find the relative rotation of my object using a specific method. To achieve this, I positioned a plane behind the object aligned with the chosen axis. By casting rays on this plane, I am able to gather 3D coordinates which allow me to calculate the angle by employing the atan2
function.
The following code snippet is what I use to obtain the relative rotation of my object:
let planeCrossObjectDir = planeNormal.clone().cross(this.objectUpDirection).normalize();
let projectedPoint = new THREE.Vector3();
rayPlane.projectPoint(intersection.point, projectedPoint);
let centerProjected = new THREE.Vector3();
rayPlane.projectPoint(this.raycastPlane.position, centerProjected);
let u1 = this.objectUpDirection.dot(projectedPoint);
let v1 = planeCrossObjectDir.dot(projectedPoint);
let u2 = this.objectUpDirection.dot(centerProjected);
let dotv2 = planeCrossObjectDir.dot(centerProjected);
let uvCoord = new THREE.Vector2(u1, v1).sub(new THREE.Vector2(u2, dotv2));
var theta = Math.atan2(uvCoord.y, uvCoord.x); // range [-PI, PI]
theta *= 180 / Math.PI; // range [-180, 180]
if (theta < 0) theta = 360 + theta; // range [0, 360]
let objectsRotation = Math.round(360 - theta); // invert rotation
I aim to extend the same concept to compute the absolute rotation by adjusting the planeCrossObjectDir
and making use of the world UP-vector of the plane instead of this.objectUpDirection
.
However, the challenge lies in figuring out how exactly to perform this calculation...
To simplify matters and for clearer understanding, I have created a diagram: https://i.sstatic.net/QdqaV.jpg