Skip to main content
Represents a vector with x and y components.

Fields

x

Represents a vector with x and y components. The x component, note that this is read-only.
local v = Vector.xy(10, -5)
local xValue = v.x  -- 10

y

The y component, note that this is read-only.
local v = Vector.xy(10, -5)
local yValue = v.y  -- -5

Constructors

xy

Creates a vector with the specified x and y components.
local v = Vector.xy(5, -2)  -- (5, -2)

origin

Returns the zero vector (0, 0).
local origin = Vector.origin()  -- (0,0)

Methods

length

Provides indexed access to the vector components.
local v = Vector.xy(3, 4)
print(v[1], v[2]) -- 3  4
Returns the length of the vector.
local v = Vector.xy(3, 4)
local len = v:length()  -- 5

lengthSquared

Returns the squared length of the vector.
local v = Vector.xy(3, 4)
local len2 = v:lengthSquared()  -- 25

normalized

Returns a normalized copy of the vector. If the length is zero, the result is the zero vector.
local v = Vector.xy(10, 0)
local n = v:normalized()  -- (1,0)

__eq

Returns true if the two vectors have equal components.
local a = Vector.xy(1, 2)
local b = Vector.xy(1, 2)
local c = Vector.xy(2, 1)
print(a == b)  -- true
print(a == c)  -- false

__unm

Returns the negated vector.
local v = Vector.xy(2, -3)
local neg = -v   -- (-2, 3)

__add

Returns the sum of two vectors.
local a = Vector.xy(2, 3)
local b = Vector.xy(-1, 5)
local c = a + b  -- (1, 8)

__sub

Returns the difference between two vectors.
local a = Vector.xy(2, 3)
local b = Vector.xy(-1, 5)
local c = a - b  -- (3, -2)
Returns the difference between two vectors.

__mul

Returns the vector scaled by the given number.
local v = Vector.xy(3, -2)
local doubled = v * 2    -- (6, -4)

__div

Returns the vector divided by the given number
local v = Vector.xy(6, -4)
local half = v / 2    -- (3, -2)

distance

Returns the distance to the other vector.
local a = Vector.xy(0, 0)
local b = Vector.xy(3, 4)
print(a:distance(b))  -- 5

distanceSquared

Returns the squared distance to the other vector.
local a = Vector.xy(0, 0)
local b = Vector.xy(3, 4)
print(a:distanceSquared(b))  -- 25

dot

Returns the dot product of the vector and the other vector.
local a = Vector.xy(1, 2)
local b = Vector.xy(3, 4)
print(a:dot(b))  -- 11  (1*3 + 2*4)

lerp

Returns the linear interpolation between the vector and the other vector, using t where 0 returns the vector and 1 returns the other.