Skip to content

vec2

A two-dimensional vector containing x and y floating-point values. This type is used for UI positioning, element dimensions, and texture coordinates (UVs).

Initialize a vector with specific values, or clone an existing one.

-- create a vector at 0,0
local v1 = vec2()
-- create a vector with specific coordinates
local v2 = vec2(100, 50)
-- create a vector from another vec2 (cloning)
local v3 = vec2(v2)

The vec2 type is the foundation of the ui and display namespaces.

In standard UI contexts, (0, 0) represents the top-left corner of the window or screen. Positive x values move to the right, and positive y values move downward. The type supports operator overloading for +, -, *, /, equality checks (==), and the # operator for vector length.

Methods such as :add(), :sub(), and :normalize() modify the vector in place and return it, allowing for method chaining (for example, v:add(v2):scale(2)).

PropertyTypeDescription
xnumberThe horizontal component.
ynumberThe vertical component.
MethodDescription
:set(x, y)Updates the vector’s components in place.
:normalize(out)Scales the vector to a length of 1.
:length()Returns the magnitude of the vector.
:distance(other)Returns the distance between two points.
:lerp(other, mix, out)Linearly interpolates toward another vector.
:unpack()Returns x and y as two separate numbers.

Using vec2 to define the position and size of a drawn rectangle.

function script.windowMain()
local pos = vec2(50, 50)
local size = vec2(200, 100)
-- draw a rectangle using the vectors
ui.drawRectFilled(pos, pos + size, rgbm.colors.red)
end