# SQuaternion

## Index

### Functions Index

| Function                                                                                                                                                                                                                             |
| ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ |
| string [**ToString** ](#tostring)()                                                                                                                                                                                                  |
| float [**Angle** ](#angle)(SQuaternion b)                                                                                                                                                                                            |
| SQuaternion [**Lerp** ](#lerp)(SQuaternion b, float t)                                                                                                                                                                               |
| SQuaternion [**Slerp** ](#slerp)(SQuaternion b, float t)                                                                                                                                                                             |
| SQuaternion [**RotateTowards** ](#rotatetowards)(SQuaternion b, float delta)                                                                                                                                                         |
| float [**Dot** ](#dot)(SQuaternion b)                                                                                                                                                                                                |
| bool [**Equals** ](#equals)(SQuaternion other)                                                                                                                                                                                       |
|                                                                                                                                                                                                                                      |
| **Static Functions**                                                                                                                                                                                                                 |
| static SQuaternion [**New** ](#new)(float x, float y, float z, float w)                                                                                                                                                              |
| static SQuaternion [**Euler** ](#euler)(float x, float y, float z)                                                                                                                                                                   |
| static SQuaternion [**AngleAxis** ](#angleaxis)(SVector axis, float angle)                                                                                                                                                           |
| <p>static SQuaternion <a href="#lookrotation"><strong>LookRotation</strong> </a>(SVector forward)<br>static SQuaternion <a href="#lookrotation"><strong>LookRotation</strong> </a>(SVector forward, SVector up)</p>                  |
| <p>static SVector <a href="#operator"><strong>operator</strong></a> <em>(SQuaternion a, SVector b)</em><br><em>static SQuaternion</em> <a href="#operator"><em><strong>operator</strong></em></a> (SQuaternion a, SQuaternion b)</p> |
| static SQuaternion **FromToRotation** (SVector a, SVector b)                                                                                                                                                                         |

### Properties Index

| Property                                           |
| -------------------------------------------------- |
| float [**X**](#x) `get` `set`                      |
| float [**Y**](#y) `get` `set`                      |
| float [**Z**](#z) `get` `set`                      |
| float [**W**](#w) `get` `set`                      |
| SVector [**EulerAngles** ](#eulerangles)`get`      |
| SQuaternion [**Inverse** ](#inverse)`get`          |
|                                                    |
| **Static Properties**                              |
| static SQuaternion [**Identity** ](#identity)`get` |

## Functions

### ToString

string **ToString** ()

*Converts a quaternion to a human readable string*

{% tabs %}
{% tab title="Lua" %}

```lua
aQuaternion = Quaternion.New(0.0, 1.0, 0.0, 0.0)
theString = aQuaternion.ToString()
```

{% endtab %}
{% endtabs %}

### Angle

float **Angle** (SQuaternion b)

*Returns the angle between two quaternions*

| Parameter | Type | Description |
| --------- | ---- | ----------- |
|           |      |             |

{% tabs %}
{% tab title="Lua" %}

```lua
local newQuat = Quaternion.New(0.0, 0.707, 0.0, 0.707);
local otherQuat = Quaternion.New(0.0, 1.0, 0.0, 0.0);
angle = newQuat.Angle(otherQuat);

Space.Log(angle);
-- prints 90.0173034667969
```

{% endtab %}
{% endtabs %}

### Lerp

SQuaternion **Lerp** (SQuaternion b, float t)

*Linearly interpolates between this and other quaternion, by factor t and returns the result*

| Parameter | Type | Description |
| --------- | ---- | ----------- |
|           |      |             |

{% tabs %}
{% tab title="Lua" %}

```lua
QuatA = Quaternion.New(0.0, 0.707, 0.0, 0.707)
QuatB = Quaternion.New(0.0, 1.0, 0.0, 0.0)
QuatLerpAB = QuatA.Lerp(QuatB, Space.Time * 0.1)
```

{% endtab %}
{% endtabs %}

{% tabs %}
{% tab title="Lua" %}

```lua
local cube = Space.Host.ExecutingObject

local fromQuat = cube.LocalRotation
local toQuat = Quaternion.New(0.0, 0.707, 0.0, 0.707)
local speed = 0.1

-- The cube will rotate 90 degrees from current rotation by speed amount.
local moveCube = function()
  cube.LocalRotation = fromQuat.Lerp(toQuat, Space.Time * speed);
end

cube.OnUpdate(moveCube)
```

{% endtab %}
{% endtabs %}

### Slerp

SQuaternion **Slerp** (SQuaternion b, float t)

*Spherically interpolates between this and other quaternion, by factor t and returns the result*

| Parameter | Type | Description |
| --------- | ---- | ----------- |
|           |      |             |

{% tabs %}
{% tab title="Lua" %}

```lua
QuatA = Quaternion.New(0.0, 0.707, 0.0, 0.707)
QuatB = Quaternion.New(0.0, 1.0, 0.0, 0.0)
QuatLerpAB = QuatA.Slerp(QuatB, Space.Time * 0.1)
```

{% endtab %}
{% endtabs %}

{% tabs %}
{% tab title="Lua" %}

```lua
local cube = Space.Host.ExecutingObject;

local fromQuat = cube.LocalRotation;
local toQuat = Quaternion.New(0.0, 1.0, 0.0, 0.0);
local speed = 0.1;

-- The cube will rotate 180 degrees from current rotation by speed amount.
local moveCube = function()
  cube.LocalRotation = fromQuat.Slerp(toQuat, Space.Time * speed)
end

cube.OnUpdate(moveCube)
```

{% endtab %}
{% endtabs %}

### RotateTowards

SQuaternion **RotateTowards** (SQuaternion b, float delta)

*Rotates this towards other, by no more than t degrees*

| Parameter | Type | Description |
| --------- | ---- | ----------- |
|           |      |             |

{% tabs %}
{% tab title="Lua" %}

```lua
QuatA = Quaternion.New(0.0, 0.707, 0.0, 0.707)
QuatB = Quaternion.New(0.0, 1.0, 0.0, 0.0)
ARotatedTowardB = QuatA.RotateTowards(QuatB, Space.Time * 0.1)
```

{% endtab %}
{% endtabs %}

{% tabs %}
{% tab title="Lua" %}

```lua
local cube = Space.Host.ExecutingObject

local fromQuat = cube.LocalRotation
local target = Quaternion.New(0.0, 0.707, 0.0, 0.707)
local speed = 10.0

-- The cube will rotate 90 degrees from current rotation by step amount.
local moveCube = function()
  local step = speed * Space.Time
  cube.LocalRotation = fromQuat.RotateTowards(target, step)
end

cube.OnUpdate(moveCube)
```

{% endtab %}
{% endtabs %}

### Dot

float **Dot** (SQuaternion b)

*Returns the dot product of this and another quaternion*

| Parameter | Type | Description |
| --------- | ---- | ----------- |
|           |      |             |

{% tabs %}
{% tab title="Lua" %}

```lua
local quat = Quaternion.New(0.0, 0.707, 0.0, 0.707);
local quatOther = Quaternion.New(0.0, 0.0, 0.0, 1.0);
Space.Log(quat.Dot(quatOther));
-- prints 0.707000017166138
```

{% endtab %}
{% endtabs %}

### Equals

bool **Equals** (SQuaternion other)

*Function Description*

| Parameter | Type        | Description                                      |
| --------- | ----------- | ------------------------------------------------ |
| other     | SQuaternion | The other Quaternion that we are comparing with. |

{% tabs %}
{% tab title="Lua" %}

```lua
QuatA = Quaternion.New(0.0, 0.707, 0.0, 0.707)
QuatB = Quaternion.New(0.0, 1.0, 0.0, 0.0)
isEqual = QuatA.Equals(QuatB)
```

{% endtab %}
{% endtabs %}

### New

static SQuaternion **New** (float x, float y, float z, float w)

*Creates a new Quaternion*

| Parameter | Type | Description |
| --------- | ---- | ----------- |
|           |      |             |

{% tabs %}
{% tab title="Lua" %}

```lua
newQuat = Quaternion.New(0.0, 0.707, 0.0, 0.707)
```

{% endtab %}
{% endtabs %}

### Euler

static SQuaternion **Euler** (float x, float y, float z)

*Creates a quaternion using Euler angles.*

| Parameter | Type | Description |
| --------- | ---- | ----------- |
|           |      |             |

{% tabs %}
{% tab title="Lua" %}

```lua
newQuat = Quaternion.Euler(0.0, 90.0, 0.0)
```

{% endtab %}
{% endtabs %}

### AngleAxis

static SQuaternion **AngleAxis** ([SVector](https://docs.breakroom.tech/scripting/client-scripting-api-reference/types/svector) axis, float angle)

*Creates a quaternion from an Angle/Axis pair*

| Parameter | Type | Description |
| --------- | ---- | ----------- |
|           |      |             |

{% tabs %}
{% tab title="Lua" %}

```lua
newVector = Vector.New(0.0, 90.0, 0.0)
newQuat = Quaternion.AngleAxis(newVector, 90.0)
```

{% endtab %}
{% endtabs %}

### LookRotation

static SQuaternion **LookRotation** ([SVector](https://docs.breakroom.tech/scripting/client-scripting-api-reference/types/svector) forward)\
static SQuaternion **LookRotation** ([SVector](https://docs.breakroom.tech/scripting/client-scripting-api-reference/types/svector) forward, [SVector](https://docs.breakroom.tech/scripting/client-scripting-api-reference/types/svector) up)

*Creates a quaternion a forward vector; presuming up is (0,1,0)*

| Parameter | Type | Description |
| --------- | ---- | ----------- |
|           |      |             |

{% tabs %}
{% tab title="Lua" %}

```lua
local newQuat = Quaternion.LookRotation(Vector.Forward);
-- or
local newQuat = Quaternion.LookRotation(Vector.Forward, Vector.Up);
```

{% endtab %}
{% endtabs %}

### operator\*

static [SVector](https://docs.breakroom.tech/scripting/client-scripting-api-reference/types/svector) **operator**\* (SQuaternion a, [SVector](https://docs.breakroom.tech/scripting/client-scripting-api-reference/types/svector) b)\
static SQuaternion **operator**\* (SQuaternion a, SQuaternion b)

*The result of using the \* operator.*

| Parameter | Type | Description |
| --------- | ---- | ----------- |
|           |      |             |

{% tabs %}
{% tab title="Lua" %}

```lua
newQuat = Quaternion.New(0.0, 1.0, 0.0, 0.0)
newVector = Vector.Forward
rotatedVector = newQuat * newVector

--or

newQuat = Quaternion.New(0.0, 0.707, 0.0, 0.707)
newVector = Vector.New(0.0, 0.0, 0.0)
rotatedVector = newQuat * newQuat
```

{% endtab %}
{% endtabs %}

### FromToRotation

static SQuaternion **FromToRotation** (SVector a, SVector b)

*Creates a rotation which rotates from a to b.*

*Usually you use this to rotate a transform so that one of its axes eg. the y-axis - follows a target direction b in world space.*

| Parameter | Type | Description |
| --------- | ---- | ----------- |
|           |      |             |

{% tabs %}
{% tab title="Lua" %}

```lua
VectorA = Vector.New(0.0,1.0,0.0)
VectorB = Vector.New(1.0,0.0,0.0)
QuaternionFromToRotation = Quaternion.FromToRotation(VectorA, VectorB)
```

{% endtab %}
{% endtabs %}

## Properties

### X

float **X** `get` `set`

*X axis*

{% tabs %}
{% tab title="Lua" %}

```lua
newQuat = Quaternion.New(0.0, 1.0, 0.0, 0.0)
newQuat.X = 40.0
```

{% endtab %}
{% endtabs %}

{% tabs %}
{% tab title="Lua" %}

```lua
obj = Space.Host.ExecutingObject
originalRot = obj.LocalRotation

onStartMethod = function()
  Space.Log(originalRot.x)
  -- prints the X component of this object as a float

  originalRot.x = 0.25
  -- assigns 0.25 value to the X component

  obj.LocalRotation = originalRot
  -- sets the the new rotation
end

obj.OnStart(onStartMethod)
```

{% endtab %}
{% endtabs %}

### Y

float **Y** `get` `set`

*Y axis*

{% tabs %}
{% tab title="Lua" %}

```lua
newQuat = Quaternion.New(0.0, 1.0, 0.0, 0.0)
newQuat.Y = 40.0
```

{% endtab %}
{% endtabs %}

{% tabs %}
{% tab title="Lua" %}

```lua
 obj = Space.Host.ExecutingObject
 originalRot = obj.LocalRotation

 onStartMethod = function()
  Space.Log(originalRot.y);
  -- prints the Y component of this object as a float

  originalRot.y = 0.25;
  -- assigns 0.25 value to the Y component

  obj.LocalRotation = originalRot;
  -- sets the the new rotation
end

obj.OnStart(onStartMethod);
```

{% endtab %}
{% endtabs %}

### Z

float **Z** `get` `set`

*Z axis*

{% tabs %}
{% tab title="Lua" %}

```lua
newQuat = Quaternion.New(0.0, 1.0, 0.0, 0.0)
newQuat.Z = 40.0
```

{% endtab %}
{% endtabs %}

{% tabs %}
{% tab title="Lua" %}

```lua
local obj = Space.Host.ExecutingObject;
local originalRot = obj.LocalRotation;
obj.SubscribeToEvents();

local onStartMethod = function()
  Space.Log(originalRot.z);
  -- prints the Z component of this object as a float

  originalRot.z = 0.25;
  -- assigns 0.25 value to the Z component

  obj.LocalRotation = originalRot;
  -- sets the the new rotation
end

obj.OnStart(onStartMethod);
```

{% endtab %}
{% endtabs %}

### W

float **W** `get` `set`

*W axis*

{% tabs %}
{% tab title="Lua" %}

```lua
newQuat = Quaternion.New(0.0, 1.0, 0.0, 0.0)
newQuat.W = 40.0
```

{% endtab %}
{% endtabs %}

{% tabs %}
{% tab title="Lua" %}

```lua
local obj = Space.Host.ExecutingObject;
local originalRot = obj.LocalRotation;
obj.SubscribeToEvents();

local onStartMethod = function()
  Space.Log(originalRot.w);
  -- prints the W component of this object as a float

  originalRot.w = 0.25;
  -- assigns 0.25 value to the W component

  obj.LocalRotation = originalRot;
  -- sets the the new rotation
end

obj.OnStart(onStartMethod);
```

{% endtab %}
{% endtabs %}

### EulerAngles

[SVector](https://docs.breakroom.tech/scripting/client-scripting-api-reference/types/svector) **EulerAngles** `get`

*Returns the Euler rotation for this Quaternion*

{% tabs %}
{% tab title="Lua" %}

```lua
newQuat = Quaternion.New(0.0, 0.707, 0.0, 0.707)
euler = newQuat.EulerAngles
```

{% endtab %}
{% endtabs %}

### Inverse

SQuaternion **Inverse** `get`

*Returns the inverse of this quaternion*

{% tabs %}
{% tab title="Lua" %}

```lua
quat = Quaternion.New(0.0, 0.707, 0.0, 0.707)
inverseQuat = quat.Inverse
```

{% endtab %}
{% endtabs %}

### Identity

static SQuaternion **Identity** `get`

*Equivalent of new SQuaternion(0,0,0,1)*

{% tabs %}
{% tab title="Lua" %}

```lua
identity = Quaternion.Identity
```

{% endtab %}
{% endtabs %}
