Vector3
Based on game build 714014 | Last updated: 2026-03-10
Overview
Vector3 is a core mathematical utility class for representing and operating on 3D vectors. It provides operator overloading (+, -, *, /, unary -, ==), standard vector operations (Dot, Cross, Normalize, Dist, etc.), and helper functions for constructing vectors from angles or tables. It is not an ECS component but a standalone Lua class used across the codebase for geometric computations (e.g., position deltas, direction vectors, movement logic). The alias Point is provided for semantic clarity when representing points in space.
Usage example
local v1 = Vector3(1, 2, 3)
local v2 = Vector3(4, 5, 6)
local sum = v1 + v2
local diff = v2 - v1
local scaled = v1 * 2
local dot = v1:Dot(v2)
local dist = v1:Dist(v2)
local normalized = v1:GetNormalized()
local point = Vector3FromTheta(math.pi / 2, 10)
Dependencies & tags
Components used: None identified
Tags: None identified
Properties
No public properties (only instance variables x, y, z set in constructor; no accessors or public table fields documented for external use).
Main functions
Vector3(x, y, z)
- Description: Constructor. Creates a new
Vector3instance. All arguments default to0ifnil. - Parameters:
x(number?) — X-coordinate.
y(number?) — Y-coordinate.
z(number?) — Z-coordinate. - Returns: A new
Vector3instance.
:__add(rhs)
- Description: Adds two vectors component-wise.
- Parameters:
rhs(Vector3) — Right-hand side vector. - Returns:
Vector3— Sum of the two vectors.
:__sub(rhs)
- Description: Subtracts
rhsfromselfcomponent-wise. - Parameters:
rhs(Vector3) — Vector to subtract. - Returns:
Vector3— Difference vector.
:__mul(rhs)
- Description: Scales the vector by a scalar value.
- Parameters:
rhs(number) — Scalar multiplier. - Returns:
Vector3— Scaled vector.
:__div(rhs)
- Description: Divides the vector by a scalar.
- Parameters:
rhs(number) — Scalar divisor. - Returns:
Vector3— Scaled-down vector. - Error states: May produce
infornanifrhsis0; not guarded internally.
:__unm()
- Description: Unary minus (negates all components).
- Parameters: None.
- Returns:
Vector3— Negated vector.
:Dot(rhs)
- Description: Computes the dot product of two vectors.
- Parameters:
rhs(Vector3) — Right-hand side vector. - Returns:
number— Dot product value.
:Cross(rhs)
- Description: Computes the cross product of two vectors.
- Parameters:
rhs(Vector3) — Right-hand side vector. - Returns:
Vector3— Perpendicular vector to the plane spanned byselfandrhs.
:Dist(other)
- Description: Computes the Euclidean distance between
selfandother. - Parameters:
other(Vector3) — Target point/vector. - Returns:
number— Distance (non-negative).
:DistSq(other)
- Description: Computes the squared Euclidean distance (avoids
sqrtfor performance). - Parameters:
other(Vector3) — Target point/vector. - Returns:
number— Squared distance.
:Length()
- Description: Computes the magnitude (length) of the vector.
- Parameters: None.
- Returns:
number— Non-negative length.
:LengthSq()
- Description: Computes the squared length (avoids
sqrtfor performance). - Parameters: None.
- Returns:
number— Squared length.
:Normalize()
- Description: Normalizes the vector in-place to unit length.
- Parameters: None.
- Returns:
self— Modified vector (unit length if original length > 0); otherwise, unchanged. - Error states: No-op (returns unmodified) if
Length()is0to avoid division by zero.
:GetNormalized()
- Description: Returns a new normalized copy of the vector.
- Parameters: None.
- Returns:
Vector3— Unit vector in the same direction. - Error states: May produce
inf/nanifLength()is0; no guard is applied.
:GetNormalizedAndLength()
- Description: Efficiently returns both the normalized vector and its original length in one pass.
- Parameters: None.
- Returns:
- First return:
Vector3— Normalized vector (orselfif length is0). - Second return:
number— Original length.
- First return:
:Get()
- Description: Returns the raw
x,y,zcomponents. - Parameters: None.
- Returns:
number, number, number— The three coordinates.
:IsVector3()
- Description: Type check to identify
Vector3instances. - Parameters: None.
- Returns:
true— Always returnstrue.
ToVector3(obj, y, z)
- Description: Coerces various inputs into a
Vector3. Handlesnil, existingVector3s, tables, or three separate numbers. - Parameters:
obj(any?) — If table orIsVector3, used to construct vector; otherwise treated asx.
y(number?) — Y-coordinate (ifobjis not a table or vector).
z(number?) — Z-coordinate (ifobjis not a table or vector). - Returns:
Vector3?— A newVector3ornilifobjisnilortonumberfails. - Error states: Returns
nilifobjisnilortonumberresults arenil.
Vector3FromTheta(theta, radius)
- Description: Creates a 2D horizontal vector (y=0) from a polar angle (
theta) and radius in the XZ plane. Positivethetarotates clockwise around the Y-axis (Z negative for forward). - Parameters:
theta(number) — Angle in radians.
radius(number?) — Magnitude; defaults to1. - Returns:
Vector3—(radius * cos(theta), 0, -radius * sin(theta)).
Events & listeners
Not applicable.