A reimplementation of Mario Kart Wii's physics engine in C++
Loading...
Searching...
No Matches
Quat.hh
1#pragma once
2
3#include "egg/math/Math.hh"
4#include "egg/math/Vector.hh"
5
6namespace EGG {
7
12struct Quatf {
13#ifdef BUILD_DEBUG
14 constexpr Quatf() : w(std::numeric_limits<f32>::signaling_NaN()) {}
15#else
16 constexpr Quatf() = default;
17#endif
18 constexpr Quatf(const Quatf &q) = default;
19 constexpr Quatf(f32 w_, const Vector3f &v_) : v(v_), w(w_) {}
20 constexpr Quatf(f32 w_, f32 x_, f32 y_, f32 z_) : v(x_, y_, z_), w(w_) {}
21 ~Quatf() = default;
22
23 Quatf &operator=(const Quatf &q) {
24 w = q.w;
25 v = q.v;
26
27 return *this;
28 }
29
30 [[nodiscard]] Quatf operator+(const Quatf &rhs) const {
31 return Quatf(w + rhs.w, v + rhs.v);
32 }
33
34 Quatf &operator+=(const Quatf &rhs) {
35 return *this = *this + rhs;
36 }
37
38 [[nodiscard]] Quatf operator*(const Vector3f &vec) const {
39 Vector3f cross = v.cross(vec);
40 Vector3f scale = vec * w;
41 return Quatf(-v.dot(vec), cross + scale);
42 }
43
44 [[nodiscard]] Quatf operator*(f32 scalar) const {
45 return Quatf(w * scalar, v * scalar);
46 }
47
48 Quatf &operator*=(f32 scalar) {
49 return *this = *this * scalar;
50 }
51
54 [[nodiscard]] Quatf operator*(const Quatf &rhs) const {
55 f32 _w = w * rhs.w - v.x * rhs.v.x - v.y * rhs.v.y - v.z * rhs.v.z;
56 f32 _x = v.y * rhs.v.z + (v.x * rhs.w + w * rhs.v.x) - v.z * rhs.v.y;
57 f32 _y = v.z * rhs.v.x + (v.y * rhs.w + w * rhs.v.y) - v.x * rhs.v.z;
58 f32 _z = v.x * rhs.v.y + (v.z * rhs.w + w * rhs.v.z) - v.y * rhs.v.x;
59
60 return Quatf(_w, _x, _y, _z);
61 }
62
63 Quatf &operator*=(const Quatf &q) {
64 return *this = *this * q;
65 }
66
67 bool operator==(const Quatf &rhs) const {
68 return w == rhs.w && v == rhs.v;
69 }
70
71 bool operator!=(const Quatf &rhs) const {
72 return !(*this == rhs);
73 }
74
76 explicit operator std::string() const {
77 return std::format("[0x{:08X}, 0x{:08X}, 0x{:08X}, 0x{:08X}] | [{}, {}, {}, {}]", f2u(v.x),
78 f2u(v.y), f2u(v.z), f2u(w), v.x, v.y, v.z, w);
79 }
80
81 void setRPY(const Vector3f &rpy);
82 void setRPY(f32 r, f32 p, f32 y);
83 void normalise();
84 void makeVectorRotation(const Vector3f &from, const Vector3f &to);
85
87 Quatf conjugate() const {
88 return Quatf(w, -v);
89 }
90
91 Vector3f rotateVector(const Vector3f &vec) const;
92 Vector3f rotateVectorInv(const Vector3f &vec) const;
93 Quatf slerpTo(const Quatf &q2, f32 t) const;
94
97 f32 squaredNorm() const {
98 return w * w + v.squaredLength();
99 }
100
101 f32 norm() const {
102 return Mathf::sqrt(squaredNorm());
103 }
104
107 f32 dot(const Quatf &q) const {
108 return w * q.w + v.dot(q.v);
109 }
110
111 void setAxisRotation(f32 angle, const Vector3f &axis);
112 Quatf multSwap(const Vector3f &v) const;
113 Quatf multSwap(const Quatf &q) const;
114
115 void read(Stream &stream);
116
117 static Quatf FromRPY(const EGG::Vector3f &rpy);
118 static Quatf FromRPY(f32 r, f32 p, f32 y);
119
120 Vector3f v;
121 f32 w;
122
123 static const Quatf ident;
124};
125
126inline constexpr Quatf Quatf::ident = Quatf(1.0f, Vector3f::zero);
127
128} // namespace EGG
A stream of data, abstracted to allow for continuous seeking.
Definition Stream.hh:10
EGG core library.
Definition Archive.cc:6
A quaternion, used to represent 3D rotation.
Definition Quat.hh:12
void normalise()
Scales the quaternion to a unit length.
Definition Quat.cc:28
Quatf operator*(const Quatf &rhs) const
Definition Quat.hh:54
Vector3f rotateVector(const Vector3f &vec) const
Rotates a vector based on the quat.
Definition Quat.cc:55
f32 squaredNorm() const
Computes .
Definition Quat.hh:97
Quatf slerpTo(const Quatf &q2, f32 t) const
Performs spherical linear interpolation.
Definition Quat.cc:84
void setAxisRotation(f32 angle, const Vector3f &axis)
Set the quat given angle and axis.
Definition Quat.cc:111
f32 dot(const Quatf &q) const
Computes .
Definition Quat.hh:107
Vector3f rotateVectorInv(const Vector3f &vec) const
Rotates a vector on the inverse quat.
Definition Quat.cc:69
void setRPY(const Vector3f &rpy)
Sets roll, pitch, and yaw.
Definition Quat.cc:7
Quatf conjugate() const
Computes .
Definition Quat.hh:87
void makeVectorRotation(const Vector3f &from, const Vector3f &to)
Captures rotation between two vectors.
Definition Quat.cc:40
A 3D float vector.
Definition Vector.hh:87
f32 dot(const Vector3f &rhs) const
The dot product between two vectors.
Definition Vector.hh:186
f32 squaredLength() const
The dot product between the vector and itself.
Definition Vector.hh:181