矩陣Matrix到尤拉角Euler轉換
阿新 • • 發佈:2021-02-13
參考文獻:
http://www.geometrictools.com/Documentation/EulerAngles.pdf
但是這裡的公式不能直接用,原因是左右手系空間不同,我這邊採用Direct3D預設的右手系,參考:
https://docs.microsoft.com/en-us/windows/win32/direct3d9/d3dxmatrixrotationyawpitchroll
所以需要自行推導右手系公式,已知各個軸旋轉矩陣公式:
,,
尤拉角變換順序為YXZ,則先計算YX矩陣
最終YXZ矩陣
可以直接得知,即,然後需要分三種情況
- ,可知,即,同理
- 當,則,YXZ矩陣可簡化為
根據兩角和公式
,即 ,且結果不唯一 - 當,則,YXZ矩陣簡化為
可得
,即
基於以上思路,就能實現D3DXMATRIX到尤拉角的轉換程式碼
D3DXVECTOR3* D3DXMatrixToEulerAngles(D3DXVECTOR3* pOut, const D3DXMATRIX* pM) { if (pM->_23 < 0.999f) // some fudge for imprecision { if (pM->_23 > -0.999f) // some fudge for imprecision { pOut->x = asin(pM->_23); pOut->y = atan2(-pM->_13, pM->_33); pOut->z = atan2(-pM->_21, pM->_22); } else { // WARNING. Not unique. YA - ZA = atan2(-r01,r00) pOut->x = -D3DX_PI * 0.5f; pOut->y = atan2(-pM->_12, pM->_11); pOut->z = 0.0f; } } else { // WARNING. Not unique. YA + ZA = atan2(r01,r00) pOut->x = D3DX_PI * 0.5f; pOut->y = atan2(pM->_12, pM->_11); pOut->z = 0.0f; } return pOut; }