概述
方法来自于《3D数学基础,图形与游戏开发》这本书。
学习这些转换可以加深自己对这些知识点的理解~两两转换一共有六种情况,下面就依次列举一下这六种情况。
1.从欧拉角转换到矩阵
这种情况的转换不难,还记得我们的旋转矩阵长啥样吗,欧拉角的三个值就是坐标系绕着特定轴旋转的值,注意是坐标系旋转哦,点的旋转值是和坐标系的旋转值相反的,需要把旋转矩阵的角度取反。
比如下面就是绕y轴的旋转矩阵,角度h取反了,
[ c o s ( − h ) 0 − s i n ( − h ) 0 1 0 s i n ( − h ) 0 c o s ( − h ) ] \begin{bmatrix} cos(-h)&0&-sin(-h)\\ 0&1&0\\ sin(-h)&0&cos(-h)\\ \end{bmatrix} ⎣⎡cos(−h)0sin(−h)010−sin(−h)0cos(−h)⎦⎤
我们根据特定的顺序把三个旋转矩阵乘起来就行了,很简单的。
书上是先绕y轴旋转再绕x轴旋转,再绕z轴旋转,得到的结果为:
[ c o s h c o s b + s i n h s i n p s i n b − c o s h s i n b + s i n h s i n p c o s b s i n h c o s p s i n b c o s p c o s b c o s p − s i n p − s i n h c o s b + c o s h s i n p s i n b s i n b s i n h + c o s h s i n p c o s b c o s h c o s p ] \begin{bmatrix} coshcosb+sinhsinpsinb&-coshsinb+sinhsinpcosb&sinhcosp\\ sinbcosp&cosbcosp&-sinp\\ -sinhcosb+coshsinpsinb&sinbsinh+coshsinpcosb&coshcosp\\ \end{bmatrix} ⎣⎡coshcosb+sinhsinpsinbsinbcosp−sinhcosb+coshsinpsinb−coshsinb+sinhsinpcosbcosbcospsinbsinh+coshsinpcosbsinhcosp−sinpcoshcosp⎦⎤
这个旋转矩阵是从惯性坐标系旋转到物体坐标系的旋转矩阵。h为绕y轴的旋转角度,p为绕x轴的旋转角度,b为绕z轴的旋转角度。
2.从旋转矩阵到欧拉角
当我们知道了从欧拉角到旋转矩阵的矩阵形式,倒推是一件非常简单的事情,假设上面的矩阵为M,则M23 = -sinp,我们能直接得到绕x轴的旋转角度p,然后把M21/M22能直接得到tanb,所以也能得到绕z的旋转角度b,把M13/M33也能直接得到tanh,然后就能得到角度h,于是这一切问题都解决了,是不是很容易。
3.从四元数转换到旋转矩阵
[ n x 2 ( 1 − c o s θ ) + c o s θ n x n y ( 1 − c o s θ ) + n z s i n θ n x n z ( 1 − c o s θ ) − n y s i n θ n x n y ( 1 − c o s θ ) − n z s i n θ n y 2 ( 1 − c o s θ ) + c o s θ n y n z ( 1 − c o s θ ) + n x s i n θ n x n z ( 1 − c o s θ ) + n y s i n θ n y n z ( 1 − 0 c o s θ ) − n x s i n θ n x 2 ( 1 − c o s θ ) + c o s θ ] \begin{bmatrix} n_x^2(1-cos\theta)+cos\theta & n_xn_y(1-cos\theta)+n_zsin\theta&n_xn_z(1-cos\theta)-n_ysin\theta\\ n_xn_y(1-cos\theta)-n_zsin\theta&n_y^2(1-cos\theta)+cos\theta&n_yn_z(1-cos\theta)+n_xsin_\theta\\ n_xn_z(1-cos\theta)+n_ysin\theta&n_yn_z(1-0cos\theta)-n_xsin\theta&n_x^2(1-cos\theta)+cos\theta \end{bmatrix} ⎣⎡nx2(1−cosθ)+cosθn