The X angle between two 3D vectors?

Arundel picture Arundel · Sep 1, 2012 · Viewed 15.4k times · Source

I have two 3D vectors called A and B that both only have a 3D position. I know how to find the angle along the unit circle ranging from 0-360 degrees with the atan2 function by doing:

EDIT: (my atan2 function made no sense, now it should find the "y-angle" between 2 vectors):

toDegrees(atan2(A.x-B.x,A.z-B.z))+180

But that gives me the Y angle between the 2 vectors. I need to find the X angle between them. It has to do with using the x, y and z position values. Not the x and z only, because that gives the Y angle between the two vectors. I need the X angle, I know it sounds vague but I don't know how to explain. Maybe for example you have a camera in 3D space, if you look up or down than you rotate the x-axis. But now I need to get the "up/down" angle between the 2 vectors. If I rotate that 3D camera along the y-axis, the x-axis doens't change. So with the 2 vectors, no matter what the "y-angle" is between them, the x-angle between the 2 vectors wil stay the same if y-angle changes because it's the "up/down" angle, like in the camara.

Please help? I just need a line of math/pseudocode, or explanation. :)

Answer

maxim1000 picture maxim1000 · Sep 2, 2012
atan2(crossproduct.length,scalarproduct)

The reason for using atan2 instead of arccos or arcsin is accuracy. arccos behaves very badly close to 0 degrees. Small computation errors in argument will lead to disproportionally big errors in result. arcsin has same problem close to 90 degrees.