Represents a quaternion orientation.
public struct QuaternionMembers at a glance
| Kind | Members |
|---|---|
| Constructors | Quaternion |
| Fields | Identity |
| Properties | Item · W · X · Y · Z |
| Methods | Dot · FromAngleAxis · FromEulerAngles · FromLook · FromLookOrthoNormal · GetEulerAngles · Inverse · Length · LengthSquared · Normalized · Parse · ShortestRotation · ToAngleAxis · ToString · TryParse |
| Operators | op_Implicit · op_Multiply · op_UnaryNegation |
Constructors
Quaternion
public Quaternion(
[System.Runtime.CompilerServices.IsReadOnly] ref Mono.Simd.Vector4f v
)Creates a new quaternion with the supplied values.
| Parameter | Type | Description |
|---|---|---|
v | Mono.Simd.Vector4f | Initializes the quaternion from a Vector4f. |
public Quaternion(float x, float y, float z, float w)Creates a new quaternion with the supplied values.
| Parameter | Type | Description |
|---|---|---|
x | float | The x coordinate. |
y | float | The y coordinate. |
z | float | The z coordinate. |
w | float | The w coordinate. |
Fields
Identity
public static readonly Quaternion IdentityThe identity quaternion.
Properties
Item
public float this [int index] { get; set; }Allows getting coordinates by index.
| Parameter | Type | Description |
|---|---|---|
index | int | 0=>X, 1=>Y, 2=>Z, 3=>W |
W
public float W { get; set; }The W coordinate of the quaternion.
X
public float X { get; set; }The X coordinate of the quaternion.
Y
public float Y { get; set; }The Y coordinate of the quaternion.
Z
public float Z { get; set; }The Z coordinate of the quaternion.
Methods
Dot
public float Dot(
[System.Runtime.CompilerServices.IsReadOnly] ref Quaternion b
)Performs a scalar or dot product.
| Parameter | Type | Description |
|---|---|---|
b | Quaternion | The second quaternion. |
Returns: float — Returns X*b.X+Y*b.Y+Z*b.Z+W*b.W
FromAngleAxis
public static Quaternion FromAngleAxis(
float angle,
[System.Runtime.CompilerServices.IsReadOnly] ref Vector axis
)Generates the corresponding quaternion.
| Parameter | Type | Description |
|---|---|---|
angle | float | Angle of rotation in radians |
axis | Vector | A normalized axis of rotation |
Returns: Quaternion — The corresponding quaternion.
FromEulerAngles
public static Quaternion FromEulerAngles(
[System.Runtime.CompilerServices.IsReadOnly] ref Vector a
)Generates the corresponding quaternion.
| Parameter | Type | Description |
|---|---|---|
a | Vector | A vector of angles in radians. |
Returns: Quaternion — The corresponding quaternion.
Remarks
Rotates first around X (local/global coincide), then around new local Y, and finally around new local Z.
FromLook
public static Quaternion FromLook(Vector facing, Vector up)Creates a new rotation with the specified direction and up vectors.
| Parameter | Type | Description |
|---|---|---|
facing | Vector | The direction to look. |
up | Vector | The up direction. |
Returns: Quaternion — A new quaternion representing this rotation.
FromLookOrthoNormal
public static Quaternion FromLookOrthoNormal(
[System.Runtime.CompilerServices.IsReadOnly] ref Vector facing,
[System.Runtime.CompilerServices.IsReadOnly] ref Vector up,
[System.Runtime.CompilerServices.IsReadOnly] ref Vector left
)Creates a new rotation with the specified direction and up vectors.
| Parameter | Type | Description |
|---|---|---|
facing | Vector | The direction to look. |
up | Vector | The up direction. |
left | Vector | The left direction. |
Returns: Quaternion — A new quaternion representing this rotation.
Remarks
The 3 given vectors must be orthonormal. See Vector.Orthonormalize
GetEulerAngles
public Vector GetEulerAngles()Converts this rotation to a vector of euler angles
Returns: Vector — A vector of angles in radians corresponding to this quaternion.
Remarks
Returns angles which can be passed to Quaternion.FromEulerAngles to create this quaternion.
Inverse
public Quaternion Inverse()Gets the Inverse of the quaternion.
Returns: Quaternion — A quaternion inverse, such that q * q.Inverse() == q.Inverse() * q == Quaternion.Identity
Remarks
Results are undefined if the length of the quaternion is close to 0.
Length
public float Length()Calculates the length of this quaternion.
Returns: float — The length of the quaternion.
LengthSquared
public float LengthSquared()Calculates the length squared of this quaternion.
Returns: float — The magnitude of the vector.
Normalized
public Quaternion Normalized()Returns a quaternion with the same orientation and unit length.
Returns: Quaternion — A quaternion with the same orientation and unit length.
Remarks
Results are undefined if the length of the quaternion is close to 0.
Parse
public static Quaternion Parse(string quaternionString)Parse a Quaternion from a string.
| Parameter | Type | Description |
|---|---|---|
quaternionString | string | A string of the format “[X,Y,Z,W]” |
Returns: Quaternion — The Quaternion parsed from quaternionString.
Throws ArgumentException: If quaternionString is null.
Throws FormatException: If the string is not a valid quaternion or its components are not valid floats.
Example
Quaternion myQuat = Quaternion.Parse("[0.0,0.0,0.0,1.0]");ShortestRotation
public static Quaternion ShortestRotation(
[System.Runtime.CompilerServices.IsReadOnly] ref Vector from,
[System.Runtime.CompilerServices.IsReadOnly] ref Vector to
)Generates the shortest rotation quaternion to rotate from one vector to another.
| Parameter | Type | Description |
|---|---|---|
from | Vector | Vector rotating from. |
to | Vector | Vector rotating to. |
Returns: Quaternion — The corresponding quaternion.
Remarks
This function assumes that the passed in vectors are normalized.
ToAngleAxis
public void ToAngleAxis(out float angle, out Vector axis)Gets the angle-axis representation
| Parameter | Type | Description |
|---|---|---|
angle | float | Angle of rotation in radians |
axis | Vector | A normalized axis of rotation |
Remarks
For the Quaternion Identity (no rotation) the axis is undefined and will be given as (0,0,1)
ToString
public override string ToString()Generates a string representation of the quaternion.
Returns: string — The quaternion as a string.
public string ToString(string format)Generates a string representation of the quaternion.
| Parameter | Type | Description |
|---|---|---|
format | string | Format to use for each of the coordinates. |
Returns: string — The quaternion as a string.
TryParse
public static bool TryParse(
string quaternionString,
out Quaternion quaternion
)Try to parse a Quaternion from a string.
| Parameter | Type | Description |
|---|---|---|
quaternionString | string | A string of the format “[X,Y,Z,W]” |
quaternion | Quaternion | The quaternion that will be set if quaternionString represents a valid quaternion. |
Returns: bool — True if successfully parsed a quaternion, false if not.
Example
Quaternion myQuat; if (Quaternion.TryParse("[0.0,0.0,0.0,1.0]", out myQuat) { // myQuat is set. }Operators
op_Implicit
public static implicit operator Quaternion(Mono.Simd.Vector4f v)Converts a Mono.Simd.Vector4f to a quaternion.
| Parameter | Type | Description |
|---|---|---|
v | Mono.Simd.Vector4f | The vector to convert |
public static implicit operator Mono.Simd.Vector4f(Quaternion v)Converts a quaternion to a Mono.Simd.Vector4f.
| Parameter | Type | Description |
|---|---|---|
v | Quaternion | The quaternion to convert |
op_Multiply
public static Quaternion operator *(
[System.Runtime.CompilerServices.IsReadOnly] ref Quaternion a,
[System.Runtime.CompilerServices.IsReadOnly] ref Quaternion b
)Multiplies two quaternions.
| Parameter | Type | Description |
|---|---|---|
a | Quaternion | The first quaternion |
b | Quaternion | The second quaternion |
Returns: Quaternion — Returns the composed orientation for the two quaternions.
op_UnaryNegation
[System.Obsolete]
public static Quaternion operator -(Quaternion q)Negates a quaternion
| Parameter | Type | Description |
|---|---|---|
q | Quaternion | The quaternion to negate |
Remarks
Note this is not a quaternion inverse, it is a component-wise multiplication by -1. For a quaternion inverse use Quaternion.Inverse
Used in examples
- MoverExample2.cs
- MoverExample3.cs
- BanHammerExample.cs
- CastRayExample.cs
MoverExample4.cs- ScriptLibrary/Scene/Dispenser.cs
- ScriptLibrary/Scene/HintText.cs
- ScriptLibrary/Scene/KeyframedMotion.cs
- ScriptLibrary/Scene/Mover.cs
Generated from the Sansar 47.10.0 client ScriptApi snapshot.