Angle X between two 3D vectors?

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

EDIT: (my atan2 function does not make sense, now it should find the "y-angle" between the two vectors):

toDegrees(atan2(Ax-Bx,Az-Bz))+180 

But that gives me an angle Y between two vectors. I need to find the X-angle between them. This is due to the use of x, y, and z position values. Not only x and z, because it gives an angle Y between two vectors. I need an X angle, I know this 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 an up / down angle between two vectors. If I rotate this three-dimensional camera along the y axis, the x axis will not change. Thus, with 2 vectors, regardless of the fact that there is an โ€œangle yโ€ between them, the angle x between the two vectors wil remains unchanged if the y-angle changes, because it is โ€œup / downโ€, as in a camara.

Please, help? I just need a math / pseudo-code string or explanation. :)

+6
source share
3 answers

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.

+5
source
 atan2(crossproduct.length,scalarproduct) 

The reason for using atan2 instead of arccos or arcsin is accuracy. arccos behaves very close to 0 degrees. Small calculation errors in the argument will result in disproportionately large errors as a result. arcsin has the same problem, close to 90 degrees.

+6
source

Just like 2D vectors , you calculate their angle by resolving the cos of their Dot Product

Dot product of a 3d vector
You do not need atan , you always go for a point product, since its fundamental operation of vectors, then use acos to get the angle.

 double angleInDegrees = acos ( cos(theta) ) * 180.0 / PI; 
+4
source

Source: https://habr.com/ru/post/924352/


All Articles