# Quaternion

Kind: Struct · Namespace: Sansar · Assembly: Sansar.Script.dll · Script API 47.10.0

<div class="api-meta">
<span class="api-kind api-kind-struct" data-pagefind-filter="kind:Struct">Struct</span>
<span data-pagefind-filter="namespace:Sansar">Namespace <a href="/scripting/api/sansar"><code>Sansar</code></a></span>
</div>

<span id="T:Sansar.Quaternion"></span>

Represents a quaternion orientation.

```csharp
public struct Quaternion
```

## Members at a glance

| Kind | Members |
|---|---|
| **Constructors** | [Quaternion](#quaternion) |
| **Fields** | [Identity](#identity) |
| **Properties** | [Item](#item) · [W](#w) · [X](#x) · [Y](#y) · [Z](#z) |
| **Methods** | [Dot](#dot) · [FromAngleAxis](#fromangleaxis) · [FromEulerAngles](#fromeulerangles) · [FromLook](#fromlook) · [FromLookOrthoNormal](#fromlookorthonormal) · [GetEulerAngles](#geteulerangles) · [Inverse](#inverse) · [Length](#length) · [LengthSquared](#lengthsquared) · [Normalized](#normalized) · [Parse](#parse) · [ShortestRotation](#shortestrotation) · [ToAngleAxis](#toangleaxis) · [ToString](#tostring) · [TryParse](#tryparse) |
| **Operators** | [op\_Implicit](#op_implicit) · [op\_Multiply](#op_multiply) · [op\_UnaryNegation](#op_unarynegation) |

## Constructors

### Quaternion

<div class="api-card" id="ctor-vector4f">
<span id="M:Sansar.Quaternion.#ctor(Mono.Simd.Vector4f@)"></span>
<span id="C:Sansar.Quaternion(Mono.Simd.Vector4f@)"></span>

```csharp signature
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`. |

</div>

<div class="api-card" id="ctor-single-single-single-single">
<span id="M:Sansar.Quaternion.#ctor(System.Single,System.Single,System.Single,System.Single)"></span>
<span id="C:Sansar.Quaternion(System.Single,System.Single,System.Single,System.Single)"></span>

```csharp signature
public Quaternion(float x, float y, float z, float w)
```

Creates a new quaternion with the supplied values.

| Parameter | Type | Description |
|---|---|---|
| `x` | [float](https://learn.microsoft.com/dotnet/api/system.single) | The x coordinate. |
| `y` | [float](https://learn.microsoft.com/dotnet/api/system.single) | The y coordinate. |
| `z` | [float](https://learn.microsoft.com/dotnet/api/system.single) | The z coordinate. |
| `w` | [float](https://learn.microsoft.com/dotnet/api/system.single) | The w coordinate. |

</div>


## Fields

### Identity

<div class="api-card">
<span id="F:Sansar.Quaternion.Identity"></span>

```csharp signature
public static readonly Quaternion Identity
```

The identity quaternion.

</div>


## Properties

### Item

<div class="api-card">
<span id="P:Sansar.Quaternion.Item(System.Int32)"></span>

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

Allows getting coordinates by index.

| Parameter | Type | Description |
|---|---|---|
| `index` | [int](https://learn.microsoft.com/dotnet/api/system.int32) | 0=&gt;X, 1=&gt;Y, 2=&gt;Z, 3=&gt;W |

</div>

### W

<div class="api-card">
<span id="P:Sansar.Quaternion.W"></span>

```csharp signature
public float W { get; set; }
```

The W coordinate of the quaternion.

</div>

### X

<div class="api-card">
<span id="P:Sansar.Quaternion.X"></span>

```csharp signature
public float X { get; set; }
```

The X coordinate of the quaternion.

</div>

### Y

<div class="api-card">
<span id="P:Sansar.Quaternion.Y"></span>

```csharp signature
public float Y { get; set; }
```

The Y coordinate of the quaternion.

</div>

### Z

<div class="api-card">
<span id="P:Sansar.Quaternion.Z"></span>

```csharp signature
public float Z { get; set; }
```

The Z coordinate of the quaternion.

</div>


## Methods

### Dot

<div class="api-card">
<span id="M:Sansar.Quaternion.Dot(Sansar.Quaternion@)"></span>

```csharp signature
public float Dot(
    [System.Runtime.CompilerServices.IsReadOnly] ref Quaternion b
)
```

Performs a scalar or dot product.

| Parameter | Type | Description |
|---|---|---|
| `b` | [Quaternion](/scripting/api/sansar/quaternion) | The second quaternion. |

**Returns:** [float](https://learn.microsoft.com/dotnet/api/system.single) — Returns X\*b.X+Y\*b.Y+Z\*b.Z+W\*b.W

</div>

### FromAngleAxis

<div class="api-card">
<span id="M:Sansar.Quaternion.FromAngleAxis(System.Single,Sansar.Vector@)"></span>

```csharp signature
public static Quaternion FromAngleAxis(
    float angle,
    [System.Runtime.CompilerServices.IsReadOnly] ref Vector axis
)
```

Generates the corresponding quaternion.

| Parameter | Type | Description |
|---|---|---|
| `angle` | [float](https://learn.microsoft.com/dotnet/api/system.single) | Angle of rotation in radians |
| `axis` | [Vector](/scripting/api/sansar/vector) | A normalized axis of rotation |

**Returns:** [Quaternion](/scripting/api/sansar/quaternion) — The corresponding quaternion.

</div>

### FromEulerAngles

<div class="api-card">
<span id="M:Sansar.Quaternion.FromEulerAngles(Sansar.Vector@)"></span>

```csharp signature
public static Quaternion FromEulerAngles(
    [System.Runtime.CompilerServices.IsReadOnly] ref Vector a
)
```

Generates the corresponding quaternion.

| Parameter | Type | Description |
|---|---|---|
| `a` | [Vector](/scripting/api/sansar/vector) | A vector of angles in radians. |

**Returns:** [Quaternion](/scripting/api/sansar/quaternion) — The corresponding quaternion.

<details class="api-remarks"><summary>Remarks</summary>

Rotates first around X (local/global coincide), then around new local Y, and finally around new local Z.

</details>

</div>

### FromLook

<div class="api-card">
<span id="M:Sansar.Quaternion.FromLook(Sansar.Vector,Sansar.Vector)"></span>

```csharp signature
public static Quaternion FromLook(Vector facing, Vector up)
```

Creates a new rotation with the specified direction and up vectors.

| Parameter | Type | Description |
|---|---|---|
| `facing` | [Vector](/scripting/api/sansar/vector) | The direction to look. |
| `up` | [Vector](/scripting/api/sansar/vector) | The up direction. |

**Returns:** [Quaternion](/scripting/api/sansar/quaternion) — A new quaternion representing this rotation.

</div>

### FromLookOrthoNormal

<div class="api-card">
<span id="M:Sansar.Quaternion.FromLookOrthoNormal(Sansar.Vector@,Sansar.Vector@,Sansar.Vector@)"></span>
<div class="api-badges"><span class="api-badge api-since">Added in 35.0.0</span></div>

```csharp signature
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](/scripting/api/sansar/vector) | The direction to look. |
| `up` | [Vector](/scripting/api/sansar/vector) | The up direction. |
| `left` | [Vector](/scripting/api/sansar/vector) | The left direction. |

**Returns:** [Quaternion](/scripting/api/sansar/quaternion) — A new quaternion representing this rotation.

<details class="api-remarks"><summary>Remarks</summary>

The 3 given vectors must be orthonormal. See [`Vector.Orthonormalize`](/scripting/api/sansar/vector#orthonormalize)

</details>

</div>

### GetEulerAngles

<div class="api-card">
<span id="M:Sansar.Quaternion.GetEulerAngles"></span>
<span id="M:Sansar.Quaternion.GetEulerAngles()"></span>

```csharp signature
public Vector GetEulerAngles()
```

Converts this rotation to a vector of euler angles

**Returns:** [Vector](/scripting/api/sansar/vector) — A vector of angles in radians corresponding to this quaternion.

<details class="api-remarks"><summary>Remarks</summary>

Returns angles which can be passed to [`Quaternion.FromEulerAngles`](/scripting/api/sansar/quaternion#fromeulerangles) to create this quaternion.

</details>

</div>

### Inverse

<div class="api-card">
<span id="M:Sansar.Quaternion.Inverse"></span>
<span id="M:Sansar.Quaternion.Inverse()"></span>

```csharp signature
public Quaternion Inverse()
```

Gets the Inverse of the quaternion.

**Returns:** [Quaternion](/scripting/api/sansar/quaternion) — A quaternion inverse, such that q \* q.Inverse() == q.Inverse() \* q == Quaternion.Identity

<details class="api-remarks"><summary>Remarks</summary>

Results are undefined if the length of the quaternion is close to 0.

</details>

</div>

### Length

<div class="api-card">
<span id="M:Sansar.Quaternion.Length"></span>
<span id="M:Sansar.Quaternion.Length()"></span>

```csharp signature
public float Length()
```

Calculates the length of this quaternion.

**Returns:** [float](https://learn.microsoft.com/dotnet/api/system.single) — The length of the quaternion.

</div>

### LengthSquared

<div class="api-card">
<span id="M:Sansar.Quaternion.LengthSquared"></span>
<span id="M:Sansar.Quaternion.LengthSquared()"></span>

```csharp signature
public float LengthSquared()
```

Calculates the length squared of this quaternion.

**Returns:** [float](https://learn.microsoft.com/dotnet/api/system.single) — The magnitude of the vector.

</div>

### Normalized

<div class="api-card">
<span id="M:Sansar.Quaternion.Normalized"></span>
<span id="M:Sansar.Quaternion.Normalized()"></span>

```csharp signature
public Quaternion Normalized()
```

Returns a quaternion with the same orientation and unit length.

**Returns:** [Quaternion](/scripting/api/sansar/quaternion) — A quaternion with the same orientation and unit length.

<details class="api-remarks"><summary>Remarks</summary>

Results are undefined if the length of the quaternion is close to 0.

</details>

</div>

### Parse

<div class="api-card">
<span id="M:Sansar.Quaternion.Parse(System.String)"></span>

```csharp signature
public static Quaternion Parse(string quaternionString)
```

Parse a Quaternion from a string.

| Parameter | Type | Description |
|---|---|---|
| `quaternionString` | [string](https://learn.microsoft.com/dotnet/api/system.string) | A string of the format &quot;\[X,Y,Z,W\]&quot; |

**Returns:** [Quaternion](/scripting/api/sansar/quaternion) — The Quaternion parsed from quaternionString.

**Throws** [`ArgumentException`](https://learn.microsoft.com/dotnet/api/system.argumentexception): If quaternionString is null.

**Throws** [`FormatException`](https://learn.microsoft.com/dotnet/api/system.formatexception): If the string is not a valid quaternion or its components are not valid floats.

<details class="api-example" open><summary>Example</summary>

```csharp
Quaternion myQuat = Quaternion.Parse("[0.0,0.0,0.0,1.0]");
```

</details>

</div>

### ShortestRotation

<div class="api-card">
<span id="M:Sansar.Quaternion.ShortestRotation(Sansar.Vector@,Sansar.Vector@)"></span>

```csharp signature
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](/scripting/api/sansar/vector) | Vector rotating from. |
| `to` | [Vector](/scripting/api/sansar/vector) | Vector rotating to. |

**Returns:** [Quaternion](/scripting/api/sansar/quaternion) — The corresponding quaternion.

<details class="api-remarks"><summary>Remarks</summary>

This function assumes that the passed in vectors are normalized.

</details>

</div>

### ToAngleAxis

<div class="api-card">
<span id="M:Sansar.Quaternion.ToAngleAxis(System.Single@,Sansar.Vector@)"></span>

```csharp signature
public void ToAngleAxis(out float angle, out Vector axis)
```

Gets the angle-axis representation

| Parameter | Type | Description |
|---|---|---|
| `angle` | [float](https://learn.microsoft.com/dotnet/api/system.single) | Angle of rotation in radians |
| `axis` | [Vector](/scripting/api/sansar/vector) | A normalized axis of rotation |

<details class="api-remarks"><summary>Remarks</summary>

For the Quaternion Identity (no rotation) the axis is undefined and will be given as (0,0,1)

</details>

</div>

### ToString

<div class="api-card" id="tostring-noargs">
<span id="M:Sansar.Quaternion.ToString"></span>
<span id="M:Sansar.Quaternion.ToString()"></span>

```csharp signature
public override string ToString()
```

Generates a string representation of the quaternion.

**Returns:** [string](https://learn.microsoft.com/dotnet/api/system.string) — The quaternion as a string.

</div>

<div class="api-card" id="tostring-string">
<span id="M:Sansar.Quaternion.ToString(System.String)"></span>

```csharp signature
public string ToString(string format)
```

Generates a string representation of the quaternion.

| Parameter | Type | Description |
|---|---|---|
| `format` | [string](https://learn.microsoft.com/dotnet/api/system.string) | Format to use for each of the coordinates. |

**Returns:** [string](https://learn.microsoft.com/dotnet/api/system.string) — The quaternion as a string.

</div>

### TryParse

<div class="api-card">
<span id="M:Sansar.Quaternion.TryParse(System.String,Sansar.Quaternion@)"></span>

```csharp signature
public static bool TryParse(
    string quaternionString,
    out Quaternion quaternion
)
```

Try to parse a Quaternion from a string.

| Parameter | Type | Description |
|---|---|---|
| `quaternionString` | [string](https://learn.microsoft.com/dotnet/api/system.string) | A string of the format &quot;\[X,Y,Z,W\]&quot; |
| `quaternion` | [Quaternion](/scripting/api/sansar/quaternion) | The quaternion that will be set if quaternionString represents a valid quaternion. |

**Returns:** [bool](https://learn.microsoft.com/dotnet/api/system.boolean) — True if successfully parsed a quaternion, false if not.

<details class="api-example" open><summary>Example</summary>

```csharp
Quaternion myQuat;
            if (Quaternion.TryParse("[0.0,0.0,0.0,1.0]", out myQuat)
            {
                // myQuat is set.
            }
```

</details>

</div>


## Operators

### op_Implicit

<div class="api-card" id="opimplicit-quaternion">
<span id="M:Sansar.Quaternion.op_Implicit(Mono.Simd.Vector4f)~Sansar.Quaternion"></span>

```csharp signature
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 |

</div>

<div class="api-card" id="opimplicit-vector4f">
<span id="M:Sansar.Quaternion.op_Implicit(Sansar.Quaternion)~Mono.Simd.Vector4f"></span>

```csharp signature
public static implicit operator Mono.Simd.Vector4f(Quaternion v)
```

Converts a quaternion to a Mono.Simd.Vector4f.

| Parameter | Type | Description |
|---|---|---|
| `v` | [Quaternion](/scripting/api/sansar/quaternion) | The quaternion to convert |

</div>

### op_Multiply

<div class="api-card">
<span id="M:Sansar.Quaternion.op_Multiply(Sansar.Quaternion@,Sansar.Quaternion@)"></span>

```csharp signature
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](/scripting/api/sansar/quaternion) | The first quaternion |
| `b` | [Quaternion](/scripting/api/sansar/quaternion) | The second quaternion |

**Returns:** [Quaternion](/scripting/api/sansar/quaternion) — Returns the composed orientation for the two quaternions.

</div>

### op_UnaryNegation

<div class="api-card">
<span id="M:Sansar.Quaternion.op_UnaryNegation(Sansar.Quaternion)"></span>
<div class="api-badges"><span class="api-badge api-deprecated">Deprecated</span></div>

```csharp signature
[System.Obsolete]
public static Quaternion operator -(Quaternion q)
```

Negates a quaternion

| Parameter | Type | Description |
|---|---|---|
| `q` | [Quaternion](/scripting/api/sansar/quaternion) | The quaternion to negate |

<details class="api-remarks"><summary>Remarks</summary>

Note this is not a quaternion inverse, it is a component-wise multiplication by -1. For a quaternion inverse use [`Quaternion.Inverse`](/scripting/api/sansar/quaternion#inverse)

</details>

</div>


## Used in examples

- [MoverExample2.cs](/scripting/examples/mover-examples)
- [MoverExample3.cs](/scripting/examples/mover-examples)
- [BanHammerExample.cs](https://github.com/Wookey-Technologies/sansar-script-starter/blob/66af8b516b701088d043e4615acfcf5779356604/examples/official/BanHammerExample.cs)
- [CastRayExample.cs](https://github.com/Wookey-Technologies/sansar-script-starter/blob/66af8b516b701088d043e4615acfcf5779356604/examples/official/CastRayExample.cs)
- `MoverExample4.cs`
- [ScriptLibrary/Scene/Dispenser.cs](https://github.com/Wookey-Technologies/sansar-script-starter/blob/66af8b516b701088d043e4615acfcf5779356604/examples/official/ScriptLibrary/Scene/Dispenser.cs)
- [ScriptLibrary/Scene/HintText.cs](https://github.com/Wookey-Technologies/sansar-script-starter/blob/66af8b516b701088d043e4615acfcf5779356604/examples/official/ScriptLibrary/Scene/HintText.cs)
- [ScriptLibrary/Scene/KeyframedMotion.cs](https://github.com/Wookey-Technologies/sansar-script-starter/blob/66af8b516b701088d043e4615acfcf5779356604/examples/official/ScriptLibrary/Scene/KeyframedMotion.cs)
- [ScriptLibrary/Scene/Mover.cs](https://github.com/Wookey-Technologies/sansar-script-starter/blob/66af8b516b701088d043e4615acfcf5779356604/examples/official/ScriptLibrary/Scene/Mover.cs)

<p class="api-generated" data-pagefind-ignore>Generated from the Sansar 47.10.0 client ScriptApi snapshot.</p>