3D Rotation – Calculating Rotation Around a Random Axis

3dmathproblem solving

This is actually a solved problem, but I want to understand why my original method didn't work (hoping someone with more knowledge can explain).

(Keep in mind, I've not very experienced in 3d programming, having only played with the very basic for a little bit…nor do I have a lot of mathematical experience in this area).

I wanted to animate a point rotating around another point at a random axis, say a 45 degrees along the y axis (think of an electron around a nucleus). I know how to rotate using the transform matrix along the X, Y and Z axis, but not an arbitrary (45 degree) axis.

Eventually after some research I found a suggestion: Rotate the point by -45 degrees around the Z so that it is aligned. Then rotate by some increment along the Y axis, then rotate it back +45 degrees for every frame tick. While this certainly worked, I felt that it seemed to be more work then needed (too many method calls, math, etc) and would probably be pretty slow at runtime with many points to deal with.

I thought maybe it was possible to combine all the rotation matrixes involve into 1 rotation matrix and use that as a single operation.

Something like:

 [ cos(-45)    -sin(-45)         0]
 [ sin(-45)     cos(-45)         0]      rotate by -45 along Z 
 [    0            0             1]

multiply by

 [ cos(2)          0       -sin(2)]
 [    0            1            0 ]      rotate by 2 degrees (my increment) along Y
 [ sin(2)          0        cos(2)]

then multiply that result by (in that order)

 [ cos(45)      -sin(45)         0]
 [ sin(45)       cos(45)         0]      rotate by 45 along Z 
 [    0            0             1]

I get 1 mess of a matrix of numbers (since I was working with unknowns and 2 angles), but I felt like it should work. It did not and I found a solution on wiki using a different matirx, but that is something else.

I'm not sure if maybe I made an error in multiplying, but my question is: this is actually a viable way to solve the problem, to take all the separate transformations, combine them via multiplying, then use that or not?

Best Answer

You can indeed compound ('add up') the rotations by multiplication.

I'd consider using Quaternions instead though. They're much nicer to work with and they avoid problems with Euler type rotations (e.g. gimbal lock). You can plug in arbitrary axes of rotation, rather than worrying about X, Y, Z rotations. Quaternion compound rotations nicely -- and if you have interactive 3D objects the user can spin etc., you'll want to compound rotations.

Lots on the web about Quaternions, e.g.: