animationmathrotationquaternionsunreal-engine5

Is it possible for a quaternion to represent a rotation pass 180 degree?


I am wondering if quaternions can be used to represent a rotation that is beyond 180 but below 360 degrees?

I am asking this in the context of Unreal Engine's FQuat library where my combined quaternion has a rotation greater than 180 degrees, and it will flip the direction of the resulting quaternion.

Is this unique to Unreal Engine? Or is there something more to quaternions, that I am not aware of?

Any guidance is appreciated, Thanks!

For example, if I combined two 95 degree clockwise quaternions, I expect a 190 degree clockwise quaternion. But in Unreal Engine, it would give a 80 degree counter clockwise quaternion.


Solution

  • Yes, as long as you don't try to keep the sign of the scalar part positive. E.g., here is MATLAB code that forms a quaternion q with a 95 degree rotation. Then multiply by itself to get a quaternion p with a 190 degree rotation. Note that the scalar part of p is negative (scalar is first in MATLAB). Then extract the angles of these two quaternions with the atan2d() function to show that you can recover the angle greater than 180:

    The code:

    angle = 95 % degrees
    q = quaternion(cosd(angle/2),sind(angle/2),0,0) % form quaternion with 95 degree rotation
    p = q * q % form quaternion with 190 degree rotation 
    [a,b,c,d] = q.parts; % extract quaternion parts ...
    qd = [a,b,c,d]; % ... and form as double vector
    [a,b,c,d] = p.parts; % extract quaternion parts ...
    pd = [a,b,c,d]; % ... and form as double vector
    atan2d(norm(qd(2:4)),qd(1))*2 % extract angle of q
    atan2d(norm(pd(2:4)),pd(1))*2 % extract angle of p
    

    The result of running the code:

    angle =
        95
    q = 
      quaternion
         0.67559 + 0.73728i +       0j +       0k
    p = 
      quaternion
        -0.087156 +  0.99619i +        0j +        0k
    ans =
        95
    ans =
       190
    

    I am unfamiliar with Unreal Engine and how it deals with quaternions, so can't advise you on that. But it sounds like maybe something in the way the quaternions are formed, or maybe something in the background angle calculations, is forcing the scalar part to be positive by flipping all of the signs?

    This can actually be a potential problem with attitude control systems that try to move from one attitude to a nearby attitude. If you are not careful with the signs, the control system can inadvertently be sent the "long" way around instead of the short way. To prevent this, one would intentionally check the scalar sign first and adjust the calculations to force the short way is taken.