-
Notifications
You must be signed in to change notification settings - Fork 0
Quaternion Types
ExodusCoder9 edited this page Jun 20, 2026
·
1 revision
Quaterniond and Quaternionf are immutable Java records. All operations return new instances.
// Identity
Quaterniond q = new Quaterniond(); // (0, 0, 0, 1)
// From axis-angle
Quaterniond q = Quaterniond.fromAxisAngle(axis, angle);
Quaterniond q = Quaterniond.fromAxisAngleDeg(axis, angleDeg);
// From Euler angles (various orders)
Quaterniond q = Quaterniond.fromEulerAnglesXYZ(xAngle, yAngle, zAngle);
Quaterniond q = Quaterniond.fromEulerAnglesZYX(zAngle, yAngle, xAngle);
Quaterniond q = Quaterniond.fromEulerAnglesYXZ(yAngle, xAngle, zAngle);
// From rotation between vectors
Quaterniond q = Quaterniond.rotateTo(fromDir, toDir);
// Look-at rotation
Quaterniond q = Quaterniond.lookAt(direction, up);
// From matrix
Quaterniond q = new Quaterniond(matrix3d);
Quaterniond q = new Quaterniond(matrix4d);
Operations
// Arithmetic
Quaterniond sum = q1.add(q2);
Quaterniond diff = q1.sub(q2);
Quaterniond product = q1.mul(q2);
Quaterniond scaled = q1.mul(0.5);
Quaterniond conj = q1.conjugate();
Quaterniond inv = q1.invert();
// Length
double len = q1.length();
double lenSq = q1.lengthSquared();
Quaterniond norm = q1.normalize();
// Angle between quaternions
double angle = q1.angle(q2);
double dot = q1.dot(q2);
// Interpolation
Quaterniond lerped = q1.lerp(q2, t); // linear (no normalize)
Quaterniond nlerped = q1.nlerp(q2, t); // normalized linear
Quaterniond slerped = q1.slerp(q2, t); // spherical linear
Quaterniond squadded = q1.squad(a, b, q2, t); // cubic spline
// Exponent, log, power
Quaterniond log = q.log();
Quaterniond exp = q.exp();
Quaterniond pow = q.pow(2.0);
Quaterniond sqrt = q.sqrt();
Conversion
Matrix4f m = q.toMatrix(); // to 4×4 rotation matrix
Matrix3f m3 = q.toMatrix3(); // to 3×3 rotation matrix
AxisAngle4f aa = q.toAxisAngle(); // to axis-angle
Vector3f euler = q.getEulerAnglesXYZ();
Vector Rotation
Vector3f rotated = q.transform(v); // rotate vector by quaternion
Vector3f x = q.positiveX(); // rotated +X basis
Vector3f y = q.positiveY(); // rotated +Y basis
Vector3f z = q.positiveZ(); // rotated +Z basis
Static Constants
Quaterniond.IDENTITY // (0, 0, 0, 1)
Memory I/O
DoubleBuffer buf = DoubleBuffer.allocate(4);
q.writeToBuffer(buf);
buf.flip();
Quaterniond back = Quaterniond.fromBuffer(buf);
MemorySegment seg = arena.allocate(32);
q.writeToMemorySegment(seg, 0);
Quaterniond back2 = Quaterniond.fromMemorySegment(seg, 0);