Jolt Physics
A multi core friendly Game Physics Engine
Loading...
Searching...
No Matches
Quat.inl
Go to the documentation of this file.
1// Jolt Physics Library (https://github.com/jrouwe/JoltPhysics)
2// SPDX-FileCopyrightText: 2021 Jorrit Rouwe
3// SPDX-License-Identifier: MIT
4
6
8{
9#ifdef JPH_USE_SSE
10 __m128 abcd = mValue.mValue;
11 __m128 xyzw = inRHS.mValue.mValue;
12
13 // Names based on logical order, opposite of shuffle order.
14 __m128 abca = _mm_shuffle_ps(abcd, abcd, _MM_SHUFFLE(0, 2, 1, 0));
15 __m128 bcab = _mm_shuffle_ps(abcd, abcd, _MM_SHUFFLE(1, 0, 2, 1));
16 __m128 cabc = _mm_shuffle_ps(abcd, abcd, _MM_SHUFFLE(2, 1, 0, 2));
17 __m128 dddd = _mm_shuffle_ps(abcd, abcd, _MM_SHUFFLE(3, 3, 3, 3));
18
19 __m128 wwwx = _mm_shuffle_ps(xyzw, xyzw, _MM_SHUFFLE(0, 3, 3, 3));
20 __m128 zxyy = _mm_shuffle_ps(xyzw, xyzw, _MM_SHUFFLE(1, 1, 0, 2));
21 __m128 yzxz = _mm_shuffle_ps(xyzw, xyzw, _MM_SHUFFLE(2, 0, 2, 1));
22
23 __m128 m2 = _mm_mul_ps(bcab, zxyy);
24#ifdef JPH_USE_FMADD
25 __m128 m3 = _mm_fmadd_ps(abca, wwwx, m2);
26#else
27 __m128 m1 = _mm_mul_ps(abca, wwwx);
28 __m128 m3 = _mm_add_ps(m1, m2);
29#endif
30
31 // Negate last (logical) component.
32 m3 = _mm_xor_ps(_mm_castsi128_ps(_mm_set_epi32(int(0x80000000), 0, 0, 0)), m3);
33
34#ifdef JPH_USE_FMADD
35 __m128 m5 = _mm_fnmadd_ps(cabc, yzxz, m3);
36 __m128 m7 = _mm_fmadd_ps(dddd, xyzw, m5);
37#else
38 __m128 m4 = _mm_mul_ps(dddd, xyzw);
39 __m128 m5 = _mm_mul_ps(cabc, yzxz);
40 __m128 m6 = _mm_sub_ps(m4, m5);
41 __m128 m7 = _mm_add_ps(m3, m6);
42#endif
43
44 // [(aw+bz)+(dx-cy),(bw+cx)+(dy-az),(cw+ay)+(dz-bx),-(ax+by)+(dw-cz)]
45 return Quat(Vec4(m7));
46#elif defined(JPH_USE_NEON)
47 float32x4_t abcd = mValue.mValue;
48 float32x4_t xyzw = inRHS.mValue.mValue;
49
50 float32x4_t abca = vcopyq_laneq_f32(abcd, 3, abcd, 0);
51 float32x4_t bcab = JPH_NEON_SHUFFLE_F32x4(abcd, abcd, 1, 2, 0, 1);
52 float32x4_t cabc = JPH_NEON_SHUFFLE_F32x4(abcd, abcd, 2, 0, 1, 2);
53 float32x4_t dddd = vdupq_laneq_f32(abcd, 3);
54
55 float32x4_t wwwx = vcopyq_laneq_f32(vdupq_laneq_f32(xyzw, 3), 3, xyzw, 0);
56 float32x4_t zxyy = JPH_NEON_SHUFFLE_F32x4(xyzw, xyzw, 2, 0, 1, 1);
57 float32x4_t yzxz = JPH_NEON_SHUFFLE_F32x4(xyzw, xyzw, 1, 2, 0, 2);
58
59 float32x4_t m1 = vmulq_f32(abca, wwwx);
60 float32x4_t m2 = vmulq_f32(bcab, zxyy);
61 float32x4_t m3 = vaddq_f32(m1, m2);
62
63 uint32x4_t w_neg_mask = JPH_NEON_UINT32x4(0, 0, 0, 0x80000000u);
64 m3 = vreinterpretq_f32_u32(veorq_u32(vreinterpretq_u32_f32(m3), w_neg_mask));
65
66 float32x4_t m4 = vmulq_f32(dddd, xyzw);
67 float32x4_t m5 = vmulq_f32(cabc, yzxz);
68 float32x4_t m6 = vsubq_f32(m4, m5);
69 float32x4_t m7 = vaddq_f32(m3, m6);
70
71 return Quat(Vec4(m7));
72#elif defined(JPH_USE_VSX)
74 Vec4::Type xyzw = inRHS.mValue.mValue;
75
76 Vec4::Type abca = __builtin_shufflevector(abcd, abcd, 0, 1, 2, 0);
77 Vec4::Type bcab = __builtin_shufflevector(abcd, abcd, 1, 2, 0, 1);
78 Vec4::Type cabc = __builtin_shufflevector(abcd, abcd, 2, 0, 1, 2);
79 Vec4::Type dddd = vec_splat(abcd, 3);
80
81 Vec4::Type wwwx = __builtin_shufflevector(xyzw, xyzw, 3, 3, 3, 0);
82 Vec4::Type zxyy = __builtin_shufflevector(xyzw, xyzw, 2, 0, 1, 1);
83 Vec4::Type yzxz = __builtin_shufflevector(xyzw, xyzw, 1, 2, 0, 2);
84
85 Vec4::Type m1 = vec_mul(abca, wwwx);
86 Vec4::Type m2 = vec_mul(bcab, zxyy);
87 Vec4::Type m3 = vec_add(m1, m2);
88
89 const UVec4::Type w_neg_mask = { 0, 0, 0, 0x80000000u };
90 m3 = (Vec4::Type)vec_xor((UVec4::Type)m3, w_neg_mask);
91
92 Vec4::Type m4 = vec_mul(dddd, xyzw);
93 Vec4::Type m5 = vec_mul(cabc, yzxz);
94 Vec4::Type m6 = vec_sub(m4, m5);
95 Vec4::Type m7 = vec_add(m3, m6);
96
97 return Quat(Vec4(m7));
98#else
99 float a = mValue.GetX();
100 float b = mValue.GetY();
101 float c = mValue.GetZ();
102 float d = mValue.GetW();
103
104 float x = inRHS.mValue.GetX();
105 float y = inRHS.mValue.GetY();
106 float z = inRHS.mValue.GetZ();
107 float w = inRHS.mValue.GetW();
108
109 return Quat((a * w + b * z) + (d * x - c * y),
110 (b * w + c * x) + (d * y - a * z),
111 (c * w + a * y) + (d * z - b * x),
112 -(a * x + b * y) + (d * w - c * z));
113#endif
114}
115
117{
118#ifdef JPH_USE_SSE
119 __m128 abc0 = inLHS.mValue;
120 __m128 xyzw = inRHS.mValue.mValue;
121
122 // Names based on logical order, opposite of shuffle order.
123 __m128 abca = _mm_shuffle_ps(abc0, abc0, _MM_SHUFFLE(0, 2, 1, 0));
124 __m128 bcab = _mm_shuffle_ps(abc0, abc0, _MM_SHUFFLE(1, 0, 2, 1));
125 __m128 cabc = _mm_shuffle_ps(abc0, abc0, _MM_SHUFFLE(2, 1, 0, 2));
126
127 __m128 wwwx = _mm_shuffle_ps(xyzw, xyzw, _MM_SHUFFLE(0, 3, 3, 3));
128 __m128 zxyy = _mm_shuffle_ps(xyzw, xyzw, _MM_SHUFFLE(1, 1, 0, 2));
129 __m128 yzxz = _mm_shuffle_ps(xyzw, xyzw, _MM_SHUFFLE(2, 0, 2, 1));
130
131 __m128 m2 = _mm_mul_ps(bcab, zxyy);
132#ifdef JPH_USE_FMADD
133 __m128 m3 = _mm_fmadd_ps(abca, wwwx, m2);
134#else
135 __m128 m1 = _mm_mul_ps(abca, wwwx);
136 __m128 m3 = _mm_add_ps(m1, m2);
137#endif
138
139 // Negate last (logical) component.
140 m3 = _mm_xor_ps(_mm_castsi128_ps(_mm_set_epi32(int(0x80000000), 0, 0, 0)), m3);
141
142 __m128 m4 = _mm_mul_ps(cabc, yzxz);
143
144 // [(aw+bz)-cy,(bw+cx)-az,(cw+ay)-bx,-(ax+by)-cz]
145 return Quat(Vec4(_mm_sub_ps(m3, m4)));
146#elif defined(JPH_USE_NEON)
147 float32x4_t abc0 = inLHS.mValue;
148 float32x4_t xyzw = inRHS.mValue.mValue;
149
150 float32x4_t abca = vcopyq_laneq_f32(abc0, 3, abc0, 0);
151 float32x4_t bcab = JPH_NEON_SHUFFLE_F32x4(abc0, abc0, 1, 2, 0, 1);
152 float32x4_t cabc = JPH_NEON_SHUFFLE_F32x4(abc0, abc0, 2, 0, 1, 2);
153
154 float32x4_t wwwx = vcopyq_laneq_f32(vdupq_laneq_f32(xyzw, 3), 3, xyzw, 0);
155 float32x4_t zxyy = JPH_NEON_SHUFFLE_F32x4(xyzw, xyzw, 2, 0, 1, 1);
156 float32x4_t yzxz = JPH_NEON_SHUFFLE_F32x4(xyzw, xyzw, 1, 2, 0, 2);
157
158 float32x4_t m1 = vmulq_f32(abca, wwwx);
159 float32x4_t m2 = vmulq_f32(bcab, zxyy);
160 float32x4_t m3 = vaddq_f32(m1, m2);
161
162 uint32x4_t w_neg_mask = JPH_NEON_UINT32x4(0, 0, 0, 0x80000000u);
163 m3 = vreinterpretq_f32_u32(veorq_u32(vreinterpretq_u32_f32(m3), w_neg_mask));
164
165 float32x4_t m4 = vmulq_f32(cabc, yzxz);
166 float32x4_t m7 = vsubq_f32(m3, m4);
167
168 return Quat(Vec4(m7));
169#elif defined(JPH_USE_VSX)
170 Vec4::Type abc0 = inLHS.mValue;
171 Vec4::Type xyzw = inRHS.mValue.mValue;
172
173 Vec4::Type abca = __builtin_shufflevector(abc0, abc0, 0, 1, 2, 0);
174 Vec4::Type bcab = __builtin_shufflevector(abc0, abc0, 1, 2, 0, 1);
175 Vec4::Type cabc = __builtin_shufflevector(abc0, abc0, 2, 0, 1, 2);
176
177 Vec4::Type wwwx = __builtin_shufflevector(xyzw, xyzw, 3, 3, 3, 0);
178 Vec4::Type zxyy = __builtin_shufflevector(xyzw, xyzw, 2, 0, 1, 1);
179 Vec4::Type yzxz = __builtin_shufflevector(xyzw, xyzw, 1, 2, 0, 2);
180
181 Vec4::Type m1 = vec_mul(abca, wwwx);
182 Vec4::Type m2 = vec_mul(bcab, zxyy);
183 Vec4::Type m3 = vec_add(m1, m2);
184
185 const UVec4::Type w_neg_mask = { 0, 0, 0, 0x80000000u };
186 m3 = (Vec4::Type)vec_xor((UVec4::Type)m3, w_neg_mask);
187
188 Vec4::Type m4 = vec_mul(cabc, yzxz);
189 Vec4::Type m7 = vec_sub(m3, m4);
190
191 return Quat(Vec4(m7));
192#else
193 float a = inLHS.GetX();
194 float b = inLHS.GetY();
195 float c = inLHS.GetZ();
196
197 float x = inRHS.mValue.GetX();
198 float y = inRHS.mValue.GetY();
199 float z = inRHS.mValue.GetZ();
200 float w = inRHS.mValue.GetW();
201
202 return Quat((a * w + b * z) - c * y,
203 (b * w + c * x) - a * z,
204 (c * w + a * y) - b * x,
205 -(a * x + b * y) - c * z);
206#endif
207}
208
209Quat Quat::sRotation(Vec3Arg inAxis, float inAngle)
210{
211 // returns [inAxis * sin(0.5f * inAngle), cos(0.5f * inAngle)]
212 JPH_ASSERT(inAxis.IsNormalized());
213 Vec4 s, c;
214 Vec4::sReplicate(0.5f * inAngle).SinCos(s, c);
215 return Quat(Vec4::sSelect(Vec4(inAxis) * s, c, UVec4(0, 0, 0, 0xffffffffU)));
216}
217
218void Quat::GetAxisAngle(Vec3 &outAxis, float &outAngle) const
219{
221 Quat w_pos = EnsureWPositive();
222 float abs_w = w_pos.GetW();
223 if (abs_w >= 1.0f)
224 {
225 outAxis = Vec3::sZero();
226 outAngle = 0.0f;
227 }
228 else
229 {
230 outAngle = 2.0f * ACos(abs_w);
231 outAxis = w_pos.GetXYZ().NormalizedOr(Vec3::sZero());
232 }
233}
234
235Vec3 Quat::GetAngularVelocity(float inDeltaTime) const
236{
238
239 // w = cos(angle / 2), ensure it is positive so that we get an angle in the range [0, PI]
240 Quat w_pos = EnsureWPositive();
241
242 // The imaginary part of the quaternion is axis * sin(angle / 2),
243 // if the length is small use the approximation sin(x) = x to calculate angular velocity
244 Vec3 xyz = w_pos.GetXYZ();
245 float xyz_len_sq = xyz.LengthSq();
246 if (xyz_len_sq < 4.0e-4f) // Max error introduced is sin(0.02) - 0.02 = 7e-5 (when w is near 1 the angle becomes more inaccurate in the code below, so don't make this number too small)
247 return (2.0f / inDeltaTime) * xyz;
248
249 // Otherwise calculate the angle from w = cos(angle / 2) and determine the axis by normalizing the imaginary part
250 // Note that it is also possible to calculate the angle through angle = 2 * atan2(|xyz|, w). This is more accurate but also 2x as expensive.
251 float angle = 2.0f * ACos(w_pos.GetW());
252 return (xyz / (Sqrt(xyz_len_sq) * inDeltaTime)) * angle;
253}
254
256{
257 /*
258 Uses (inFrom = v1, inTo = v2):
259
260 angle = arcos(v1 . v2 / |v1||v2|)
261 axis = normalize(v1 x v2)
262
263 Quaternion is then:
264
265 s = sin(angle / 2)
266 x = axis.x * s
267 y = axis.y * s
268 z = axis.z * s
269 w = cos(angle / 2)
270
271 Using identities:
272
273 sin(2 * a) = 2 * sin(a) * cos(a)
274 cos(2 * a) = cos(a)^2 - sin(a)^2
275 sin(a)^2 + cos(a)^2 = 1
276
277 This reduces to:
278
279 x = (v1 x v2).x
280 y = (v1 x v2).y
281 z = (v1 x v2).z
282 w = |v1||v2| + v1 . v2
283
284 which then needs to be normalized because the whole equation was multiplied by 2 cos(angle / 2)
285 */
286
287 float len_v1_v2 = Sqrt(inFrom.LengthSq() * inTo.LengthSq());
288 float w = len_v1_v2 + inFrom.Dot(inTo);
289
290 if (w == 0.0f)
291 {
292 if (len_v1_v2 == 0.0f)
293 {
294 // If either of the vectors has zero length, there is no rotation and we return identity
295 return Quat::sIdentity();
296 }
297 else
298 {
299 // If vectors are perpendicular, take one of the many 180 degree rotations that exist
300 return Quat(Vec4(inFrom.GetNormalizedPerpendicular(), 0));
301 }
302 }
303
304 Vec3 v = inFrom.Cross(inTo);
305 return Quat(Vec4(v, w)).Normalized();
306}
307
308template <class Random>
309Quat Quat::sRandom(Random &inRandom)
310{
311 // Using Uniform Random Rotations - Graphics Gems III - Ken Shoemake
312 float x0 = float(inRandom() - inRandom.min()) / float(inRandom.max() - inRandom.min());
313 float r1 = Sqrt(1.0f - x0), r2 = Sqrt(x0);
314 float theta1 = 2.0f * JPH_PI * float(inRandom() - inRandom.min()) / float(inRandom.max() - inRandom.min());
315 float theta2 = 2.0f * JPH_PI * float(inRandom() - inRandom.min()) / float(inRandom.max() - inRandom.min());
316 Vec4 s, c;
317 Vec4(theta1, theta2, 0, 0).SinCos(s, c);
318 return Quat(s.GetX() * r1, c.GetX() * r1, s.GetY() * r2, c.GetY() * r2);
319}
320
322{
323 Vec4 half(0.5f * inAngles);
324 Vec4 s, c;
325 half.SinCos(s, c);
326
327 float cx = c.GetX();
328 float sx = s.GetX();
329 float cy = c.GetY();
330 float sy = s.GetY();
331 float cz = c.GetZ();
332 float sz = s.GetZ();
333
334 return Quat(
335 cz * sx * cy - sz * cx * sy,
336 cz * cx * sy + sz * sx * cy,
337 sz * cx * cy - cz * sx * sy,
338 cz * cx * cy + sz * sx * sy);
339}
340
342{
343 float x = GetX(), y = GetY(), z = GetZ(), w = GetW();
344 float y_sq = y * y;
345
346 // X
347 float t0 = 2.0f * (w * x + y * z);
348 float t1 = 1.0f - 2.0f * (x * x + y_sq);
349
350 // Y
351 float t2 = 2.0f * (w * y - z * x);
352 t2 = t2 > 1.0f? 1.0f : t2;
353 t2 = t2 < -1.0f? -1.0f : t2;
354
355 // Z
356 float t3 = 2.0f * (w * z + x * y);
357 float t4 = 1.0f - 2.0f * (y_sq + z * z);
358
359 return Vec3(ATan2(t0, t1), ASin(t2), ATan2(t3, t4));
360}
361
363{
364 Quat twist(Vec4(GetXYZ().Dot(inAxis) * inAxis, GetW()));
365 float twist_len = twist.LengthSq();
366 if (twist_len != 0.0f)
367 return twist / Sqrt(twist_len);
368 else
369 return Quat::sIdentity();
370}
371
372void Quat::GetSwingTwist(Quat &outSwing, Quat &outTwist) const
373{
374 float x = GetX(), y = GetY(), z = GetZ(), w = GetW();
375 float s = Sqrt(Square(w) + Square(x));
376 if (s != 0.0f)
377 {
378 outTwist = Quat(x / s, 0, 0, w / s);
379 outSwing = Quat(0, (w * y - x * z) / s, (w * z + x * y) / s, s);
380 }
381 else
382 {
383 // If both x and w are zero, this must be a 180 degree rotation around either y or z
384 outTwist = Quat::sIdentity();
385 outSwing = *this;
386 }
387}
388
389Quat Quat::LERP(QuatArg inDestination, float inFraction) const
390{
391 float scale0 = 1.0f - inFraction;
392 return Quat(scale0 * mValue + inFraction * inDestination.mValue);
393}
394
395Quat Quat::SLERP(QuatArg inDestination, float inFraction) const
396{
397 // Difference at which to LERP instead of SLERP
398 const float delta = 0.0001f;
399
400 // Calc cosine
401 float sign_scale1 = 1.0f;
402 float cos_omega = Dot(inDestination);
403
404 // Adjust signs (if necessary)
405 if (cos_omega < 0.0f)
406 {
407 cos_omega = -cos_omega;
408 sign_scale1 = -1.0f;
409 }
410
411 // Calculate coefficients
412 float scale0, scale1;
413 if (1.0f - cos_omega > delta)
414 {
415 // Standard case (slerp)
416 float omega = ACos(cos_omega);
417 float sin_omega = Sin(omega);
418 scale0 = Sin((1.0f - inFraction) * omega) / sin_omega;
419 scale1 = sign_scale1 * Sin(inFraction * omega) / sin_omega;
420 }
421 else
422 {
423 // Quaternions are very close so we can do a linear interpolation
424 scale0 = 1.0f - inFraction;
425 scale1 = sign_scale1 * inFraction;
426 }
427
428 // Interpolate between the two quaternions
429 return Quat(scale0 * mValue + scale1 * inDestination.mValue).Normalized();
430}
431
433{
434 // Rotating a vector by a quaternion is done by: p' = q * (p, 0) * q^-1 (q^-1 = conjugated(q) for a unit quaternion)
435 // Using Rodrigues formula: https://en.m.wikipedia.org/wiki/Euler%E2%80%93Rodrigues_formula
436 // This is equivalent to: p' = p + 2 * (q.w * q.xyz x p + q.xyz x (q.xyz x p))
437 //
438 // This is:
439 //
440 // Vec3 xyz = GetXYZ();
441 // Vec3 q_cross_p = xyz.Cross(inValue);
442 // Vec3 q_cross_q_cross_p = xyz.Cross(q_cross_p);
443 // Vec3 v = mValue.SplatW3() * q_cross_p + q_cross_q_cross_p;
444 // return inValue + (v + v);
445 //
446 // But we can write out the cross products in a more efficient way:
448 Vec3 xyz = GetXYZ();
450 Vec3 q_cross_p = (inValue.Swizzle<SWIZZLE_Y, SWIZZLE_Z, SWIZZLE_X>() * xyz - yzx * inValue).Swizzle<SWIZZLE_Y, SWIZZLE_Z, SWIZZLE_X>();
451 Vec3 q_cross_q_cross_p = (q_cross_p.Swizzle<SWIZZLE_Y, SWIZZLE_Z, SWIZZLE_X>() * xyz - yzx * q_cross_p).Swizzle<SWIZZLE_Y, SWIZZLE_Z, SWIZZLE_X>();
452 Vec3 v = mValue.SplatW3() * q_cross_p + q_cross_q_cross_p;
453 return inValue + (v + v);
454}
455
457{
459 Vec3 xyz = GetXYZ(); // Needs to be negated, but we do this in the equations below
461 Vec3 q_cross_p = (yzx * inValue - inValue.Swizzle<SWIZZLE_Y, SWIZZLE_Z, SWIZZLE_X>() * xyz).Swizzle<SWIZZLE_Y, SWIZZLE_Z, SWIZZLE_X>();
462 Vec3 q_cross_q_cross_p = (yzx * q_cross_p - q_cross_p.Swizzle<SWIZZLE_Y, SWIZZLE_Z, SWIZZLE_X>() * xyz).Swizzle<SWIZZLE_Y, SWIZZLE_Z, SWIZZLE_X>();
463 Vec3 v = mValue.SplatW3() * q_cross_p + q_cross_q_cross_p;
464 return inValue + (v + v);
465}
466
468{
469 // This is *this * Vec3::sAxisX() written out:
470 JPH_ASSERT(IsNormalized(2.0e-5f));
471 Vec4 t = mValue + mValue;
472 return Vec3(t.SplatX() * mValue + (t.SplatW() * mValue.Swizzle<SWIZZLE_W, SWIZZLE_Z, SWIZZLE_Y, SWIZZLE_X>()).FlipSign<1, 1, -1, 1>() - Vec4(1, 0, 0, 0));
473}
474
476{
477 // This is *this * Vec3::sAxisY() written out:
478 JPH_ASSERT(IsNormalized(2.0e-5f));
479 Vec4 t = mValue + mValue;
480 return Vec3(t.SplatY() * mValue + (t.SplatW() * mValue.Swizzle<SWIZZLE_Z, SWIZZLE_W, SWIZZLE_X, SWIZZLE_Y>()).FlipSign<-1, 1, 1, 1>() - Vec4(0, 1, 0, 0));
481}
482
484{
485 // This is *this * Vec3::sAxisZ() written out:
486 JPH_ASSERT(IsNormalized(2.0e-5f));
487 Vec4 t = mValue + mValue;
488 return Vec3(t.SplatZ() * mValue + (t.SplatW() * mValue.Swizzle<SWIZZLE_Y, SWIZZLE_X, SWIZZLE_W, SWIZZLE_Z>()).FlipSign<1, -1, 1, 1>() - Vec4(0, 0, 1, 0));
489}
490
491void Quat::StoreFloat3(Float3 *outV) const
492{
495}
496
497void Quat::StoreFloat4(Float4 *outV) const
498{
499 mValue.StoreFloat4(outV);
500}
501
503{
505 float w = Sqrt(max(1.0f - v.LengthSq(), 0.0f)); // It is possible that the length of v is a fraction above 1, and we don't want to introduce NaN's in that case so we clamp to 0
506 return Quat(Vec4(v, w));
507}
508
#define JPH_NAMESPACE_END
Definition Core.h:494
#define JPH_NAMESPACE_BEGIN
Definition Core.h:488
#define xyz
Definition HLSLToCPP.h:513
#define yzx
Definition HLSLToCPP.h:516
#define JPH_ASSERT(...)
Definition IssueReporting.h:28
JPH_INLINE constexpr T Square(T inV)
Square a value.
Definition Math.h:70
JPH_INLINE float Sqrt(float inV)
Take the square root of a float value.
Definition Math.h:76
@ SWIZZLE_Z
Use the Z component.
Definition Swizzle.h:14
@ SWIZZLE_W
Use the W component.
Definition Swizzle.h:15
@ SWIZZLE_X
Use the X component.
Definition Swizzle.h:12
@ SWIZZLE_Y
Use the Y component.
Definition Swizzle.h:13
JPH_INLINE float ACos(float inX)
Definition Trigonometry.h:42
JPH_INLINE float ATan2(float inY, float inX)
Arc tangent of y / x using the signs of the arguments to determine the correct quadrant (returns valu...
Definition Trigonometry.h:74
JPH_NAMESPACE_BEGIN JPH_INLINE float Sin(float inX)
Sine of x (input in radians)
Definition Trigonometry.h:12
JPH_INLINE float ASin(float inX)
Definition Trigonometry.h:35
Class that holds 3 floats. Used as a storage class. Convert to Vec3 for calculations.
Definition Float3.h:13
Class that holds 4 float values. Convert to Vec4 to perform calculations.
Definition Float4.h:11
Definition Quat.h:33
static Quat sRandom(Random &inRandom)
Random unit quaternion.
Definition Quat.inl:309
JPH_INLINE Vec3 InverseRotate(Vec3Arg inValue) const
Rotate a vector by the inverse of this quaternion.
Definition Quat.inl:456
JPH_INLINE float GetW() const
Get W component (real part)
Definition Quat.h:79
static JPH_INLINE Quat sMultiplyImaginary(Vec3Arg inLHS, QuatArg inRHS)
Multiply a quaternion with imaginary components and no real component (x, y, z, 0) with a quaternion.
Definition Quat.inl:116
static Quat sEulerAngles(Vec3Arg inAngles)
Conversion from Euler angles. Rotation order is X then Y then Z (RotZ * RotY * RotX)....
Definition Quat.inl:321
JPH_INLINE float GetY() const
Get Y component (imaginary part j)
Definition Quat.h:73
JPH_INLINE float GetZ() const
Get Z component (imaginary part k)
Definition Quat.h:76
static JPH_INLINE Quat sRotation(Vec3Arg inAxis, float inAngle)
Rotation from axis and angle.
Definition Quat.inl:209
JPH_INLINE float GetX() const
Get X component (imaginary part i)
Definition Quat.h:70
JPH_INLINE Vec3 GetAngularVelocity(float inDeltaTime) const
Calculate angular velocity given that this quaternion represents the rotation that is reached after i...
Definition Quat.inl:235
JPH_INLINE Quat LERP(QuatArg inDestination, float inFraction) const
Definition Quat.inl:389
static JPH_INLINE Quat sLoadFloat3Unsafe(const Float3 &inV)
Load 3 floats from memory (X, Y and Z component and then calculates W) reads 32 bits extra which it d...
Definition Quat.inl:502
JPH_INLINE float LengthSq() const
Definition Quat.h:136
JPH_INLINE void GetAxisAngle(Vec3 &outAxis, float &outAngle) const
Get axis and angle that represents this quaternion, outAngle will always be in the range .
Definition Quat.inl:218
static JPH_INLINE Quat sIdentity()
Definition Quat.h:104
JPH_INLINE void GetSwingTwist(Quat &outSwing, Quat &outTwist) const
Definition Quat.inl:372
JPH_INLINE void StoreFloat4(Float4 *outV) const
Store as 4 floats.
Definition Quat.inl:497
static JPH_INLINE Quat sFromTo(Vec3Arg inFrom, Vec3Arg inTo)
Definition Quat.inl:255
friend Quat operator*(float inValue, QuatArg inRHS)
Definition Quat.h:158
JPH_INLINE Quat EnsureWPositive() const
Ensures that the W component is positive by negating the entire quaternion if it is not....
Definition Quat.h:191
JPH_INLINE Quat GetTwist(Vec3Arg inAxis) const
Definition Quat.inl:362
JPH_INLINE float Dot(QuatArg inRHS) const
Dot product.
Definition Quat.h:182
Quat()=default
Intentionally not initialized for performance reasons.
JPH_INLINE Vec3 RotateAxisZ() const
Rotate a the vector (0, 0, 1) with this quaternion.
Definition Quat.inl:483
JPH_INLINE Vec3 RotateAxisX() const
Rotate a the vector (1, 0, 0) with this quaternion.
Definition Quat.inl:467
JPH_INLINE Vec3 RotateAxisY() const
Rotate a the vector (0, 1, 0) with this quaternion.
Definition Quat.inl:475
Vec3 GetEulerAngles() const
Conversion to Euler angles. Rotation order is X then Y then Z (RotZ * RotY * RotX)....
Definition Quat.inl:341
bool IsNormalized(float inTolerance=1.0e-5f) const
If the length^2 of this quaternion is within the range [1 - inTolerance, 1 + inTolerance].
Definition Quat.h:60
JPH_INLINE void StoreFloat3(Float3 *outV) const
Store as 3 floats to memory (X, Y and Z component). Ensures that W is positive before storing.
Definition Quat.inl:491
JPH_INLINE Vec3 GetXYZ() const
Get the imaginary part of the quaternion.
Definition Quat.h:82
Vec4 mValue
4 vector that stores [x, y, z, w] parts of the quaternion
Definition Quat.h:264
JPH_INLINE Quat SLERP(QuatArg inDestination, float inFraction) const
Definition Quat.inl:395
Definition UVec4.h:12
struct { uint32 mData[4];} Type
Definition UVec4.h:24
Definition Vec3.h:17
JPH_INLINE float Dot(Vec3Arg inV2) const
Dot product.
Definition Vec3.inl:1021
JPH_INLINE Vec3 Normalized() const
Normalize vector.
Definition Vec3.inl:1065
JPH_INLINE Vec3 Cross(Vec3Arg inV2) const
Cross product.
Definition Vec3.inl:932
JPH_INLINE Vec3 GetNormalizedPerpendicular() const
Get normalized vector that is perpendicular to this vector.
Definition Vec3.inl:1226
JPH_INLINE float GetX() const
Get individual components.
Definition Vec3.h:133
JPH_INLINE bool IsNormalized(float inTolerance=1.0e-6f) const
Test if length^2 of this vector is within the range [1 - inTolerance, 1 + inTolerance].
Definition Vec3.inl:1126
JPH_INLINE Vec3 NormalizedOr(Vec3Arg inZeroValue) const
Normalize vector or return inZeroValue if the length of the vector is zero.
Definition Vec3.inl:1070
Type mValue
Definition Vec3.h:314
JPH_INLINE float GetY() const
Definition Vec3.h:134
JPH_INLINE void StoreFloat3(Float3 *outV) const
Store 3 floats to memory.
Definition Vec3.inl:1154
JPH_INLINE float LengthSq() const
Squared length of vector.
Definition Vec3.inl:1036
static JPH_INLINE Vec3 sZero()
Vector with all zeros.
Definition Vec3.inl:133
static JPH_INLINE Vec3 sLoadFloat3Unsafe(const Float3 &inV)
Load 3 floats from memory (reads 32 bits extra which it doesn't use)
Definition Vec3.inl:179
JPH_INLINE float GetZ() const
Definition Vec3.h:135
JPH_INLINE Vec3 Swizzle() const
Swizzle the elements in inV.
Definition Vec4.h:14
JPH_INLINE Vec4 SplatX() const
Replicate the X component to all components.
Definition Vec4.inl:876
JPH_INLINE Vec3 SplatW3() const
Replicate the W component to all components.
Definition Vec4.inl:1002
JPH_INLINE Vec4 FlipSign() const
Flips the signs of the components, e.g. FlipSign<-1, 1, -1, 1>() will flip the signs of the X and Z c...
Definition Vec4.inl:1177
JPH_INLINE float GetW() const
Definition Vec4.h:129
JPH_INLINE Vec4 SplatY() const
Replicate the Y component to all components.
Definition Vec4.inl:894
JPH_INLINE Vec4 SplatZ() const
Replicate the Z component to all components.
Definition Vec4.inl:912
JPH_INLINE Vec4 SplatW() const
Replicate the W component to all components.
Definition Vec4.inl:930
JPH_INLINE float GetX() const
Get individual components.
Definition Vec4.h:126
JPH_INLINE Vec4 Swizzle() const
Swizzle the elements in inV.
struct { float mData[4];} Type
Definition Vec4.h:26
Type mValue
Definition Vec4.h:324
JPH_INLINE float GetZ() const
Definition Vec4.h:128
static JPH_INLINE Vec4 sSelect(Vec4Arg inNotSet, Vec4Arg inSet, UVec4Arg inControl)
Component wise select, returns inNotSet when highest bit of inControl = 0 and inSet when highest bit ...
Definition Vec4.inl:405
JPH_INLINE float GetY() const
Definition Vec4.h:127
static JPH_INLINE Vec4 sReplicate(float inV)
Replicate inV across all components.
Definition Vec4.inl:107
void SinCos(Vec4 &outSin, Vec4 &outCos) const
Calculate the sine and cosine for each element of this vector (input in radians)
Definition Vec4.inl:1278
JPH_INLINE void StoreFloat4(Float4 *outV) const
Store 4 floats to memory.
Definition Vec4.inl:1186