Skip to content

Vector

vec is a global constructor for 3D vectors. Creates a {x, y, z} table. All methods that accept vectors will accept {x, y, z}, {x=x, y=y, z=z} and vec(x, y, z)

local v = vec(1, 2, 3)

Metatable name: "vec"

Three read/write numeric properties: v.x, v.y, v.z.

Returns sqrt(x² + y² + z²) as a number.

local len = vec(3, 4, 0):length() -- 5.0

Returns x² + y² + z² without the square root. Faster than length() when only comparisons are needed.

local sq = vec(3, 4, 0):lengthSq() -- 25.0

Returns the Euclidean distance between two vectors.

local d = vec(0, 0, 0):distance(vec(3, 4, 0)) -- 5.0

Returns the squared distance. Faster than distance() for range checks.

if pos:distanceSq(target) < 100 then -- within 10 blocks
...
end

Returns a unit vector (length ≈ 1) in the same direction. Returns (0, 0, 0) for zero vectors.

local dir = vec(3, 4, 0):normalized() -- approx (0.6, 0.8, 0)

Returns the dot product.

local d = vec(1, 0, 0):dot(vec(0, 1, 0)) -- 0.0

Returns the cross product.

local c = vec(1, 0, 0):cross(vec(0, 1, 0)) -- vec(0, 0, 1)
OperatorExampleBehaviour
+v1 + v2Component-wise addition
-v1 - v2Component-wise subtraction
*v1 * v2Component-wise multiplication
*v * n or n * vScalar multiplication
/v / nScalar division
--vNegation
==v1 == v2Equality (all components equal)
tostringtostring(v)Returns "(x, y, z)"
local a = vec(1, 2, 3)
local b = vec(4, 5, 6)
local sum = a + b -- vec(5, 7, 9)
local diff = a - b -- vec(-3, -3, -3)
local compMul = a * b -- vec(4, 10, 18)
local scalar = a * 10 -- vec(10, 20, 30)
local div = a / 2 -- vec(0.5, 1, 1.5)
local neg = -a -- vec(-1, -2, -3)
print(tostring(a)) -- "(1, 2, 3)"