In Three.js I have 2 3d vectors and I want to find the x-axis and y-axis angle between them.
For the x-axis I found this
toDegrees(atan2(A.z, A.y) - atan2(B.z, B.y)) from The X angle between two 3D vectors?
which works, but for y-axis, I am trying
toDegrees(atan2(A.z, A.x) - atan2(B.z, B.x)) but it gives me the wrong value. How can I fix this?
Thanks
11 Answer
You can use Vector3.angleTo directly, it will give you the angle in radians, as usual to math functions, here I printing it in degrees (multiplying by 180 degrees/pi rad= 1)
// notice that v1 is pointing to the x-axis direction. const v1 = new THREE.Vector3(10,0,0); const v2 = new THREE.Vector3(3,3,0); const v3 = new THREE.Vector3(0, 50, 23); // Alngle between v1 and v2 console.log(v1.angleTo(v2)*180/Math.PI) // The operation is comutative console.log(v2.angleTo(v1)*180/Math.PI) // An example at 90 degree console.log(v1.angleTo(v3)*180/Math.PI)<script src=""></script>This makes it possible to find angles to any direction, if you want angle to a plane for instance, it is given by ans(a - 90) where a is the angle in degrees to the normal vector of the plane.