quaternion
struct quaternionAvailable without an import.
A 3D rotation represented by four floating-point components (x, y, z, w), with w the
scalar part. Useful for composing and interpolating rotations without Euler-angle gimbal lock.
Rotation methods accept angle values.
api fn Main() : int32 {
var rotation = quaternion.FromAxisAngle(float3.UnitZ, 90.degrees);
var turned = quaternion.RotateVector(rotation, float3.UnitX);
return turned.y > 0.999f && turned.x > -0.001f && turned.x < 0.001f ? 1 : 0;
}
Members
xfield
public float xThe X component of the vector part.
yfield
public float yThe Y component of the vector part.
zfield
public float zThe Z component of the vector part.
wfield
public float wThe scalar component.
constructorconstructor
public constructor (float x, float y, float z, float w)Stores x/y/z/w without normalization. Rotation operations expect unit quaternions unless they explicitly normalize.
FromFloat4constructor
public constructor FromFloat4 (float4 v)Copies x/y/z/w from a vector without normalization.
Identitygetter
public static get Identity() : quaternionThe identity rotation (0, 0, 0, 1).
lengthSquaredgetter
public get lengthSquared() : floatReturns the sum of squares of all four components.
lengthgetter
public get length() : floatReturns the Euclidean magnitude of all four components.
normalizedgetter
public get normalized() : quaternionReturns a unit quaternion, or Identity when the magnitude is zero.
conjugategetter
public get conjugate() : quaternionNegates the vector part, preserving w. Equals the inverse for a unit quaternion.
inversegetter
public get inverse() : quaternionReturns the conjugate divided by squared magnitude, or Identity for a zero quaternion.
isIdentitygetter
public get isIdentity() : boolTests exactly for (0, 0, 0, 1), without tolerance or sign-equivalent rotation matching.
toFloat4getter
public get toFloat4() : float4Copies x/y/z/w into a four-component vector.
Dotmethod
public static fn Dot(quaternion a, quaternion b) : floatReturns the four-component dot product.
FromAxisAnglemethod
public static fn FromAxisAngle(float3 axis, angle a) : quaternionNormalizes the axis and constructs its rotation by the supplied angle. Supply a nonzero axis.
FromEulermethod
public static fn FromEuler(float3 eulerRadians) : quaternionConstructs from X/Y/Z Euler angles in radians, composed as Z * Y * X (X applied first to a column vector).
RotateVectormethod
public static fn RotateVector(quaternion q, float3 v) : float3Rotates v by q. Supply a unit quaternion.
Slerpmethod
public static fn Slerp(quaternion a, quaternion b, float t) : quaternionInterpolates unit rotations along the shorter arc, correcting the sign of b when needed. Uses normalized linear interpolation near parallel inputs. Does not clamp t; returns a normalized result.
NLerpmethod
public static fn NLerp(quaternion a, quaternion b, float t) : quaternionLinearly interpolates components and normalizes. Does not clamp t or choose a sign-equivalent shorter arc; a zero intermediate returns Identity.
Anglemethod
public static fn Angle(quaternion a, quaternion b) : angleReturns the smaller angular separation between normalized rotations in [0, pi]. Treats opposite-sign quaternions as the same rotation and zero quaternions as Identity.
ToFloat3x3method
public static fn ToFloat3x3(quaternion q) : float3x3Normalizes q and returns its column-major 3D rotation matrix. A zero quaternion produces Identity.
ToFloat4x4method
public static fn ToFloat4x4(quaternion q) : float4x4Normalizes q and returns its homogeneous rotation matrix with no translation. A zero quaternion produces Identity.
ToStringmethod
public fn ToString() : stringFormats x/y/z/w in parentheses separated by commas.
ToStringBuffermethod
public fn ToStringBuffer(StringBuilder builder) : int32Appends the component representation and returns the number of UTF-8 bytes appended.