Quaternion¶
C++ type: Quaternion
Description¶
Represents a rotation in 3D space using the quaternion formalism. Quaternions avoid gimbal lock, support smooth slerp interpolation, and are more numerically stable than Euler-angle representations for concatenated rotations.
In Doriax, all Object rotations are stored as quaternions. Euler angles, axis-angle, and matrix forms can be converted to/from quaternions using the provided static and instance methods.
Properties¶
| Type | Name | Default | Langs |
|---|---|---|---|
| float | w | 1.0 |
C++ | Lua |
| float | x | 0.0 |
C++ | Lua |
| float | y | 0.0 |
C++ | Lua |
| float | z | 0.0 |
C++ | Lua |
Static constants¶
| Name | Value |
|---|---|
IDENTITY |
(1, 0, 0, 0) — no rotation |
Methods¶
| Type | Name | Langs |
|---|---|---|
| void | fromEulerAngles | C++ | Lua |
| void | fromAngleAxis | C++ | Lua |
| void | fromAngle | C++ | Lua |
| void | fromAxes | C++ | Lua |
| Matrix4 | getRotationMatrix | C++ | Lua |
| Vector3 | getEulerAngles | C++ | Lua |
| Vector3 | xAxis / yAxis / zAxis | C++ | Lua |
| float | dot | C++ | Lua |
| float | norm | C++ | Lua |
| Quaternion | inverse | C++ | Lua |
| Quaternion | unitInverse | C++ | Lua |
| Quaternion& | normalize | C++ | Lua |
| Quaternion | normalized | C++ | Lua |
| float | normalizeL | C++ | Lua |
| float | getRoll / getPitch / getYaw | C++ | Lua |
| static Quaternion | slerp | C++ | Lua |
| static Quaternion | nlerp | C++ | Lua |
| static Quaternion | squad | C++ | Lua |
| std::string | toString | C++ | Lua |
Enumerations¶
RotationOrder¶
Controls the order in which Euler-angle rotations are applied when constructing a quaternion:
- XYZ — Roll, then Pitch, then Yaw
- XZY, YXZ, YZX, ZXY, ZYX — Other standard combinations
The most common order in Doriax is XYZ.
Method details¶
fromEuler¶
- void fromEulerAngles(float xAngle, float yAngle, float zAngle, const RotationOrder& order)
Constructs the quaternion from Euler angles. Angles are in degrees if Engine::useDegrees is true.
There is also a shorthand constructor:
fromAngleAxis¶
- void fromAngleAxis(float angle, const Vector3& rkAxis)
Constructs the quaternion from an angle-axis representation. Useful for arbitrary-axis rotations.
fromAngle¶
- void fromAngle(float angle)
Constructs a 2D-style rotation around the Z axis. Convenience for 2D scenes.
fromAxes¶
- void fromAxes(const Vector3& xaxis, const Vector3& yaxis, const Vector3& zaxis)
Builds the quaternion from an orthonormal basis of three axes.
getRotationMatrix¶
- Matrix4 getRotationMatrix() const
Converts this quaternion to a Matrix4 rotation matrix.
getEuler¶
- Vector3 getEulerAngles(const RotationOrder& order) const
Extracts Euler angles from this quaternion in the specified rotation order.
xAxis / yAxis / zAxis¶
- Vector3 xAxis() const
- Vector3 yAxis() const
- Vector3 zAxis() const
Returns the local X, Y, or Z axis expressed in world space — equivalent to rotating the corresponding unit vector by this quaternion.
dot¶
- float dot(const Quaternion& rkQ) const
Returns the dot product between two quaternions. Values near 1 indicate similar orientations; near -1 indicate opposite orientations.
norm¶
- float norm() const
Returns the squared length of the quaternion. A normalised quaternion has norm() = 1.
inverse / unitInverse¶
- Quaternion inverse() const
- Quaternion unitInverse() const
inverse() returns the inverse (safe for any non-zero quaternion). unitInverse() is faster but only correct for unit quaternions.
normalize / normalized¶
- Quaternion& normalize()
- Quaternion normalized() const
normalize() scales the quaternion in-place to unit length. normalized() returns a new unit-length copy.
normalizeL¶
- float normalizeL()
Normalises in-place and returns the original norm.
getRoll / getPitch / getYaw¶
- float getRoll() const
- float getPitch() const
- float getYaw() const
Extracts the roll (Z), pitch (X), or yaw (Y) angle component.
slerp¶
- static Quaternion slerp(float t, const Quaternion& q1, const Quaternion& q2)
- static Quaternion slerp(float t, const Quaternion& q1, const Quaternion& q2, bool shortestPath)
Spherical linear interpolation. t=0 returns q1, t=1 returns q2. When shortestPath is true, the interpolation always takes the shorter arc.
nlerp¶
- static Quaternion nlerp(float fT, const Quaternion& rkP, const Quaternion& rkQ)
- static Quaternion nlerp(float fT, const Quaternion& rkP, const Quaternion& rkQ, bool shortestPath)
Normalised linear interpolation. Cheaper than slerp() but not constant-speed; suitable for small angular differences or when speed uniformity is not critical.
squad¶
- static Quaternion squad(float fT, const Quaternion& rkP, const Quaternion& rkA, const Quaternion& rkB, const Quaternion& rkQ)
Spherical cubic (SQUAD) interpolation between two quaternions rkP and rkQ with inner control points rkA and rkB. Used for smooth keyframe animation.
toString¶
- std::string toString() const
Returns a string like "Quaternion(1.000000, 0.000000, 0.000000, 0.000000)".