3 #include <DirectXMath.h> 7 DirectX::XMFLOAT3 XMQuaternionToEuler(DirectX::XMVECTOR quaternion_vec)
9 DirectX::XMFLOAT3 output;
15 DirectX::XMFLOAT4 quaternion;
16 DirectX::XMStoreFloat4(&quaternion, quaternion_vec);
18 sqw = quaternion.w * quaternion.w;
19 sqx = quaternion.x * quaternion.x;
20 sqy = quaternion.y * quaternion.y;
21 sqz = quaternion.z * quaternion.z;
23 float unit = sqw + sqx + sqy + sqz;
24 float test = quaternion.x * quaternion.w - quaternion.y * quaternion.z;
26 if (test > 0.4995f * unit)
28 output.y = (2.0f * atan2(quaternion.y, quaternion.x));
29 output.x = (DirectX::XM_PIDIV2);
33 if (test < -0.4995f * unit)
35 output.y = (-2.0f * atan2(quaternion.y, quaternion.x));
36 output.x = (-DirectX::XM_PIDIV2);
41 output.x = (asin(2.0f * (quaternion.w * quaternion.x - quaternion.z * quaternion.y)));
42 output.y = (atan2(2.0f * quaternion.w * quaternion.y + 2.0f * quaternion.z * quaternion.x, 1 - 2.0f * (sqx + sqy)));
43 output.z = (atan2(2.0f * quaternion.w * quaternion.z + 2.0f * quaternion.x * quaternion.y, 1 - 2.0f * (sqz + sqx)));
Definition: quaternion.h:5