September 29, 2013

Quaternion as 3D Rotation Hand

Quaternion algebra was introduced by Hamilton in 1843. It is powerful three dimensional complex number system. This article will present how to use quaternion  as rotation tools which is very useful for the calculation involving three-dimensional rotation in 3D computer graphics.

In order to match with OpenGL, this article apply the right hand rule to explain quaternion . If you come across another article which based on the left hand rule , do not be surprised to see that the terms are different to those described in this article.


Quaternion multiplication of basis element

  Q  =  w  +  x i   +   yj   +  zk  

   where  i , j , k  is complex number   w,x,y,z  are real number

     i2  =    j2  =   k2  =   i j k   =   -1

    ij = k  ;   ji = -k

    jk = i  ;   kj = -i

    ki =j   ;  ik = -j

   The  x,y,z  is rotation axis  and   w  is some thing which relate to the rotation angle required.

Quaternion Product

      define qutaternion   Q1  and Q2

      Q1  =  w1  +  x1 i   +   y1 j   +  z1 k     =   w1 +  v1     where  v1  = (  x1 i   +   y1 j   +  z1 k  )

      Q2  =  w2 +  x2 i    +   y2 j   +  z2 k     =   w2 +  v2     weher  v2  =  ( x2 i   +   y2 j   +  z2 k )

      Q1*Q2  =  w1w2 +  w1v2 +w2v1 + v1v2

      v1v2    =    - ( x1x2 + y1y2 +z1z2 ),     ==>   -  ( v1.v2 )  dot product
                   
                        + (y1z2  - z1y2)  i ,
                        + (z1x2  - x1z2)  j ,             ==>       ( v1 x v2 )      cross product
                        + (x1y2  - y1x2)  k

       Q1*Q2    =   w1w2 + w1v2 +w2v1 - v1.v2 + v1 x v2
                      = ( w1w2  - v1.v2)    +  (w1v2 +w2v1  + v1 x v2)
                      = ........
 
                   replace the terms with

                   i2 =   j2 =  k2 =   i j k   =   -1
 
                   ij = k  ;   ji = -k
 
                   jk = i  ;   kj = -i
 
                   ki =j   ;  ik = -j


**** Finally you will get ****

   Q1*Q2   =        ( w1w2  - x1x2 - y1y2 -z1z2 )                  

                        + ( w1x2 + w2x1 +y1z2 -z1y2 )  i

                        + ( w1y2 + w2y1 +z1x2 -x1z2 )  j

                        + ( w1z2 + w2z1 +x1y2 -y1x2 )  k

          @!!       Q1*Q2   !=   Q2*Q1



Normalise  Quaternion

      Q  =  w  +  x i   +   y j   +  z k    
      Qmag  =  ( w2 + x2 + y2 + z2  )1/2
      Qnormal   =  w/Qmag +  (x/Qmag)  i   +   (y/Qmag)  j   + ( z/Qmag)   k    

Conjugate  Quaternion

     Q  =  w +  x  i   +   y  j   +  z  k   
     Qconjugate  =  w  -  x  i  -   y j   -  z  k


HOW TO USE QUATERNION AS ROTATION HAND

     
   
     Suppose that you want to rotate the object  located at point     P =  (x0,y0,z0
     with angle  θ  around   Axis   =  ax x + ay y + az z

   Step 1 ::  Build  quaternion   Q
   
      Q  =  w +  x  i   +   y  j   +  z  k    
   
     where  ::

      w  =  cos(θ/2)
      x   =  ax   * sin(θ/2)
      y   =  ay   * sin(θ/2)
      z   =  az   * sin(θ/2)
   
      Then your quaternion rotation hand is 

      Q  =  cos(θ/2)  +  (ax *sin(θ/2)) i  + (ay *sin(θ/2)) j   +  (az*sin(θ/2))  k  

   Step 2 ::  Normalise  quaternion   Q  => Qnormal

   Step 3 ::  Conjugate quaternion   Qnormal   = > Qconjugate

   Step 4 :: Transform object coordinate at point  P(x0,y0,z0 into quaternion format  Qp
       w  =  0
       x   =  x0
       y   =  y0
       z   =  z0
      Qp  =  0 + x0 i + yj+ zk

    Step 5 :: Get final coordinate from equation

    Qfinal  = Qnormal  * Qp  *  Qconjugate  =     w +  xf  i   +   yf  j   +  zf  k  

    where  xf,yf,zf    is  new coordinate of object resulting from rotation.