Struct Quaternion
Represents a Quaternion.
Inherited Members
Namespace: OpenTK.Mathematics
Assembly: OpenTK.Mathematics.dll
Syntax
[Serializable]
public struct Quaternion : IEquatable<Quaternion>, IFormattable
Constructors
Quaternion(Vector3)
Initializes a new instance of the Quaternion struct from given Euler angles in radians. The rotations will get applied in following order:
- Around X, 2. Around Y, 3. Around Z.
Declaration
public Quaternion(Vector3 eulerAngles)
Parameters
Type | Name | Description |
---|---|---|
Vector3 | eulerAngles | The counterclockwise euler angles as a Vector3. |
Quaternion(Vector3, float)
Initializes a new instance of the Quaternion struct.
Declaration
public Quaternion(Vector3 v, float w)
Parameters
Type | Name | Description |
---|---|---|
Vector3 | v | The vector part. |
float | w | The w part. |
Quaternion(float, float, float)
Initializes a new instance of the Quaternion struct from given Euler angles in radians. The rotations will get applied in following order:
- around X axis, 2. around Y axis, 3. around Z axis.
Declaration
public Quaternion(float rotationX, float rotationY, float rotationZ)
Parameters
Type | Name | Description |
---|---|---|
float | rotationX | Counterclockwise rotation around X axis in radian. |
float | rotationY | Counterclockwise rotation around Y axis in radian. |
float | rotationZ | Counterclockwise rotation around Z axis in radian. |
Quaternion(float, float, float, float)
Initializes a new instance of the Quaternion struct.
Declaration
public Quaternion(float x, float y, float z, float w)
Parameters
Type | Name | Description |
---|---|---|
float | x | The x component. |
float | y | The y component. |
float | z | The z component. |
float | w | The w component. |
Fields
Identity
Defines the identity quaternion.
Declaration
public static readonly Quaternion Identity
Field Value
Type | Description |
---|---|
Quaternion |
W
The W component of this instance.
Declaration
public float W
Field Value
Type | Description |
---|---|
float |
Xyz
The X, Y and Z components of this instance.
Declaration
public Vector3 Xyz
Field Value
Type | Description |
---|---|
Vector3 |
Properties
Length
Gets the length (magnitude) of the quaternion.
Declaration
public readonly float Length { get; }
Property Value
Type | Description |
---|---|
float |
See Also
LengthSquared
Gets the square of the quaternion length (magnitude).
Declaration
public readonly float LengthSquared { get; }
Property Value
Type | Description |
---|---|
float |
X
Gets or sets the X component of this instance.
Declaration
public float X { readonly get; set; }
Property Value
Type | Description |
---|---|
float |
Y
Gets or sets the Y component of this instance.
Declaration
public float Y { readonly get; set; }
Property Value
Type | Description |
---|---|
float |
Z
Gets or sets the Z component of this instance.
Declaration
public float Z { readonly get; set; }
Property Value
Type | Description |
---|---|
float |
Methods
Add(Quaternion, Quaternion)
Add two quaternions.
Declaration
[Pure]
public static Quaternion Add(Quaternion left, Quaternion right)
Parameters
Type | Name | Description |
---|---|---|
Quaternion | left | The first operand. |
Quaternion | right | The second operand. |
Returns
Type | Description |
---|---|
Quaternion | The result of the addition. |
Add(in Quaternion, in Quaternion, out Quaternion)
Add two quaternions.
Declaration
public static void Add(in Quaternion left, in Quaternion right, out Quaternion result)
Parameters
Type | Name | Description |
---|---|---|
Quaternion | left | The first operand. |
Quaternion | right | The second operand. |
Quaternion | result | The result of the addition. |
Conjugate()
Inverts the Vector3 component of this Quaternion.
Declaration
public void Conjugate()
Conjugate(Quaternion)
Get the conjugate of the given quaternion.
Declaration
[Pure]
public static Quaternion Conjugate(Quaternion q)
Parameters
Type | Name | Description |
---|---|---|
Quaternion | q | The quaternion. |
Returns
Type | Description |
---|---|
Quaternion | The conjugate of the given quaternion. |
Conjugate(in Quaternion, out Quaternion)
Get the conjugate of the given quaternion.
Declaration
public static void Conjugate(in Quaternion q, out Quaternion result)
Parameters
Type | Name | Description |
---|---|---|
Quaternion | q | The quaternion. |
Quaternion | result | The conjugate of the given quaternion. |
Equals(Quaternion)
Indicates whether the current object is equal to another object of the same type.
Declaration
public readonly bool Equals(Quaternion other)
Parameters
Type | Name | Description |
---|---|---|
Quaternion | other | An object to compare with this object. |
Returns
Type | Description |
---|---|
bool | true if the current object is equal to the |
Equals(object)
Indicates whether this instance and a specified object are equal.
Declaration
public override readonly bool Equals(object obj)
Parameters
Type | Name | Description |
---|---|---|
object | obj | The object to compare with the current instance. |
Returns
Type | Description |
---|---|
bool | true if |
Overrides
FromAxisAngle(Vector3, float)
Build a quaternion from the given axis and angle in radians.
Declaration
[Pure]
public static Quaternion FromAxisAngle(Vector3 axis, float angle)
Parameters
Type | Name | Description |
---|---|---|
Vector3 | axis | The axis to rotate about. |
float | angle | The rotation angle in radians. |
Returns
Type | Description |
---|---|
Quaternion | The equivalent quaternion. |
FromEulerAngles(Vector3)
Builds a Quaternion from the given euler angles in radians. The rotations will get applied in following order:
- X axis, 2. Y axis, 3. Z axis.
Declaration
[Pure]
public static Quaternion FromEulerAngles(Vector3 eulerAngles)
Parameters
Type | Name | Description |
---|---|---|
Vector3 | eulerAngles | The counterclockwise euler angles as a vector. |
Returns
Type | Description |
---|---|
Quaternion | The equivalent Quaternion. |
FromEulerAngles(in Vector3, out Quaternion)
Builds a Quaternion from the given euler angles in radians. The rotations will get applied in following order:
- Around X, 2. Around Y, 3. Around Z.
Declaration
public static void FromEulerAngles(in Vector3 eulerAngles, out Quaternion result)
Parameters
Type | Name | Description |
---|---|---|
Vector3 | eulerAngles | The counterclockwise euler angles a vector. |
Quaternion | result | The equivalent Quaternion. |
FromEulerAngles(float, float, float)
Builds a Quaternion from the given euler angles in radians The rotations will get applied in following order:
- pitch (X axis), 2. yaw (Y axis), 3. roll (Z axis).
Declaration
[Pure]
public static Quaternion FromEulerAngles(float pitch, float yaw, float roll)
Parameters
Type | Name | Description |
---|---|---|
float | pitch | The pitch (attitude), counterclockwise rotation around X axis. |
float | yaw | The yaw (heading), counterclockwise rotation around Y axis. |
float | roll | The roll (bank), counterclockwise rotation around Z axis. |
Returns
Type | Description |
---|---|
Quaternion | The quaternion. |
FromMatrix(Matrix3)
Builds a quaternion from the given rotation matrix.
Declaration
[Pure]
public static Quaternion FromMatrix(Matrix3 matrix)
Parameters
Type | Name | Description |
---|---|---|
Matrix3 | matrix | A rotation matrix. |
Returns
Type | Description |
---|---|
Quaternion | The equivalent quaternion. |
FromMatrix(in Matrix3, out Quaternion)
Builds a quaternion from the given rotation matrix.
Declaration
public static void FromMatrix(in Matrix3 matrix, out Quaternion result)
Parameters
Type | Name | Description |
---|---|---|
Matrix3 | matrix | A rotation matrix. |
Quaternion | result | The equivalent quaternion. |
GetHashCode()
Returns the hash code for this instance.
Declaration
public override readonly int GetHashCode()
Returns
Type | Description |
---|---|
int | A 32-bit signed integer that is the hash code for this instance. |
Overrides
Invert()
Inverts this Quaternion.
Declaration
public void Invert()
Invert(Quaternion)
Get the inverse of the given quaternion.
Declaration
[Pure]
public static Quaternion Invert(Quaternion q)
Parameters
Type | Name | Description |
---|---|---|
Quaternion | q | The quaternion to invert. |
Returns
Type | Description |
---|---|
Quaternion | The inverse of the given quaternion. |
Invert(in Quaternion, out Quaternion)
Get the inverse of the given quaternion.
Declaration
public static void Invert(in Quaternion q, out Quaternion result)
Parameters
Type | Name | Description |
---|---|---|
Quaternion | q | The quaternion to invert. |
Quaternion | result | The inverse of the given quaternion. |
Inverted()
Returns the inverse of this Quaternion.
Declaration
public readonly Quaternion Inverted()
Returns
Type | Description |
---|---|
Quaternion | The inverted copy. |
Multiply(Quaternion, Quaternion)
Multiplies two instances.
Declaration
[Pure]
public static Quaternion Multiply(Quaternion left, Quaternion right)
Parameters
Type | Name | Description |
---|---|---|
Quaternion | left | The first instance. |
Quaternion | right | The second instance. |
Returns
Type | Description |
---|---|
Quaternion | A new instance containing the result of the calculation. |
Multiply(Quaternion, float)
Multiplies an instance by a scalar.
Declaration
[Pure]
public static Quaternion Multiply(Quaternion quaternion, float scale)
Parameters
Type | Name | Description |
---|---|---|
Quaternion | quaternion | The instance. |
float | scale | The scalar. |
Returns
Type | Description |
---|---|
Quaternion | A new instance containing the result of the calculation. |
Multiply(in Quaternion, in Quaternion, out Quaternion)
Multiplies two instances.
Declaration
public static void Multiply(in Quaternion left, in Quaternion right, out Quaternion result)
Parameters
Type | Name | Description |
---|---|---|
Quaternion | left | The first instance. |
Quaternion | right | The second instance. |
Quaternion | result | A new instance containing the result of the calculation. |
Multiply(in Quaternion, float, out Quaternion)
Multiplies an instance by a scalar.
Declaration
public static void Multiply(in Quaternion quaternion, float scale, out Quaternion result)
Parameters
Type | Name | Description |
---|---|---|
Quaternion | quaternion | The instance. |
float | scale | The scalar. |
Quaternion | result | A new instance containing the result of the calculation. |
Normalize()
Scales the Quaternion to unit length.
Declaration
public void Normalize()
Normalize(Quaternion)
Scale the given quaternion to unit length.
Declaration
[Pure]
public static Quaternion Normalize(Quaternion q)
Parameters
Type | Name | Description |
---|---|---|
Quaternion | q | The quaternion to normalize. |
Returns
Type | Description |
---|---|
Quaternion | The normalized copy. |
Normalize(in Quaternion, out Quaternion)
Scale the given quaternion to unit length.
Declaration
public static void Normalize(in Quaternion q, out Quaternion result)
Parameters
Type | Name | Description |
---|---|---|
Quaternion | q | The quaternion to normalize. |
Quaternion | result | The normalized quaternion. |
Normalized()
Returns a copy of the Quaternion scaled to unit length.
Declaration
public readonly Quaternion Normalized()
Returns
Type | Description |
---|---|
Quaternion | The normalized copy. |
Slerp(Quaternion, Quaternion, float)
Do Spherical linear interpolation between two quaternions.
Declaration
[Pure]
public static Quaternion Slerp(Quaternion q1, Quaternion q2, float blend)
Parameters
Type | Name | Description |
---|---|---|
Quaternion | q1 | The first quaternion. |
Quaternion | q2 | The second quaternion. |
float | blend | The blend factor. |
Returns
Type | Description |
---|---|
Quaternion | A smooth blend between the given quaternions. |
Sub(Quaternion, Quaternion)
Subtracts two instances.
Declaration
[Pure]
public static Quaternion Sub(Quaternion left, Quaternion right)
Parameters
Type | Name | Description |
---|---|---|
Quaternion | left | The left instance. |
Quaternion | right | The right instance. |
Returns
Type | Description |
---|---|
Quaternion | The result of the operation. |
Sub(in Quaternion, in Quaternion, out Quaternion)
Subtracts two instances.
Declaration
public static void Sub(in Quaternion left, in Quaternion right, out Quaternion result)
Parameters
Type | Name | Description |
---|---|---|
Quaternion | left | The left instance. |
Quaternion | right | The right instance. |
Quaternion | result | The result of the operation. |
ToAxisAngle()
Convert this instance to an axis-angle representation.
Declaration
public Vector4 ToAxisAngle()
Returns
Type | Description |
---|---|
Vector4 | A Vector4 that is the axis-angle representation of this quaternion. |
ToAxisAngle(out Vector3, out float)
Convert the current quaternion to axis angle representation.
Declaration
public void ToAxisAngle(out Vector3 axis, out float angle)
Parameters
Type | Name | Description |
---|---|---|
Vector3 | axis | The resultant axis. |
float | angle | The resultant angle. |
ToEulerAngles()
Convert this instance to an Euler angle representation.
Declaration
public readonly Vector3 ToEulerAngles()
Returns
Type | Description |
---|---|
Vector3 | The Euler angles in radians. |
ToEulerAngles(in Quaternion, out Vector3)
Converts a quaternion to it's euler angle representation.
Declaration
public static void ToEulerAngles(in Quaternion q, out Vector3 result)
Parameters
Type | Name | Description |
---|---|---|
Quaternion | q | The Quaternion. |
Vector3 | result | The resulting euler angles in radians. |
ToEulerAngles(out Vector3)
Convert the current quaternion to Euler angle representation.
Declaration
public readonly void ToEulerAngles(out Vector3 angles)
Parameters
Type | Name | Description |
---|---|---|
Vector3 | angles | The Euler angles in radians. |
ToString()
Returns a System.String that represents the current Quaternion.
Declaration
public override readonly string ToString()
Returns
Type | Description |
---|---|
string | A human-readable representation of the quaternion. |
Overrides
ToString(IFormatProvider)
Formats the value of the current instance using the specified format.
Declaration
public readonly string ToString(IFormatProvider formatProvider)
Parameters
Type | Name | Description |
---|---|---|
IFormatProvider | formatProvider | The provider to use to format the value. -or- A null reference ( |
Returns
Type | Description |
---|---|
string | The value of the current instance in the specified format. |
ToString(string)
Formats the value of the current instance using the specified format.
Declaration
public readonly string ToString(string format)
Parameters
Type | Name | Description |
---|---|---|
string | format | The format to use. -or- A null reference ( |
Returns
Type | Description |
---|---|
string | The value of the current instance in the specified format. |
ToString(string, IFormatProvider)
Formats the value of the current instance using the specified format.
Declaration
public readonly string ToString(string format, IFormatProvider formatProvider)
Parameters
Type | Name | Description |
---|---|---|
string | format | The format to use. -or- A null reference ( |
IFormatProvider | formatProvider | The provider to use to format the value. -or- A null reference ( |
Returns
Type | Description |
---|---|
string | The value of the current instance in the specified format. |
Operators
operator +(Quaternion, Quaternion)
Adds two instances.
Declaration
[Pure]
public static Quaternion operator +(Quaternion left, Quaternion right)
Parameters
Type | Name | Description |
---|---|---|
Quaternion | left | The first instance. |
Quaternion | right | The second instance. |
Returns
Type | Description |
---|---|
Quaternion | The result of the calculation. |
operator ==(Quaternion, Quaternion)
Compares two instances for equality.
Declaration
public static bool operator ==(Quaternion left, Quaternion right)
Parameters
Type | Name | Description |
---|---|---|
Quaternion | left | The first instance. |
Quaternion | right | The second instance. |
Returns
Type | Description |
---|---|
bool | True, if left equals right; false otherwise. |
operator !=(Quaternion, Quaternion)
Compares two instances for inequality.
Declaration
public static bool operator !=(Quaternion left, Quaternion right)
Parameters
Type | Name | Description |
---|---|---|
Quaternion | left | The first instance. |
Quaternion | right | The second instance. |
Returns
Type | Description |
---|---|
bool | True, if left does not equal right; false otherwise. |
operator *(Quaternion, Quaternion)
Multiplies two instances.
Declaration
[Pure]
public static Quaternion operator *(Quaternion left, Quaternion right)
Parameters
Type | Name | Description |
---|---|---|
Quaternion | left | The first instance. |
Quaternion | right | The second instance. |
Returns
Type | Description |
---|---|
Quaternion | The result of the calculation. |
operator *(Quaternion, float)
Multiplies an instance by a scalar.
Declaration
[Pure]
public static Quaternion operator *(Quaternion quaternion, float scale)
Parameters
Type | Name | Description |
---|---|---|
Quaternion | quaternion | The instance. |
float | scale | The scalar. |
Returns
Type | Description |
---|---|
Quaternion | A new instance containing the result of the calculation. |
operator *(float, Quaternion)
Multiplies an instance by a scalar.
Declaration
[Pure]
public static Quaternion operator *(float scale, Quaternion quaternion)
Parameters
Type | Name | Description |
---|---|---|
float | scale | The scalar. |
Quaternion | quaternion | The instance. |
Returns
Type | Description |
---|---|
Quaternion | A new instance containing the result of the calculation. |
operator -(Quaternion, Quaternion)
Subtracts two instances.
Declaration
[Pure]
public static Quaternion operator -(Quaternion left, Quaternion right)
Parameters
Type | Name | Description |
---|---|---|
Quaternion | left | The first instance. |
Quaternion | right | The second instance. |
Returns
Type | Description |
---|---|
Quaternion | The result of the calculation. |