Euler angles between two 3d vectors
Solution 1
As others have already pointed out, your question should be revised. Let's call your vectors a
and b
. I assume that length(a)==length(b) > 0
otherwise I cannot answer the question.
Calculate the cross product of your vectors v = a x b
; v
gives the axis of rotation. By computing the dot product, you can get the cosine of the angle you should rotate with cos(angle)=dot(a,b)/(length(a)length(b))
, and with acos
you can uniquely determine the angle (@Archie thanks for pointing out my earlier mistake). At this point you have the axis angle representation of your rotation.
The remaining work is to convert this representation to the representation you are looking for: Euler angles. Conversion Axis-Angle to Euler is a way to do it, as you have found it. You have to handle the degenerate case when v = [ 0, 0, 0]
, that is, when the angle is either 0 or 180 degrees.
I personally don't like Euler angles, they screw up the stability of your app and they are not appropriate for interpolation, see also
Solution 2
At first you would have to subtract vector one from vector two in order to get vector two relative to vector one. With these values you can calculate Euler angles.
To understand the calculation from vector to Euler intuitively, lets imagine a sphere with the radius of 1 and the origin at its center. A vector represents a point on its surface in 3D coordinates. This point can also be defined by spherical 2D coordinates: latitude and longitude, pitch and yaw respectively.
In order "roll <- pitch <- yaw" calculation can be done as follows:
To calculate the yaw you calculate the tangent of the two planar axes (x and z) considering the quadrant.
yaw = atan2(x, z) *180.0/PI;
Pitch is quite the same but as its plane is rotated along with yaw the 'adjacent' is on two axis. In order to find its length we will have to use the Pythagorean theorem.
float padj = sqrt(pow(x, 2) + pow(z, 2));
pitch = atan2(padj, y) *180.0/PI;
Notes:
- Roll can not be calculated as a vector has no rotation around its own axis. I usually set it to 0.
- The length of your vector is lost and can not be converted back.
- In Euler the order of your axes matters, mix them up and you will get different results.
tomyake
Updated on December 03, 2020Comments
-
tomyake over 3 years
How do you find the 3 euler angles between 2 3D vectors? When I have one Vector and I want to get its rotation, this link can be usually used: Calculate rotations to look at a 3D point?
But how do I do it when calculating them according to one another? -
Archie about 11 yearsCross production is not enough - it won't distinguish 0 and 180 degrees angles. You should compute both: cross to get a sine and scalar to get a cosine, and then use both of them to compute the angle (for instance via atan2() function in C math library).
-
tomyake about 11 yearsWhat do you think of this method:euclideanspace.com/maths/geometry/rotations/conversions/…
-
Ali about 11 years@Archie Yes, correct, fixed. Actually the dot product and
acos
is enough. -
Ali about 11 years@tomyake Yes, that seems to be an easier way to do it.