---
name: vector
last_updated: 2026-12-04T23:21:36Z
type: library
summary: "A library of vector functions."
---

# vector

A library of vector functions.

**Type:** library

## Description

This library implements functionality for the vector type in addition to the
built-in primitive operator support. It uses vectors with three components
(`x`, `y`, and `z`).

Individual vector components can be accessed using the fields `x` or `X`, `y`
or `Y`, `z` or `Z`. Since vector values are immutable, writing to individual
components is not supported.

## Properties

### vector.zero

**Type:** `vector`

A pre-allocated constant equivalent to `vector.create(0, 0, 0)`. Useful as
a default value or additive identity in vector arithmetic without
allocating a new vector each time.

**Vector Zero**

Demonstrates the `vector.zero` constant as an additive identity.

```lua
local position = vector.create(4, 5, 6)
print(vector.zero) --> 0, 0, 0
print(position + vector.zero == position) --> true
```

### vector.one

**Type:** `vector`

A pre-allocated constant equivalent to `vector.create(1, 1, 1)`. Useful as
a multiplicative identity or scale factor in vector arithmetic without
allocating a new vector each time.

**Vector One**

Demonstrates the `vector.one` constant as a multiplicative identity and
uniform scale.

```lua
local size = vector.create(2, 3, 4)
print(vector.one) --> 1, 1, 1
print(size * vector.one == size) --> true
print(vector.one * 5) --> 5, 5, 5
```

## Functions

### vector.create

**Signature:** `vector.create(x: number, y: number, z: number): vector`

Creates a new vector with the given component values. The `z` parameter
defaults to `0` when omitted, so `vector.create(1, 2)` is equivalent to
`vector.create(1, 2, 0)`.

**Parameters:**

| Name | Type | Default | Description |
|------|------|---------|-------------|
| `x` | `number` |  | The x component of the new vector. |
| `y` | `number` |  | The y component of the new vector. |
| `z` | `number` |  | The z component of the new vector. Defaults to `0` when omitted. |

**Returns:** `vector` — A new vector with the specified x, y, and z component values.

**Vector Create**

Demonstrates `vector.create`, including the default `z` of `0`.

```lua
local full = vector.create(1, 2, 3)
print(full.x, full.Y, full.z) --> 1 2 3

print(vector.create(1, 2) == vector.create(1, 2, 0)) --> true
```

### vector.magnitude

**Signature:** `vector.magnitude(vec: vector): number`

Returns the magnitude (Euclidean length) of the vector, computed as
`math.sqrt(vec.x^2 + vec.y^2 + vec.z^2)`.

**Parameters:**

| Name | Type | Default | Description |
|------|------|---------|-------------|
| `vec` | `vector` |  | The vector whose magnitude is computed. |

**Returns:** `number` — The Euclidean length of the vector.

**Vector Magnitude**

Demonstrates `vector.magnitude`, which returns the Euclidean length of a
vector.

```lua
print(vector.magnitude(vector.create(3, 4, 0))) --> 5
print(vector.magnitude(vector.zero)) --> 0
```

### vector.normalize

**Signature:** `vector.normalize(vec: vector): vector`

Returns a unit vector (magnitude of 1) pointing in the same direction as
the input. Computed as `vec / vector.magnitude(vec)`. If the input vector
has zero magnitude, the result contains `nan` components.

**Parameters:**

| Name | Type | Default | Description |
|------|------|---------|-------------|
| `vec` | `vector` |  | The vector to normalize. |

**Returns:** `vector` — A unit vector pointing in the same direction as the input.

**Vector Normalize**

Demonstrates `vector.normalize`, including the `nan` result for a zero-length
vector.

```lua
print(vector.normalize(vector.create(0, 10, 0))) --> 0, 1, 0
print(vector.magnitude(vector.normalize(vector.create(3, 4, 12)))) --> 1

local degenerate = vector.normalize(vector.zero)
print(degenerate.x ~= degenerate.x) --> true
```

### vector.cross

**Signature:** `vector.cross(vec1: vector, vec2: vector): vector`

Returns the cross product of two vectors, which is a vector perpendicular
to both inputs whose magnitude equals the area of the parallelogram they
span. The result is computed as
`vector.create(vec1.y*vec2.z - vec1.z*vec2.y, vec1.z*vec2.x - vec1.x*vec2.z, vec1.x*vec2.y - vec1.y*vec2.x)`.

**Parameters:**

| Name | Type | Default | Description |
|------|------|---------|-------------|
| `vec1` | `vector` |  | The first operand vector. |
| `vec2` | `vector` |  | The second operand vector. |

**Returns:** `vector` — A vector perpendicular to both inputs whose magnitude equals the area
of the parallelogram they span.

**Vector Cross**

Demonstrates `vector.cross`, which returns a vector perpendicular to both
inputs.

```lua
local xAxis = vector.create(1, 0, 0)
local yAxis = vector.create(0, 1, 0)
print(vector.cross(xAxis, yAxis)) --> 0, 0, 1
print(vector.cross(yAxis, xAxis)) --> 0, 0, -1
```

### vector.dot

**Signature:** `vector.dot(vec1: vector, vec2: vector): number`

Returns the dot product (scalar product) of two vectors, computed as
`vec1.x*vec2.x + vec1.y*vec2.y + vec1.z*vec2.z`. The result is a single
number equal to the product of the magnitudes of the two vectors times the
cosine of the angle between them.

**Parameters:**

| Name | Type | Default | Description |
|------|------|---------|-------------|
| `vec1` | `vector` |  | The first operand vector. |
| `vec2` | `vector` |  | The second operand vector. |

**Returns:** `number` — The scalar dot product of the two vectors.

**Vector Dot**

Demonstrates `vector.dot`, which returns the scalar product of two vectors.

```lua
print(vector.dot(vector.create(1, 2, 3), vector.create(4, 5, 6))) --> 32
print(vector.dot(vector.create(1, 0, 0), vector.create(0, 1, 0))) --> 0
```

### vector.angle

**Signature:** `vector.angle(vec1: vector, vec2: vector, axis: vector?): number`

Computes the angle between two vectors in radians. The axis, if specified,
is used to determine the sign of the angle.

**Parameters:**

| Name | Type | Default | Description |
|------|------|---------|-------------|
| `vec1` | `vector` |  | The first vector. |
| `vec2` | `vector` |  | The second vector. |
| `axis` | `vector?` |  | An optional reference axis used to determine the sign of the returned angle. |

**Returns:** `number` — The angle between the two vectors in radians, signed if an axis is
provided.

**Vector Angle**

Demonstrates `vector.angle`, including the signed result when an axis is
supplied.

```lua
local xAxis = vector.create(1, 0, 0)
local yAxis = vector.create(0, 1, 0)
local halfPi = math.pi / 2

print(vector.angle(xAxis, yAxis) == halfPi) --> true
print(vector.angle(xAxis, yAxis, vector.create(0, 0, 1)) == halfPi) --> true
print(vector.angle(xAxis, yAxis, vector.create(0, 0, -1)) == -halfPi) --> true
```

### vector.floor

**Signature:** `vector.floor(vec: vector): vector`

Returns a new vector with each component rounded down to the nearest
integer, equivalent to
`vector.create(math.floor(vec.x), math.floor(vec.y), math.floor(vec.z))`.

**Parameters:**

| Name | Type | Default | Description |
|------|------|---------|-------------|
| `vec` | `vector` |  | The vector whose components are rounded down. |

**Returns:** `vector` — A new vector with each component rounded down to the nearest integer.

**Vector Floor**

Demonstrates `vector.floor`, which rounds every component down.

```lua
print(vector.floor(vector.create(1.7, -1.2, 3))) --> 1, -2, 3
```

### vector.ceil

**Signature:** `vector.ceil(vec: vector): vector`

Returns a new vector with each component rounded up to the nearest
integer, equivalent to
`vector.create(math.ceil(vec.x), math.ceil(vec.y), math.ceil(vec.z))`.

**Parameters:**

| Name | Type | Default | Description |
|------|------|---------|-------------|
| `vec` | `vector` |  | The vector whose components are rounded up. |

**Returns:** `vector` — A new vector with each component rounded up to the nearest integer.

**Vector Ceil**

Demonstrates `vector.ceil`, which rounds every component up.

```lua
print(vector.ceil(vector.create(1.2, -1.7, 3))) --> 2, -1, 3
```

### vector.abs

**Signature:** `vector.abs(vec: vector): vector`

Returns a new vector with each component replaced by its absolute value,
equivalent to
`vector.create(math.abs(vec.x), math.abs(vec.y), math.abs(vec.z))`.

**Parameters:**

| Name | Type | Default | Description |
|------|------|---------|-------------|
| `vec` | `vector` |  | The vector whose components are made absolute. |

**Returns:** `vector` — A new vector with each component replaced by its absolute value.

**Vector Abs**

Demonstrates `vector.abs`, which takes the absolute value of every component.

```lua
print(vector.abs(vector.create(-1, 2, -3))) --> 1, 2, 3
```

### vector.sign

**Signature:** `vector.sign(vec: vector): vector`

Returns a new vector where each component is `1` if the corresponding
input component is positive, `-1` if negative, or `0` if zero. Equivalent
to `vector.create(math.sign(vec.x), math.sign(vec.y), math.sign(vec.z))`.
The behavior for negative zero and `nan` is consistent with `math.sign`.

**Parameters:**

| Name | Type | Default | Description |
|------|------|---------|-------------|
| `vec` | `vector` |  | The vector whose component signs are extracted. |

**Returns:** `vector` — A new vector where each component is the sign of the corresponding
input component.

**Vector Sign**

Demonstrates `vector.sign`, which returns `-1`, `0`, or `1` for each
component.

```lua
print(vector.sign(vector.create(-3.5, 0, 8))) --> -1, 0, 1
```

### vector.clamp

**Signature:** `vector.clamp(vec: vector, min: vector, max: vector): vector`

Returns a new vector with each component clamped between the corresponding
components of `min` and `max`. Each component of `max` must be greater
than or equal to the corresponding component of `min`; otherwise, an error
is raised.

**Parameters:**

| Name | Type | Default | Description |
|------|------|---------|-------------|
| `vec` | `vector` |  | The vector whose components are clamped. |
| `min` | `vector` |  | The vector of per-component minimum bounds. |
| `max` | `vector` |  | The vector of per-component maximum bounds. Each component must be greater than or equal to the corresponding component of `min`. |

**Returns:** `vector` — A new vector with each component clamped between the corresponding
components of `min` and `max`.

**Vector Clamp**

Demonstrates `vector.clamp`, including the error raised when `max` is below
`min`.

```lua
local lower = vector.create(0, 0, 0)
local upper = vector.create(10, 10, 10)
print(vector.clamp(vector.create(-5, 5, 15), lower, upper)) --> 0, 5, 10

print(pcall(vector.clamp, vector.one, upper, lower)) --> false invalid argument #3 to 'clamp' (max.x must be greater than or equal to min.x)
```

### vector.lerp

**Signature:** `vector.lerp(vec1: vector, vec2: vector, alpha: number): vector`

Returns a vector linearly interpolated between two vectors (`vec1`,
`vec2`) by the fraction `alpha`. Note that `alpha` is **not** limited to
the range `[0, 1]`.

**Parameters:**

| Name | Type | Default | Description |
|------|------|---------|-------------|
| `vec1` | `vector` |  | The starting vector (returned when `alpha` is `0`). |
| `vec2` | `vector` |  | The ending vector (returned when `alpha` is `1`). |
| `alpha` | `number` |  | The interpolation fraction. Not clamped to `[0, 1]`. |

**Returns:** `vector` — A vector interpolated component-wise between `vec1` and `vec2`.

**Vector Lerp**

Demonstrates `vector.lerp`, which is not limited to an `alpha` between `0` and
`1`.

```lua
local start = vector.zero
local finish = vector.create(10, 20, 30)
print(vector.lerp(start, finish, 0.5)) --> 5, 10, 15
print(vector.lerp(start, finish, 1.5)) --> 15, 30, 45
```

### vector.max

**Signature:** `vector.max(...: vector): vector`

Returns a new vector where each component is the maximum of the
corresponding components across all input vectors. Accepts a variadic
number of vectors (at least one). Components are compared from left to
right, as [math.max()](/docs/reference/engine/globals/math.md) and [math.min()](/docs/reference/engine/globals/math.md) do. When a
component is `nan` in the first vector, the result is `nan`. When it is
`nan` only in a later vector, the valid number is used.

**Parameters:**

| Name | Type | Default | Description |
|------|------|---------|-------------|
| `...` | `vector` |  | One or more vectors to compare component-wise. |

**Returns:** `vector` — A new vector where each component is the maximum of the corresponding
components across all inputs.

**Vector Max**

Demonstrates `vector.max`, which takes the largest value per component across
any number of vectors.

```lua
local a = vector.create(1, 5, 3)
local b = vector.create(4, 2, 6)
local c = vector.create(0, 9, 0)
print(vector.max(a, b)) --> 4, 5, 6
print(vector.max(a, b, c)) --> 4, 9, 6

-- Components are compared left to right like math.max, so a nan in the first
-- vector is returned, while a nan in a later vector loses to the valid number
local nan = 0 / 0
print(vector.max(vector.create(nan, 1, 1), vector.create(1, 1, 1))) --> nan, 1, 1
print(vector.max(vector.create(1, 1, 1), vector.create(nan, 1, 1))) --> 1, 1, 1
```

### vector.min

**Signature:** `vector.min(...: vector): vector`

Returns a new vector where each component is the minimum of the
corresponding components across all input vectors. Accepts a variadic
number of vectors (at least one). Components are compared from left to
right, as [math.max()](/docs/reference/engine/globals/math.md) and [math.min()](/docs/reference/engine/globals/math.md) do. When a
component is `nan` in the first vector, the result is `nan`. When it is
`nan` only in a later vector, the valid number is used.

**Parameters:**

| Name | Type | Default | Description |
|------|------|---------|-------------|
| `...` | `vector` |  | One or more vectors to compare component-wise. |

**Returns:** `vector` — A new vector where each component is the minimum of the corresponding
components across all inputs.

**Vector Min**

Demonstrates `vector.min`, which takes the smallest value per component across
any number of vectors.

```lua
local a = vector.create(1, 5, 3)
local b = vector.create(4, 2, 6)
local c = vector.create(0, 9, 0)
print(vector.min(a, b)) --> 1, 2, 3
print(vector.min(a, b, c)) --> 0, 2, 0

-- Components are compared left to right like math.min, so a nan in the first
-- vector is returned, while a nan in a later vector loses to the valid number
local nan = 0 / 0
print(vector.min(vector.create(nan, 1, 1), vector.create(1, 1, 1))) --> nan, 1, 1
print(vector.min(vector.create(1, 1, 1), vector.create(nan, 1, 1))) --> 1, 1, 1
```