Skip to content

Math API

Neon provides high-performance vector types under the types.* global table for 2D, 3D, and 4D mathematical operations.

Vector Types

  • types.vector2
  • types.vector3
  • types.vector4

Creating Vectors

lua
local v2 = types.vector2.new(10, 20)
local v3 = types.vector3.new(1, 2, 3)
local v4 = types.vector4.new(0, 0, 0, 1)

Operator Overloading

All vector types support standard Lua mathematical operators. These operators always return a **new ** vector instance.

OperatorDescription
+Addition of two vectors
-Subtraction of two vectors
*Multiplication by vector or scalar (number)
/Division by vector or scalar (number)
- (unary)Negation of all components

Example

lua
local pos = types.vector2.new(100, 100)
local dir = types.vector2.new(1, 0)
local speed = 5

-- Move position along direction
local new_pos = pos + (dir * speed)

print(new_pos) -- vector2(105.00, 100.00)

Common Methods

Vectors also provide instance methods for more advanced operations.

MethodSignatureDescription
add(other: vector): vectorFunctional equivalent of +
sub(other: vector): vectorFunctional equivalent of -
mul(other: vector|number): vectorFunctional equivalent of *
div(other: vector|number): vectorFunctional equivalent of /
dist(other: vector): numberReturns distance to another point
length(): numberReturns the magnitude of the vector
dot(other: vector): numberReturns the dot product
normalize(): vectorReturns a vector with length 1.0
lua
local v = types.vector2.new(3, 4)
print(v.length()) -- 5.0
print(v.normalize().length()) -- 1.0