Skip to content

Get Sansar

Script API

Quaternion

Where: Client
More page actions

For the complete documentation index, see llms.txt. This page is also available as Markdown.

Struct Namespace Sansar

Represents a quaternion orientation.

public struct Quaternion

Members at a glance

KindMembers
ConstructorsQuaternion
FieldsIdentity
PropertiesItem · W · X · Y · Z
MethodsDot · FromAngleAxis · FromEulerAngles · FromLook · FromLookOrthoNormal · GetEulerAngles · Inverse · Length · LengthSquared · Normalized · Parse · ShortestRotation · ToAngleAxis · ToString · TryParse
Operatorsop_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.

ParameterTypeDescription
vMono.Simd.Vector4fInitializes the quaternion from a Vector4f.
public Quaternion(float x, float y, float z, float w)

Creates a new quaternion with the supplied values.

ParameterTypeDescription
xfloatThe x coordinate.
yfloatThe y coordinate.
zfloatThe z coordinate.
wfloatThe w coordinate.

Fields

Identity

public static readonly Quaternion Identity

The identity quaternion.

Properties

Item

public float this [int index] { get; set; }

Allows getting coordinates by index.

ParameterTypeDescription
indexint0=>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.

ParameterTypeDescription
bQuaternionThe 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.

ParameterTypeDescription
anglefloatAngle of rotation in radians
axisVectorA 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.

ParameterTypeDescription
aVectorA 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.

ParameterTypeDescription
facingVectorThe direction to look.
upVectorThe up direction.

Returns: Quaternion — A new quaternion representing this rotation.

FromLookOrthoNormal

Added in 35.0.0
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.

ParameterTypeDescription
facingVectorThe direction to look.
upVectorThe up direction.
leftVectorThe 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.

ParameterTypeDescription
quaternionStringstringA 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.

ParameterTypeDescription
fromVectorVector rotating from.
toVectorVector 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

ParameterTypeDescription
anglefloatAngle of rotation in radians
axisVectorA 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.

ParameterTypeDescription
formatstringFormat 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.

ParameterTypeDescription
quaternionStringstringA string of the format “[X,Y,Z,W]”
quaternionQuaternionThe 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.

ParameterTypeDescription
vMono.Simd.Vector4fThe vector to convert
public static implicit operator Mono.Simd.Vector4f(Quaternion v)

Converts a quaternion to a Mono.Simd.Vector4f.

ParameterTypeDescription
vQuaternionThe 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.

ParameterTypeDescription
aQuaternionThe first quaternion
bQuaternionThe second quaternion

Returns: Quaternion — Returns the composed orientation for the two quaternions.

op_UnaryNegation

Deprecated
[System.Obsolete] public static Quaternion operator -(Quaternion q)

Negates a quaternion

ParameterTypeDescription
qQuaternionThe 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

Generated from the Sansar 47.10.0 client ScriptApi snapshot.