Blowbox 2017
A 3D Game Engine by Riko Ophorst using DirectX 12
quaternion.h
1 #pragma once
2 
3 #include <DirectXMath.h>
4 
5 namespace DirectX
6 {
7  DirectX::XMFLOAT3 XMQuaternionToEuler(DirectX::XMVECTOR quaternion_vec)
8  {
9  DirectX::XMFLOAT3 output;
10  float sqw;
11  float sqx;
12  float sqy;
13  float sqz;
14 
15  DirectX::XMFLOAT4 quaternion;
16  DirectX::XMStoreFloat4(&quaternion, quaternion_vec);
17 
18  sqw = quaternion.w * quaternion.w;
19  sqx = quaternion.x * quaternion.x;
20  sqy = quaternion.y * quaternion.y;
21  sqz = quaternion.z * quaternion.z;
22 
23  float unit = sqw + sqx + sqy + sqz;
24  float test = quaternion.x * quaternion.w - quaternion.y * quaternion.z;
25 
26  if (test > 0.4995f * unit)
27  {
28  output.y = (2.0f * atan2(quaternion.y, quaternion.x));
29  output.x = (DirectX::XM_PIDIV2);
30 
31  return output;
32  }
33  if (test < -0.4995f * unit)
34  {
35  output.y = (-2.0f * atan2(quaternion.y, quaternion.x));
36  output.x = (-DirectX::XM_PIDIV2);
37 
38  return output;
39  }
40 
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)));
44 
45  return output;
46  }
47 }
Definition: quaternion.h:5