Quaternion
A quaternion for representing and interpolating 3D rotation.
Use a unit Quaternion for rotation. Quaternion.Identity means no rotation; a zero struct is not identity. Slerp interpolates orientations rather than averaging Euler components.
Inputs must be normalized; weight is a transition fraction. Equivalent orientations may have every quaternion component sign reversed, so raw-component equality is not orientation equality. Rotation stores neither Entity translation nor scale.
Declarations checked against C# SDK 1.0.0. Expand a name to see declarations and documentation.
Constructors4
Quaternion4 overloads
public Quaternion(Basis basis);Constructs a Quaternion defined by the given values.
Note: Only normalized quaternions represent rotation; if these values are not normalized, the new Quaternion will not be a valid rotation.
| Name | Type | Default |
|---|---|---|
basis | Basis | — |
basisThe Basis to construct from.
public Quaternion(Vector3 arcFrom, Vector3 arcTo);Constructs a Quaternion defined by the given values.
Note: Only normalized quaternions represent rotation; if these values are not normalized, the new Quaternion will not be a valid rotation.
| Name | Type | Default |
|---|---|---|
arcFrom | Vector3 | — |
arcTo | Vector3 | — |
public Quaternion(Vector3 axis, float angle);Constructs a Quaternion defined by the given values.
Note: Only normalized quaternions represent rotation; if these values are not normalized, the new Quaternion will not be a valid rotation.
| Name | Type | Default |
|---|---|---|
axis | Vector3 | — |
angle | float | — |
axisThe axis to rotate around. Must be normalized.
angleThe angle to rotate, in radians.
public Quaternion(float x, float y, float z, float w);Constructs a Quaternion defined by the given values.
Note: Only normalized quaternions represent rotation; if these values are not normalized, the new Quaternion will not be a valid rotation.
| Name | Type | Default |
|---|---|---|
x | float | — |
y | float | — |
z | float | — |
w | float | — |
xX component of the quaternion (imaginary i axis part).
yY component of the quaternion (imaginary j axis part).
zZ component of the quaternion (imaginary k axis part).
wW component of the quaternion (real part).
Properties2
IdentityProperty
public static Quaternion Identity { get; }The identity quaternion, representing no rotation. This has the same rotation as Basis.Identity.
If a Vector3 is rotated (multiplied) by this quaternion, it does not change.
Note: In C#, this constant is equivalent to creating a [constructor Quaternion] without any arguments. It can be used to make your code clearer, and for consistency with C#.
ItemProperty
public float this[int index] { get; set; }Access quaternion components using their index.
Methods36
AngleTofloat
public float AngleTo(Quaternion to);Returns the angle between this quaternion and to. This is the magnitude of the angle you would need to rotate by to get from one to the other.
Note: The magnitude of the floating-point error for this method is abnormally high, so methods such as is_zero_approx will not work reliably.
| Name | Type | Default |
|---|---|---|
to | Quaternion | — |
toThe other quaternion.
floatThe angle between the quaternions.
Dotfloat
public float Dot(Quaternion b);Returns the dot product between this quaternion and with.
This is equivalent to (quat.x * with.x) + (quat.y * with.y) + (quat.z * with.z) + (quat.w * with.w).
| Name | Type | Default |
|---|---|---|
b | Quaternion | — |
bThe other quaternion.
floatThe dot product.
Equals2 overloads
public bool Equals(Quaternion other);Returns true if this quaternion and other are equal.
| Name | Type | Default |
|---|---|---|
other | Quaternion | — |
otherThe other quaternion to compare.
boolWhether or not the quaternions are exactly equal.
public override bool Equals(object? obj);Returns true if this quaternion and obj are equal.
| Name | Type | Default |
|---|---|---|
obj | object | — |
objThe other object to compare.
boolWhether or not the quaternion and the other object are exactly equal.
ExpQuaternion
public Quaternion Exp();Returns the exponential of this quaternion. The rotation axis of the result is the normalized rotation axis of this quaternion, the angle of the result is the length of the vector part of this quaternion.
QuaternionFromEulerQuaternion
public static Quaternion FromEuler(Vector3 eulerYXZ);Constructs a new Quaternion from the given Vector3 of Euler angles, in radians. In Reckfell, Euler angles always use intrinsic order. This method always uses the intrinsic YXZ convention (EULER_ORDER_YXZ).
| Name | Type | Default |
|---|---|---|
eulerYXZ | Vector3 | — |
eulerYXZEuler angles that the quaternion will be rotated by.
QuaternionGetAnglefloat
public float GetAngle();Returns the angle of the rotation represented by this quaternion. Note: The quaternion must be normalized.
floatGetAxisVector3
public Vector3 GetAxis();Returns the rotation axis of the rotation represented by this quaternion.
Vector3GetEulerVector3
public Vector3 GetEuler(EulerOrder order = EulerOrder.Yxz);Returns this quaternion's rotation as a Vector3 of Euler angles, in radians.
The order of each consecutive rotation can be changed with order (see EulerOrder constants). In Reckfell, Euler angles always use intrinsic order. By default, the intrinsic YXZ convention is used (EULER_ORDER_YXZ): since we are decomposing, local Z (roll) is calculated first, then local X (pitch), and lastly local Y (yaw). When using the opposite method Quaternion.FromEuler to compose a rotation, this order is reversed.
| Name | Type | Default |
|---|---|---|
order | EulerOrder | EulerOrder.Yxz |
Vector3The Euler angle representation of this quaternion.
GetHashCodeint
public override int GetHashCode();Serves as the hash function for Quaternion.
intA hash code for this quaternion.
InverseQuaternion
public Quaternion Inverse();Returns the inverse version of this quaternion, inverting the sign of every component except Quaternion.W.
QuaternionThe inverse quaternion.
IsEqualApproxbool
public bool IsEqualApprox(Quaternion other);Returns true if this quaternion and to are approximately equal, by calling @GlobalScope.is_equal_approx on each component.
| Name | Type | Default |
|---|---|---|
other | Quaternion | — |
otherThe other quaternion to compare.
boolWhether or not the quaternions are approximately equal.
IsFinitebool
public bool IsFinite();Returns true if this quaternion is finite, by calling @GlobalScope.is_finite on each component.
boolWhether this vector is finite or not.
IsNormalizedbool
public bool IsNormalized();Returns true if this quaternion is normalized. See also Quaternion.Normalized.
boolA bool for whether the quaternion is normalized or not.
Lengthfloat
public float Length();Returns this quaternion's length, also called magnitude.
floatLengthSquaredfloat
public float LengthSquared();Returns this quaternion's length, squared.
Note: This method is faster than Quaternion.Length, so prefer it if you only need to compare quaternion lengths.
floatLogQuaternion
public Quaternion Log();Returns the logarithm of this quaternion. Multiplies this quaternion's rotation axis by its rotation angle, and stores the result in the returned quaternion's vector part (Quaternion.X, Quaternion.Y, and Quaternion.Z). The returned quaternion's real part (Quaternion.W) is always 0.0.
QuaternionNormalizedQuaternion
public Quaternion Normalized();Returns a copy of this quaternion, normalized so that its length is 1.0. See also Quaternion.IsNormalized.
QuaternionThe normalized quaternion.
SlerpQuaternion
public Quaternion Slerp(Quaternion to, float weight);Spherically interpolates between unit orientation quaternions. Weight is a transition fraction, not angular speed or seconds.
| Name | Type | Default |
|---|---|---|
to | Quaternion | — |
weight | float | — |
toThe destination quaternion for interpolation. Must be normalized.
weightA value on the range of 0.0 to 1.0, representing the amount of interpolation.
QuaternionThe resulting quaternion of the interpolation.
SlerpniQuaternion
public Quaternion Slerpni(Quaternion to, float weight);Performs a spherical-linear interpolation with the to quaternion, given a weight and returns the result. Unlike Quaternion.Slerp, this method does not check if the rotation path is smaller than 90 degrees. Both this quaternion and to must be normalized.
| Name | Type | Default |
|---|---|---|
to | Quaternion | — |
weight | float | — |
toThe destination quaternion for interpolation. Must be normalized.
weightA value on the range of 0.0 to 1.0, representing the amount of interpolation.
QuaternionThe resulting quaternion of the interpolation.
SphericalCubicInterpolateQuaternion
public Quaternion SphericalCubicInterpolate(Quaternion b, Quaternion preA, Quaternion postB, float weight);Performs a spherical cubic interpolation between quaternions pre_a, this quaternion, b, and post_b, by the given amount weight.
| Name | Type | Default |
|---|---|---|
b | Quaternion | — |
preA | Quaternion | — |
postB | Quaternion | — |
weight | float | — |
bThe destination quaternion.
preAA quaternion before this quaternion.
postBA quaternion after b.
weightA value on the range of 0.0 to 1.0, representing the amount of interpolation.
QuaternionThe interpolated quaternion.
SphericalCubicInterpolateInTimeQuaternion
public Quaternion SphericalCubicInterpolateInTime(Quaternion b, Quaternion preA, Quaternion postB, float weight, float bT, float preAT, float postBT);Performs a spherical cubic interpolation between quaternions pre_a, this quaternion, b, and post_b, by the given amount weight.
It can perform smoother interpolation than Quaternion.SphericalCubicInterpolate by the time values.
| Name | Type | Default |
|---|---|---|
b | Quaternion | — |
preA | Quaternion | — |
postB | Quaternion | — |
weight | float | — |
bT | float | — |
preAT | float | — |
postBT | float | — |
bThe destination quaternion.
preAA quaternion before this quaternion.
postBA quaternion after b.
weightA value on the range of 0.0 to 1.0, representing the amount of interpolation.
QuaternionThe interpolated quaternion.
ToString2 overloads
public override string ToString();Converts this Quaternion to a string.
stringA string representation of this quaternion.
public string ToString(string? format);Converts this Quaternion to a string with the given format.
| Name | Type | Default |
|---|---|---|
format | string | — |
stringA string representation of this quaternion.
op_AdditionQuaternion
public static Quaternion operator +(Quaternion left, Quaternion right);Adds each component of the left Quaternion
to the right Quaternion. This operation is not
meaningful on its own, but it can be used as a part of a
larger expression, such as approximating an intermediate
rotation between two nearby rotations.
| Name | Type | Default |
|---|---|---|
left | Quaternion | — |
right | Quaternion | — |
leftThe left quaternion to add.
rightThe right quaternion to add.
QuaternionThe added quaternion.
op_DivisionQuaternion
public static Quaternion operator /(Quaternion left, float right);Divides each component of the Quaternion
by the given System.Single. This operation is not
meaningful on its own, but it can be used as a part of a
larger expression.
| Name | Type | Default |
|---|---|---|
left | Quaternion | — |
right | float | — |
leftThe quaternion to divide.
rightThe value to divide by.
QuaternionThe divided quaternion.
op_Equalitybool
public static bool operator ==(Quaternion left, Quaternion right);Returns true if the quaternions are exactly equal.
Note: Due to floating-point precision errors, consider using
Quaternion.IsEqualApprox instead, which is more reliable.
| Name | Type | Default |
|---|---|---|
left | Quaternion | — |
right | Quaternion | — |
leftThe left quaternion.
rightThe right quaternion.
boolWhether or not the quaternions are exactly equal.
op_Inequalitybool
public static bool operator !=(Quaternion left, Quaternion right);Returns true if the quaternions are not equal.
Note: Due to floating-point precision errors, consider using
Quaternion.IsEqualApprox instead, which is more reliable.
| Name | Type | Default |
|---|---|---|
left | Quaternion | — |
right | Quaternion | — |
leftThe left quaternion.
rightThe right quaternion.
boolWhether or not the quaternions are not equal.
op_Multiply5 overloads
public static Quaternion operator *(Quaternion left, Quaternion right);Composes these two quaternions by multiplying them together. This has the effect of rotating the second quaternion (the child) by the first quaternion (the parent).
| Name | Type | Default |
|---|---|---|
left | Quaternion | — |
right | Quaternion | — |
leftThe parent quaternion.
rightThe child quaternion.
QuaternionThe composed quaternion.
public static Vector3 operator *(Quaternion quaternion, Vector3 vector);Returns a Vector3 rotated (multiplied) by the quaternion.
| Name | Type | Default |
|---|---|---|
quaternion | Quaternion | — |
vector | Vector3 | — |
quaternionThe quaternion to rotate by.
vectorA Vector3 to transform.
Vector3The rotated Vector3.
public static Quaternion operator *(Quaternion left, float right);Multiplies each component of the Quaternion
by the given System.Single. This operation is not
meaningful on its own, but it can be used as a part of a
larger expression.
| Name | Type | Default |
|---|---|---|
left | Quaternion | — |
right | float | — |
leftThe quaternion to multiply.
rightThe value to multiply by.
QuaternionThe multiplied quaternion.
public static Vector3 operator *(Vector3 vector, Quaternion quaternion);Returns a Vector3 rotated (multiplied) by the inverse quaternion.
vector * quaternion is equivalent to quaternion.Inverse() * vector. See Quaternion.Inverse.
| Name | Type | Default |
|---|---|---|
vector | Vector3 | — |
quaternion | Quaternion | — |
vectorA Vector3 to inversely rotate.
quaternionThe quaternion to rotate by.
Vector3The inversely rotated Vector3.
public static Quaternion operator *(float left, Quaternion right);Multiplies each component of the Quaternion
by the given System.Single. This operation is not
meaningful on its own, but it can be used as a part of a
larger expression.
| Name | Type | Default |
|---|---|---|
left | float | — |
right | Quaternion | — |
leftThe value to multiply by.
rightThe quaternion to multiply.
QuaternionThe multiplied quaternion.
op_SubtractionQuaternion
public static Quaternion operator -(Quaternion left, Quaternion right);Subtracts each component of the left Quaternion
by the right Quaternion. This operation is not
meaningful on its own, but it can be used as a part of a
larger expression.
| Name | Type | Default |
|---|---|---|
left | Quaternion | — |
right | Quaternion | — |
leftThe left quaternion to subtract.
rightThe right quaternion to subtract.
QuaternionThe subtracted quaternion.
op_UnaryNegationQuaternion
public static Quaternion operator -(Quaternion quat);Returns the negative value of the Quaternion.
This is the same as writing
new Quaternion(-q.X, -q.Y, -q.Z, -q.W). This operation
results in a quaternion that represents the same rotation.
| Name | Type | Default |
|---|---|---|
quat | Quaternion | — |
quatThe quaternion to negate.
QuaternionThe negated quaternion.
op_UnaryPlusQuaternion
public static Quaternion operator +(Quaternion quat);Returns the same value as if the + was not there.
Unary + does nothing, but sometimes it can make your
code more readable.
| Name | Type | Default |
|---|---|---|
quat | Quaternion | — |
quatThe quaternion to do nothing to.
QuaternionThe original quaternion.
Fields and constants4
WField
public float W;W component of the quaternion. This is the "real" part. Note: Quaternion components should usually not be manipulated directly.
XField
public float X;X component of the quaternion. This is the value along the "imaginary" i axis.
Note: Quaternion components should usually not be manipulated directly.
YField
public float Y;Y component of the quaternion. This is the value along the "imaginary" j axis.
Note: Quaternion components should usually not be manipulated directly.
ZField
public float Z;Z component of the quaternion. This is the value along the "imaginary" k axis.
Note: Quaternion components should usually not be manipulated directly.