Height Angle Calculation
OK, maybe I finally understood your comment below that the result does not depend on the angle y and how it relates to two vectors. It seems that you are not interested in the two vectors and the angle between the two, but instead you are interested in the vector of differences and the angle that it forms against the horizontal plane. In the horizontal coordinate system (often used in astronomy) this angle will be called "height" or "height", unlike the "azimuth" "You calculate the formula in your (edited) question." The altitude is closely related to the tilt of your camera, while the azimuth refers to panning .
We still have a two-dimensional problem. The single coordinate of the 2D vector is the y coordinate of the difference vector. Another coordinate is the length of the vector after projecting it onto the horizontal plane, i.e. sqrt(x*x + z*z) . Final decision would be
x = Ax - Bx y = Ay - By z = Az - Bz alt = toDegrees(atan2(y, sqrt(x*x + z*z))) az = toDegrees(atan2(-x, -z))
The order ( A - B as opposed to B - A ) was chosen so that "A is above B" gives a positive y and therefore a positive height according to your comment below. The minus signs in the azimuthal calculation above should replace + 180 in the code from your question, except that the range is now [-180, 180] instead of yours [0, 360]. To give you an alternative, choose what you prefer. In fact, you are calculating the bearing B - A in any case. The fact that you are using a different order for these two angles can be a bit confusing, so consider whether this is really what you want, or if you want to change the elevation sign or change the azimuth by 180 ยฐ.
Orthogonal projection
For reference, I will include my initial answer below, for those who are actually looking for a rotation angle around some fixed x axis, as suggested by the original question.
If this x angle, which you indicate in your question, is really the rotation angle around the x axis, as the camera example suggests, then you can think of it this way: set the x coordinate to zero, and you end with two-dimensional vectors in the yz plane. You can think of it as an orthogonal projection onto a plain. Now you return to the two-dimensional problem and you can solve it.
Personally, I just called atan2 twice, once for each vector, and subtracted the resulting angles:
toDegrees(atan2(Az, Ay) - atan2(Bz, By))
x=0 implicit in the above formula simply because I only work on y and z .
I have not yet fully understood the logic of your only call to atan2 , but the fact that I should think about it means that I do not want to support it, at least not without a good explanation for the comment.
I hope I understood your question correctly, and this is what you are looking for.