8
$\begingroup$

I already know about the matrices I have to use in order to perform rotations. If I have to rotate in z-axis and then in x-axis, I would do it in 2 steps. My question is, is it possible to combine both rotations into a single matrix? I will appreciate your feedback.

$\endgroup$
8
$\begingroup$

(This answer is essentially the same as Stefan's but I wanted to add some detail about row and column vectors, and how to determine which you are using.)

Yes, this is possible, but the details depend on whether you represent your vectors as rows or columns.

Column vectors

If you are using column vectors, you will normally transform them by left-multiplying your matrices:

vector = mRotateZ * vector;
vector = mRotateX * vector;

Of course, you can also do this in one step:

vector = mRotateX * mRotateZ * vector;

But matrix multiplication is associative, which means it doesn't matter which multiplication is performed first:

A * B * C = (A * B) * C = A * (B * C)

So we can write

Matrix mRotate = mRotateX * mRotateZ;
vector = mRotate * vector;

We have now created a single matrix, which is equivalent to first rotating about Z and second about X. This generalises trivially for any number of transformations. Notice that transformations are applied from right to left.

Row vectors

If, on the other hand, you are using row vectors, you will normally right-multiply your matrices:

vector = vector * mRotateZ;
vector = vector * mRotateX;

Again, writing it in one step, we get

vector = vector * mRotateZ * mRotateX;

which can be rewritten as

Matrix mRotate = mRotateZ * mRotateX;
vector = vector * mRotate;

Notice that in this case, the transformations applied from left to right.

$\endgroup$
  • 1
    $\begingroup$ I would be very carrfull with that associativity comment its easy to misunderstand $\endgroup$ – joojaa Oct 13 '15 at 3:55
  • $\begingroup$ @joojaa I don't know what exactly you mean, but I've tried to clarify that bit. $\endgroup$ – Martin Ender Oct 13 '15 at 7:36
  • $\begingroup$ Its hard for a layman to separate betveen order you multiply things and the order in which the elements are in multiplication. $\endgroup$ – joojaa Oct 13 '15 at 11:25
  • $\begingroup$ so they do not understand the difference between assiocative and commutative. so if you talk of the order of multiplication many may think of commutativity $\endgroup$ – joojaa Oct 13 '15 at 11:37
6
$\begingroup$

Yes, just multiply them in reverse order:

Matrix myrotation = Matrix.CreateRotationX(xrot) * Matrix.CreateRotationZ(zrot);

EDIT. My answer only applies if you are using column vectors. Please see Martin Büttner detailed answer.

$\endgroup$
  • $\begingroup$ I am sorry but I do not get the idea. What do you exactly mean by "reverse order"? $\endgroup$ – JORGE Oct 9 '15 at 20:21
  • 1
    $\begingroup$ Multiply x by z instead of z by x; $\endgroup$ – Stefan Agartsson Oct 9 '15 at 20:27
  • 1
    $\begingroup$ actually order is arbitrary one can model using row vectors and one can model column vectors. The computation yelds same result in both but the multiplication order changes. But yes this is sortof the right answer. $\endgroup$ – joojaa Oct 10 '15 at 5:11
  • $\begingroup$ Joojaa, thanks for making that clear! Row matrix means reversed order of multiplication, is that correct? $\endgroup$ – Stefan Agartsson Oct 10 '15 at 19:55
3
$\begingroup$

From math:

There is a 2:1 homomorphism from the unit quaternions to SO(3) (the rotation group).

What this (essentially) means is that:

  1. Every orientation can be represented as a quaternion
  2. Quaternions represent a single rotation
  3. Multiplication of quaternions produces another quaternion (closure), and is equivalent to composing the rotations.
  4. Therefore any number of rotations can be represented as a single rotation!

Think about that. Starting from object space, you can rotate your object into any orientation using only a single rotation.


I would like to point out that bringing quaternions in wasn't just random math-ese. In contrast to the other answers, the favored approach in graphics is actually to represent rotations as quaternions, since they take up less space and are faster to combine.

There are easily-Googleable ways to convert between rotation matrices and quaternions, depending on which you prefer. The point is that rotations are the quaternions in a mathematical sense, so combinations thereof are also single rotations.

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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