3D Rotation - Again and again

This text comes from IMPHOBIA Issue XII - July 1996


When I started coding in three dimensions I needed 12 muls to rotate each vertex around 3 axis. I changed the order of the steps and was happy to eliminate 2 multiplications. I thought it would be impossible to reduce the number of muls anymore. But I was wrong and heard about a different optimization using only 9 muls. Again, I thought it would be impossible to optimize the algorithm now. But again I was wrong when reading issue 11 of this magazine. Here I found a way to rotate with only 8 muls, using the trick to rotate only around 2 (spherical) axis. This is not acceptable for all kind of 3d-Engine but for demos.

I learned there is always a faster method and again there is one... ...with only 7 muls - and here we are :)
Ice of Anarchy showed in issue 11 how to infer the basic formula to rotate around 2 (spherical) axix with 12 muls:
 1) x'     = -x*sin(P)        + y*cos(P)
 2) y'     = -x*cos(P)*sin(Q) - y*sin(P)*sin(Q) + z*cos(Q)
 3) z'     = -x*cos(P)*cos(Q) - y*sin(P)*cos(Q) - z*sin(Q) + R
First of all we can factor out the sin(Q) in the 2nd line and cos(Q) in the 3rd line (10 muls):
 1) x'     =         -x*sin(P) + y*cos(P)
 2) y'     = sin(Q)*(-x*cos(P) - y*sin(P))      + z*cos(Q)
 3) z'     = cos(Q)*(-x*cos(P) - y*sin(P))      - z*sin(Q) + R
As you can see between the brackets remains the same (8 muls):
 1) XcaYsa =          x*cos(P) + y*sin(P)
 2) x'     =         -x*sin(P) + y*cos(P)
 3) y'     = sin(Q)*(       -XcaYsa      )      + z*cos(Q)
 4) z'     = cos(Q)*(       -XcaYsa      )      - z*sin(Q) + R
For the next step we will rewrite the 1st line using x*sin(P) and y*cos(P) of the 2nd line to remove another mul:
    XcaYsa =           (x+y)*(cos(P)+sin(P))           - x*sin(P) - y*cos(P)
If you expand this again you can see that it still is the same:
           = x*cos(P) + x*sin(P) + y*cos(P) + y*sin(P) - x*sin(P) - y*cos(P)
           = x*cos(P)                       + y*sin(P)
The final formula with only 7 muls:
 1) Xsa    = x*sin(P)
 2) Yca    = y*cos(P)
 3) XcaYsa = (x+y)*(cos(P)+sin(P)) - Xsa - Yca
 4) x'     =          Yca - Xsa
 5) y'     =     z*cos(Q) - sin(Q)*XcaYsa
 6) z'     = R - z*sin(Q) - cos(Q)*XcaYsa
I think this is a great improvement compared to the formula of issue 11 where only one angle was left due to the optimization to control the rotation.

That's all folks... maybe there are faster ways with less or even no (?) muls... who knows!? If you do know, please describe them in an article like it did or email me.

- StarCode -
                                  Contacts:
                                Jan Tegtmeier
                                Reinckeweg  9
                           22399 Hamburg / GERMANY
                        Phone: ++49 / 40 / 606 711 99
                     E-Mail: PV80090@PH80090.HH.eunet.de
PS: CU @ Mekka'97 or even B 4! Greets to everybody on #coders / #coders.ger!