<p>(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.)</p>

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

<h3>Column vectors</h3>

<p>If you are using <em>column</em> vectors, you will normally transform them by <em>left</em>-multiplying your matrices:</p>

<pre><code>vector = mRotateZ * vector;
vector = mRotateX * vector;
</code></pre>

<p>Of course, you can also do this in one step:</p>

<pre><code>vector = mRotateX * mRotateZ * vector;
</code></pre>

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

<pre><code>A * B * C = (A * B) * C = A * (B * C)
</code></pre>

<p>So we can write</p>

<pre><code>Matrix mRotate = mRotateX * mRotateZ;
vector = mRotate * vector;
</code></pre>

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

<h3>Row vectors</h3>

<p>If, on the other hand, you are using <em>row</em> vectors, you will normally <em>right</em>-multiply your matrices:</p>

<pre><code>vector = vector * mRotateZ;
vector = vector * mRotateX;
</code></pre>

<p>Again, writing it in one step, we get</p>

<pre><code>vector = vector * mRotateZ * mRotateX;
</code></pre>

<p>which can be rewritten as</p>

<pre><code>Matrix mRotate = mRotateZ * mRotateX;
vector = vector * mRotate;
</code></pre>

<p>Notice that in this case, the transformations applied from left to right.</p>

CC BY-SA 3.0
2015-10-13T03:55:31.857Z
I would be very carrfull with that associativity comment its easy to misunderstand
joojaa
CC BY-SA 3.0
2015-10-13T07:36:30.703Z
@joojaa I don't know what exactly you mean, but I've tried to clarify that bit.
Martin Ender
CC BY-SA 3.0
2015-10-13T11:25:49.913Z
Its hard for a layman to separate betveen order you multiply things and the order in which the elements are in multiplication.
joojaa
CC BY-SA 3.0
2015-10-13T11:37:31.017Z
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
joojaa
CC BY-SA 3.0
2015-10-12T15:17:48.963Z
2015-10-13T07:36:02.573Z
2015-10-13T07:36:02.573Z
Martin Ender
<p>Yes, just multiply them in reverse order:</p>

<pre><code>Matrix myrotation = Matrix.CreateRotationX(xrot) * Matrix.CreateRotationZ(zrot);
</code></pre>

<p><strong>EDIT.</strong> 
My answer only applies if you are using column vectors. Please see Martin Büttner detailed answer.</p>

CC BY-SA 3.0
2015-10-09T20:21:31.200Z
I am sorry but I do not get the idea. What do you exactly mean by "reverse order"?
JORGE
CC BY-SA 3.0
2015-10-09T20:27:31.040Z
Multiply x by z instead of z by x;
Stefan Agartsson
CC BY-SA 3.0
2015-10-10T05:11:52.983Z
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.
joojaa
CC BY-SA 3.0
2015-10-10T19:55:37.973Z
Joojaa, thanks for making that clear! Row matrix means reversed order of multiplication, is that correct?
Stefan Agartsson
CC BY-SA 3.0
2015-10-09T19:58:32.230Z
2015-10-13T11:58:18.750Z
2015-10-13T11:58:18.750Z
Stefan Agartsson
<p>From math:</p>

<blockquote>
  <p>There is a 2:1 homomorphism from the unit <a href="https://en.wikipedia.org/wiki/Quaternion" rel="nofollow">quaternions</a> to <a href="https://en.wikipedia.org/wiki/Rotation_group_SO%283%29" rel="nofollow">SO(3)</a> (the rotation group).</p>
</blockquote>

<p>What this (essentially) means is that:</p>

<ol>
<li>Every orientation can be represented as a quaternion</li>
<li>Quaternions represent a single rotation</li>
<li>Multiplication of quaternions produces another quaternion (closure), and is equivalent to composing the rotations.</li>
<li><strong>Therefore <em>any</em> number of rotations can be represented as a single rotation!</strong></li>
</ol>

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

<hr>

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

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

CC BY-SA 3.0
2015-10-18T06:09:20.450Z
2015-10-18T06:09:20.450Z
geometrian
<p>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.</p>

<transformations>
How to combine rotation in 2 axis into one matrix
JORGE
# How to combine rotation in 2 axis into one matrix

(Asked by: username_0 on 2015-10-09)

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.

**Tags:** transformations


---

## 3 answers

### Answer by username_1 on 2015-10-12. Score: 9

(This answer is essentially the same as username_3'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.

> **Comments:**
> **username_2**: I would be very carrfull with that associativity comment its easy to misunderstand  
> **username_1**: @username_2 I don't know what exactly you mean, but I've tried to clarify that bit.  
> **username_2**: Its hard for a layman to separate betveen order you multiply things and the order in which the elements are in multiplication.  
> **username_2**: 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  

---

### Answer by username_3 on 2015-10-09. Score: 6

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 username_1 Büttner detailed answer.

> **Comments:**
> **username_0**: I am sorry but I do not get the idea. What do you exactly mean by "reverse order"?  
> **username_3**: Multiply x by z instead of z by x;  
> **username_2**: 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.  
> **username_3**: username_2, thanks for making that clear! Row matrix means reversed order of multiplication, is that correct?  

---

### Answer by username_4 on 2015-10-18. Score: 3

From math:

> There is a 2:1 homomorphism from the unit [quaternions](https://en.wikipedia.org/wiki/Quaternion) to [SO(3)](https://en.wikipedia.org/wiki/Rotation_group_SO%283%29) (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.


---

