Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

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?

share|improve this question
1  
Shouldn't those be 4x4 matrix when you are working on 3x3 space? –  Manoj R Nov 27 '12 at 11:11
    
@ManojR: homogeneous coordinates allow transformation of such 3D problems into 4D space (en.wikipedia.org/wiki/Homogeneous_coordinates), where 4x4 matrizes can be used uniformly for rotations and translations. But the original question can be answered in 3D space, without over-complicating it by adding a different concept. –  Doc Brown Nov 27 '12 at 11:47
    
@mitim: google for "3d rotation arbitrary axis" and you will find plenty of tutorials. –  Doc Brown Nov 27 '12 at 15:54
    
I did try googling at first but I kept getting results in the context of 3d modeling and rotating my mesh. I did come across 4x4 matrices on one link but wasn't sure exactly how that worked (in my head the 4th d was 'time'). =b –  mitim Nov 28 '12 at 8:04

3 Answers 3

up vote 5 down vote accepted

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.:

share|improve this answer
    
I believe the day I actually seriously worked with Quaternions was the day I understood that maybe 3D programming was probably too hard for me and I should stick to the easy stuff :) –  glenatron Nov 27 '12 at 13:33
    
Oh, I've never heard of "Quaternions" before. Looks like I have some reading to do. Thanks. –  mitim Nov 28 '12 at 7:58

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 4x4 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.

share|improve this answer
    
I have heard of "gimbal lock" before, but this was in some 3d modeling software. I shall have to read about it in this context, along with "Quaternions" as suggested by someone else. Thanks. –  mitim Nov 28 '12 at 8:01

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.

share|improve this answer
    
Hm, I've never heard of the reversing rule (?), but I'll give it a shot to see anyways just to see what the result would be. Thanks. –  mitim Nov 28 '12 at 7:59

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.