# 空間中3D對象的旋轉矩陣

Skimming the math. Starting with a quaternion $Q=w+\left(x,y,z\right)$ then we can rotate $\mathbf{v}$ by:

$$\mathbf{v}' = Q\mathbf{v}Q^{-1}$$

and if $Q$ is unit magnitude this reduces to:

$$\mathbf{v}' = Q\mathbf{v}Q^*$$

To create a matrix we need to apply the rotation to the basis set to form our three equations:

$$\mathbf{x} = \left(1,0,0\right) \\ \mathbf{y} = \left(0,1,0\right) \\ \mathbf{z} = \left(0,0,1\right) \\$$

which expanded and reduced gives:

$$\mathbf{x}' = Q\mathbf{x}Q^{-1} = \left(1 - 2 \left(y^2+z^2\right), 2\left(xy+wz\right), 2\left(xz-wy\right) \right) \\ \mathbf{y}' = Q\mathbf{y}Q^{-1} = \left(2 \left(xy-wz\right), 1 - 2\left(x^2+z^2\right), 2\left(wx+yz\right) \right) \\ \mathbf{z}' = Q\mathbf{z}Q^{-1} = \left(2 \left(wy+xz\right), 2\left(yz-wx\right), 1 - 2\left(x^2+y^2\right) \right)$$

Sticking to the math convention of column vectors, then we shove the three equations into the first three columns and to add a translation by $\left(t_x,t_y,t_z\right)$ we shove that into the last column giving:

$${ \left( \begin{array}{ccc} 1 - 2 \left(y^2+z^2\right) & 2\left(xy-wz\right) & 2\left(xz+wy\right) & t_x\\ 2 \left(xy+wz\right) & 1 - 2\left(x^2+z^2\right) & 2\left(wx-yz\right) & t_y \\ 2 \left(wy-xz\right) & 2\left(yz+wx\right) & 1 - 2\left(x^2+y^2\right) & t_z \\ 0 & 0 & 0 & 1 \end{array} \right) }$$