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?
4
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.:
-
http://en.wikipedia.org/wiki/Quaternions_and_spatial_rotation
-
http://www.ogre3d.org/tikiwiki/tiki-index.php?page=Quaternion+and+Rotation+Primer
-
https://stackoverflow.com/questions/8919086/why-are-quaternions-used-for-rotations
2
It’s been a while since I did much matrix maths, but I’m pretty sure your approach is valid.
Sounds like you need to reverse the order of the transforms when you multiply those matrices together. When you combine matrix transforms (of any sort) you always start with the last one and work backwards.
1
The problem you get by rotating the axis separately is that you can experience what is called ‘gimbal lock’, a state where the rotations on the separate axis make it overlap in a way that your calculations will not produce the desired effect because the separate calculations interfere with the end result due to overlap. The solution to this is to specify the axis in a vector and rotation expressed in the form of an imaginary number. The way to calculate this is to use
Quaternions.
all types of rotation calculation will eventually yield a 4×4 matrix, as this is always the required form for expressing the final transformation. This implies that you can indeed add up the calculations through multiplication, but as described before, the additions can interfere with each other in certain cases.
1