-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathQuaternion.h
230 lines (204 loc) · 7.57 KB
/
Quaternion.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
#pragma once
/**
* Holds a three degree of freedom orientation.
*
* Quaternions have
* several mathematical properties that make them useful for
* representing orientations, but require four items of data to
* hold the three degrees of freedom. These four items of data can
* be viewed as the coefficients of a complex number with three
* imaginary parts. The mathematics of the quaternion is then
* defined and is roughly correspondent to the math of 3D
* rotations. A quaternion is only a valid rotation if it is
* normalised: i.e. it has a length of 1.
*
* @note Angular velocity and acceleration can be correctly
* represented as vectors. Quaternions are only needed for
* orientation.
*/
#include <float.h>
#include <math.h>
#include <directxmath.h>
#include <d3d11_1.h>
#include "Vector3.h"
using namespace DirectX;
class Quaternion
{
public:
/**
* Holds the real component of the quaternion.
*/
float r;
/**
* Holds the first complex component of the
* quaternion.
*/
float i;
/**
* Holds the second complex component of the
* quaternion.
*/
float j;
/**
* Holds the third complex component of the
* quaternion.
*/
float k;
/**
* The default constructor creates a quaternion representing
* a zero rotation.
*/
Quaternion() : r(1), i(0), j(0), k(0) {}
/**
* The explicit constructor creates a quaternion with the given
* components.
*
* @param r The real component of the rigid body's orientation
* quaternion.
*
* @param i The first complex component of the rigid body's
* orientation quaternion.
*
* @param j The second complex component of the rigid body's
* orientation quaternion.
*
* @param k The third complex component of the rigid body's
* orientation quaternion.
*
* @note The given orientation does not need to be normalised,
* and can be zero. This function will not alter the given
* values, or normalise the quaternion. To normalise the
* quaternion (and make a zero quaternion a legal rotation),
* use the normalise function.
*
* @see normalise
*/
Quaternion(const float r, const float i, const float j, const float k)
: r(r), i(i), j(j), k(k)
{
}
/**
* Normalises the quaternion to unit length, making it a valid
* orientation quaternion.
*/
void normalise()
{
float d = r * r + i * i + j * j + k * k;
// Check for zero length quaternion, and use the no-rotation
// quaternion in that case.
if (d < FLT_EPSILON)
{
r = 1;
return;
}
d = static_cast<float>(1.0) / sqrtf(d);
r *= d;
i *= d;
j *= d;
k *= d;
}
/**
* Multiplies the quaternion by the given quaternion.
*
* @param multiplier The quaternion by which to multiply.
*/
void operator *=(const Quaternion& multiplier)
{
Quaternion q = *this;
r = q.r * multiplier.r - q.i * multiplier.i -
q.j * multiplier.j - q.k * multiplier.k;
i = q.r * multiplier.i + q.i * multiplier.r +
q.j * multiplier.k - q.k * multiplier.j;
j = q.r * multiplier.j + q.j * multiplier.r +
q.k * multiplier.i - q.i * multiplier.k;
k = q.r * multiplier.k + q.k * multiplier.r +
q.i * multiplier.j - q.j * multiplier.i;
}
/**
* Adds the given vector to this, scaled by the given amount.
* This is used to update the orientation quaternion by a rotation
* and time.
*
* @param vector The vector to add.
*
* @param scale The amount of the vector to add.
*/
void addScaledVector(const Vector3& vector, float scale)
{
Quaternion q(0,
vector.x * scale,
vector.y * scale,
vector.z * scale);
q *= *this;
r += q.r * 0.5f;
i += q.i * 0.5f;
j += q.j * 0.5f;
k += q.k * 0.5f;
}
void rotateByVector(const Vector3& vector)
{
Quaternion q(0, vector.x, vector.y, vector.z);
(*this) *= q;
}
};
/**
* Inline function that creates a transform matrix from a
* position and orientation.
*/
static inline void CalculateTransformMatrixColumnMajor(XMMATRIX& transformMatrix,
const Vector3& position,
const Quaternion& orientation)
{
transformMatrix.r[0] = XMVectorSetX(transformMatrix.r[0], 1 - 2 * orientation.j * orientation.j - 2 * orientation.k * orientation.k);
transformMatrix.r[0] = XMVectorSetY(transformMatrix.r[0], 2 * orientation.i * orientation.j -
2 * orientation.r * orientation.k);
transformMatrix.r[0] = XMVectorSetZ(transformMatrix.r[0], 2 * orientation.i * orientation.k +
2 * orientation.r * orientation.j);
transformMatrix.r[0] = XMVectorSetW(transformMatrix.r[0], 0.0f);
transformMatrix.r[1] = XMVectorSetX(transformMatrix.r[1], 2 * orientation.i * orientation.j +
2 * orientation.r * orientation.k);
transformMatrix.r[1] = XMVectorSetY(transformMatrix.r[1], 1 - 2 * orientation.i * orientation.i -
2 * orientation.k * orientation.k);
transformMatrix.r[1] = XMVectorSetZ(transformMatrix.r[1], 2 * orientation.j * orientation.k -
2 * orientation.r * orientation.i);
transformMatrix.r[1] = XMVectorSetW(transformMatrix.r[1], 0.0f);
transformMatrix.r[2] = XMVectorSetX(transformMatrix.r[2], 2 * orientation.i * orientation.k -
2 * orientation.r * orientation.j);
transformMatrix.r[2] = XMVectorSetY(transformMatrix.r[2], 2 * orientation.j * orientation.k +
2 * orientation.r * orientation.i);
transformMatrix.r[2] = XMVectorSetZ(transformMatrix.r[2], 1 - 2 * orientation.i * orientation.i -
2 * orientation.j * orientation.j);
transformMatrix.r[2] = XMVectorSetW(transformMatrix.r[2], 0.0f);
transformMatrix.r[3] = XMVectorSetX(transformMatrix.r[3], position.x);
transformMatrix.r[3] = XMVectorSetY(transformMatrix.r[3], position.y);
transformMatrix.r[3] = XMVectorSetZ(transformMatrix.r[3], position.z);
transformMatrix.r[3] = XMVectorSetW(transformMatrix.r[3], 1.0f);
}
static inline void CalculateTransformMatrixRowMajor(XMMATRIX& transformMatrix,
const Vector3& position,
const Quaternion& orientation)
{
transformMatrix.r[0] = XMVectorSetX(transformMatrix.r[0], 1 - 2 * orientation.j * orientation.j - 2 * orientation.k * orientation.k);
transformMatrix.r[0] = XMVectorSetY(transformMatrix.r[0], 2 * orientation.i * orientation.j - 2 * orientation.r * orientation.k);
transformMatrix.r[0] = XMVectorSetZ(transformMatrix.r[0], 2 * orientation.i * orientation.k + 2 * orientation.r * orientation.j);
transformMatrix.r[0] = XMVectorSetW(transformMatrix.r[0], position.x);
transformMatrix.r[1] = XMVectorSetX(transformMatrix.r[1], 2 * orientation.i * orientation.j + 2 * orientation.r * orientation.k);
transformMatrix.r[1] = XMVectorSetY(transformMatrix.r[1], 1 - 2 * orientation.i * orientation.i - 2 * orientation.k * orientation.k);
transformMatrix.r[1] = XMVectorSetZ(transformMatrix.r[1], 2 * orientation.j * orientation.k - 2 * orientation.r * orientation.i);
transformMatrix.r[1] = XMVectorSetW(transformMatrix.r[1], position.y);
transformMatrix.r[2] = XMVectorSetX(transformMatrix.r[2], 2 * orientation.i * orientation.k - 2 * orientation.r * orientation.j);
transformMatrix.r[2] = XMVectorSetY(transformMatrix.r[2], 2 * orientation.j * orientation.k + 2 * orientation.r * orientation.i);
transformMatrix.r[2] = XMVectorSetZ(transformMatrix.r[2], 1 - 2 * orientation.i * orientation.i - 2 * orientation.j * orientation.j);
transformMatrix.r[2] = XMVectorSetW(transformMatrix.r[2], position.z);
/*
transformMatrix.r[3] = XMVectorSetX(transformMatrix.r[3], position.x);
transformMatrix.r[3] = XMVectorSetY(transformMatrix.r[3], position.y);
transformMatrix.r[3] = XMVectorSetZ(transformMatrix.r[3], position.z);
*/
transformMatrix.r[3] = XMVectorSetX(transformMatrix.r[3], 0);
transformMatrix.r[3] = XMVectorSetY(transformMatrix.r[3], 0);
transformMatrix.r[3] = XMVectorSetZ(transformMatrix.r[3], 0);
transformMatrix.r[3] = XMVectorSetW(transformMatrix.r[3], 1.0f);
transformMatrix = XMMatrixTranspose(transformMatrix);
transformMatrix = transformMatrix;
}