# How to Build Rotation Matrix From Orientation Vectors

Here is a vector in 3-d space:
$V=\begin{bmatrix}x\\y\\z\end{bmatrix}$

We can express this vector with three linearly-independent vectors and coefficients:
$V=\begin{bmatrix}1\\0\\0\end{bmatrix}x+\begin{bmatrix}0\\1\\0\end{bmatrix}y+\begin{bmatrix}0\\0\\1\end{bmatrix}z$

We can also write this in matrix form:
$V=\begin{bmatrix}1&0&0\\0&1&0\\0&0&1\end{bmatrix}\begin{bmatrix}x\\y\\z\end{bmatrix}=\mathbb{I}\begin{bmatrix}x\\y\\z\end{bmatrix}$

The three vectors $\begin{bmatrix}1\\0\\0\end{bmatrix},\begin{bmatrix}0\\1\\0\end{bmatrix},\begin{bmatrix}0\\0\\1\end{bmatrix}$ are called as a basis in 3-d space, as they can be used to express any vector in 3-d space. Especially they are called as the standard basis, because the basis is what we normally think in.

Of course, we can have another basis to express the same V. Let’s try one:
$V=\begin{bmatrix}1/2\\0\\0\end{bmatrix}u+\begin{bmatrix}0\\1/2\\0\end{bmatrix}v+\begin{bmatrix}0\\0\\1/2\end{bmatrix}w$

Similarly, in matrix form:
$V=\begin{bmatrix}1/2&0&0\\0&1/2&0\\0&0&1/2\end{bmatrix}\begin{bmatrix}u\\v\\w\end{bmatrix}$
where $u=2x,v=2y,w=2z$.

Now, let’s say we have three vectors that represent the orientation of an object in the world. Notice that we need at least two vectors; one to represent where the object looks at, and the other to indicate the up direction. If we have two linearly independent vectors, we can easily create the third one by cross-producting the two.

Now, let’s call the three vectors as side(or right, X), forward(Z), up(Y) vectors each, and the orientation basis altogether.

Let there be an arbitrary point $P$. In standard basis, $P$ is written as:
$P=\begin{bmatrix}1&0&0\\0&1&0\\0&0&1\end{bmatrix}\begin{bmatrix}P_x\\P_y\\P_z\end{bmatrix}$

In orientation basis, $P$ is written as:
$P=\begin{bmatrix}X_x&Y_x&Z_x\\X_y&Y_y&Z_y\\X_z&Y_z&Z_z\end{bmatrix}\begin{bmatrix}Q_x\\Q_y\\Q_z\end{bmatrix}$
Notice that $Q$ is P $P$, but expressed in the orientation basis.

Since they are the same P:
$P=\begin{bmatrix}1&0&0\\0&1&0\\0&0&1\end{bmatrix}\begin{bmatrix}P_x\\P_y\\P_z\end{bmatrix}=\begin{bmatrix}X_x&Y_x&Z_x\\X_y&Y_y&Z_y\\X_z&Y_z&Z_z\end{bmatrix}\begin{bmatrix}Q_x\\Q_y\\Q_z\end{bmatrix}$

To find Q expressed in the standard basis, we multifly the inverse of the orientation basis matrix to each side’s left:
$\begin{bmatrix}X_x&Y_x&Z_x\\X_y&Y_y&Z_y\\X_z&Y_z&Z_z\end{bmatrix}^{-1}\begin{bmatrix}P_x\\P_y\\P_z\end{bmatrix}=\begin{bmatrix}Q_x\\Q_y\\Q_z\end{bmatrix}$

Now, $Q$ is expressed in the standard basis. To make it clear, we can write it as:
$\begin{bmatrix}X_x&Y_x&Z_x\\X_y&Y_y&Z_y\\X_z&Y_z&Z_z\end{bmatrix}^{-1}\begin{bmatrix}P_x\\P_y\\P_z\end{bmatrix}=\begin{bmatrix}1&0&0\\0&1&0\\0&0&1\end{bmatrix}\begin{bmatrix}Q_x\\Q_y\\Q_z\end{bmatrix}$

We can induce from it that the inverse of orientation basis matrix transforms any point $P$ in orientation basis to be expressed in the standard basis.

Notice, because the orientation basis vectors are all orthogonal to each other, we can inverse the the matrix simply by transposing the matrix:
$\begin{bmatrix}X_x&Y_x&Z_x\\X_y&Y_y&Z_y\\X_z&Y_z&Z_z\end{bmatrix}^{-1}=\begin{bmatrix}X_x&Y_x&Z_x\\X_y&Y_y&Z_y\\X_z&Y_z&Z_z\end{bmatrix}^{T}=\begin{bmatrix}X_x&X_y&X_z\\Y_x&Y_y&Y_z\\Z_x&Z_y&Z_z\end{bmatrix}$

In conclusion, given that we know forward, up, side vectors of an object, making the rotational transform matrix to the world space is very easy and efficient, as putting them in rows in order does the trick.

Posted in Game Programming, Mathematics, Uncategorized and tagged , , , , .