I am trying to compare the Z-axis and the X-axis of two different quaternions in a way that would give me the Euler angles about the X and Y axes to line up the two different axes. In my program, the first quaternion (whose X-axis we are trying to line up) only rotates about the z and y axes and is the one being rotated first. Whereas the second quaternion (whose Z-axis we are trying to line up) only rotates about the x and y axis and is the one being rotated to match the other quaternion. Currently, I've been able to deduce the direct distance to line up the axes though am struggling to break the angles into pitch and yaw.


I am working with 2 sensors and those are the quaternion data points I am working with. When the sensors are started, they start in different orientations. One (sensor 1) is laid flat, with the X axis pointing out/away from the user whereas the other (sensor 2) is standing up, with the Z axis pointing out/away. For this certain activity, the sensor laying flat is then moved/rotated away from this base position and the user would have to rotate the other sensor on its x and y axis to become in line with the other sensor. In this case, becoming in line would mean lining up sensor 2's Z-axis with sensor 1's X-axis.

Before I was trying to find their orientation relative to each other during the base position which I was having trouble with and ended up deciding to try and attempt to make sensor 2 believe it was rotated 180 degrees on its x and 90 degrees on its y to have its x-axis pointing out instead of its Z-axis to make the comparison between the quaternions easier as I would be checking to see if the same axis were in line.

  • \$\begingroup\$ Do I understand correctly you have an oriented object A whose x local axis points in direction xA, and an orientated object B whose local z axis points in direction zB, and you want to compute a set of angles to rotate object B through so that zB points parallel to xA? In what order do you apply these X and Y rotations to make the alignment? And do you care what happens to B's other direction axes? \$\endgroup\$
    – DMGregory
    Commented May 24 at 19:04
  • \$\begingroup\$ Yes you're spot on, The order of the rotations is also X, then Y on object B \$\endgroup\$
    – ACR115
    Commented May 24 at 19:12
  • \$\begingroup\$ These rotations X and Y are applied in world space? If so, it's not guaranteed that a solution exists — if zB is parallel to world X and xA is not in the XZ plane, then there's no X rotation that will lift zB to match. Or are these rotations applied "under" quaternion B? In that case this is easy: inverse transform xA by B, then convert to spherical coordinates. \$\endgroup\$
    – DMGregory
    Commented May 25 at 14:31
  • \$\begingroup\$ There is no question in your question. You are "trying to compare the Z-axis...", so you probably know something about it, but it is not clear to me where is the problem exactly. Does this answer your question? \$\endgroup\$
    – Piro
    Commented May 30 at 6:34
  • 1
    \$\begingroup\$ Try editing your question to walk us through what you're doing with these sensors, and how they're used in your game. Often that context can help us resolve any ambiguity in the abstractions. \$\endgroup\$
    – DMGregory
    Commented May 31 at 19:18

1 Answer 1


I am finding it difficult to follow your question (even after the edits).

Therefore I will answer my own question (which I am hoping is close to your question) in the hope that you can adapt my answer to your question.

Assuming a RHS (Right Handed System):

  • +X = Right
  • +Y = Into screen.
  • +Z = Up

We have two motorized gimbals (each with only two motors).

  • Gimbal 1 (G1) has motors on the Z and X axes (Z is first in the Euler Rotation order) - its neutral/starting position is pointing up.
  • Gimbal 2 (G2) has motors on the X and Y axes (X is first in the Euler Rotation order) - its neutral/starting position is pointing right.

The rotation of both gimbals can be expressed as Quaternions, however since both gimbals only have 2 motors they each only have 2 degrees of freedom as a result only a subset of Quaternions can be expressed using the physical gimbals.

G1 has been driven to a particular position and is currently represented by Quaternion Q1.

We need the rotation angles for the motors on G2 so that we can drive it to point in the same direction as G1. We could derive Q2 from the rotation angles, if required.

Since we only have two degrees of freedom we can still "look at" any direction, however we have to sacrifice the "up" ability we typically get with a Quaternion (in most cases there will only be one viable "up" since we don't have a third motor to change it).

The first step is to get the direction G1 is looking, we can do that by multiplying Q1 against the Z unit vector (0, 0, 1).

l = Q1*Z

As the X axis angle is the first in the Euler rotation order we need to project the point into the YZ plane, hence atan2 will give us the rotation around the X axis:

atan2(l.z, l.y)

Since we have a unit vector, we can calculate the rotation around the Y axis as:


Given the math above, the ideal starting position would be with G2 pointing straight up (+Z).

You can still make things work, but technically the starting position is with:

X = 0 and Y = -90

Hence the X motor can turn 90 degrees in either direction, but the Y motor is at the end of its travel hence it can only move 180 degrees in one direction.


You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .