Search code examples
math3drotationquaternionseuler-angles

Rotating multiple points around axis using Quaternion


I want to simply rotate multiple points (a shape) around X, Y, Z axis using Quaternions. I convert my Euler Angles to Quaternion so the quaternion has been setup and seems good.

I have my things setup like this:

  • q (quaternion) -> W: 0.99, X: 0.07, Y: 0, Z: 0. (which is an ~17.18° rotation around X axis)
  • p (point) -> X: -0.35, Y: 1.4, Z: 0.35.
  • p' (rotated point) -> ?

I am new to quaternion, I don't know what letters stand for what values in the operations that I want to use to rotate the points.


Solution

  • For rotations, you use a unit quaternion, such that W^2 + X^2 + Y^2 + Z^2 = 1

    The (X, Y, Z) portion of the quaternion can be considered a vector along the rotation axis. The magnitude of this vector component encodes the rotation angle in a less-than-obvious way:

    |X,Y,Z| = sqrt(X^2 + Y^2 + Z^2) = sin(rotation_angle/2)
    

    To complete the quaternion, we have the real component W:

    W = cos(rotation_angle/2)
    

    The advantage of using a quaternion for rotation is that it has a simple multiplication rule, and avoids gimbal lock (unlike Euler angles), while having only 1 excess degree of freedom (unlike rotation matrices, which have 6 excess DOF's).

    It is possible to use quaternions to rotate points directly, by first converting the point into a "pure imaginary quaternion": P = (W=0, X=p.x, Y=p.y, Z=p.z) (note that the vector component of this quaternion is the same as the point's coordinates). Then, you compute the rotated point by quaternion multiplication as follows:

    either:  P' = Q* P Q
    or:      P' = Q P Q*
    (depending on convention)
    

    where Q* is the conjugate of quaternion Q. The real component of the result will be zero: P'.W = 0, while the vector component (P'.X, P'.Y, P'.Z) will be the rotated point coordinates.

    However, the usual way to use rotation quaternions for graphics purposes is to turn them into an equivalent rotation matrix. You can work out the details from the above formula and the quaternion multiplication rules, but the resulting 3x3 rotation matrix is something like:

    [  W^2+X^2-Y^2-Z^2   2(XY-WZ)          2(ZX+WY)         ]
    [  2(XY+WZ)          W^2+Y^2-X^2-Z^2   2(YZ-WX)         ]
    [  2(ZX-WY)          2(YZ+WX)          W^2+Z^2-X^2-Y^2  ]