Directionality of movement along the surface of a sphere with quaternions

185 views Asked by At

I am using a motion tracking device equipped with a gyrometer/accelerometer/magnetometer. The device outputs its rotational orientation at any timepoint as a quaternion.

As I rotate the device from its original orientation, I am trying to calculate two things: (1) the distance along the surface of the sphere from its original orientation (I've actually already completed this step), and (2) the direction of the orientation - but the direction needs to be a simple 1 or -1, not a vector.

Let me explain further what I mean with regard to the direction: Let's assume the device begins in an initial rotation state, and then I rotate it in one direction. After completing some amount of rotation, I then rotate it back to its original position, and then I continue rotating it along that same trajectory - essentially rotating it in the opposite direction of my original rotation.

So, if I have an original quaternion Q0, and then I have another quaternion representing my first rotation Q1, I would then like to say for any future quaternion Qn:

  1. What is the distance of Qn from Q0?
  2. What is the direction (valid values are 1 or -1) of Qn from Q0? Where "1" is "in the same direction as the rotation from Q0 to Q1" and "-1" is "in the opposite direction as the rotation from Q0 to Q1".

Like I said, the "distance" part I have already solved thanks to finding this helpful post: https://math.stackexchange.com/questions/90081/quaternion-distance?newreg=f0fcab1eca8d4a4faaad1ea555d1cdf7

I haven't solved the direction part yet. The following posts have gotten me part of the way there:

  1. Difference between the two quaternions
  2. 'Difference' between two quaternions

But my understanding is still incomplete. Can anyone help elucidate how I could do this? Thanks!

1

There are 1 answers

1
James Tursa On BEST ANSWER

I don’t know whether the quaternion convention you are using is left-chain or right-chain, so I will pick one for example. Suppose P1 is the quaternion that takes you from Q0 to Q1 as follows:

P1 * Q0 = Q1

Then we have

P1 = Q1 * Q0^-1

If the w part of P1 is negative, then flip all of the signs of the P1 elements so that the w part is positive.

If P1w < 0
    P1 = -P1
Endif

The remaining x, y, z parts of P1 then point along the axis of rotation, so we can use that for “direction”. Call this vector v1 = [P1x,P1y,P1z]

Now you have another quaternion Qn and you want to see if it is in the same rotation “direction” as Q1. First generate the following in the same way as we did above:

Pn = Qn * Q0^-1

Flip all the signs of Pn if necessary to ensure Pnw is positive.

Form the vector vn = [Pnx,Pny,Pnz]

If vn is in the same “direction” as v1, then the rotation is in the same direction. Otherwise it is in the opposite direction. I.e.,

If dot(v1,vn) > 0
    Direction is the same
Else
    Direction is opposite
Endif

This all breaks down when you have rotations of 0 or 180 degrees involved, or if you are rotating beyond 180 degrees, so you may need special logic to handle these cases. E.g., if v1 or vn is very close to or identically [0,0,0], what result do you want?

For a different quaternion convention, you may need to start with Q0 * P1 = Q1 and proceed accordingly.