Appearance
Math API
Neon provides high-performance vector types under the types.* global table for 2D, 3D, and 4D mathematical operations.
Vector Types
types.vector2types.vector3types.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.
| Operator | Description |
|---|---|
+ | 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.
| Method | Signature | Description |
|---|---|---|
add | (other: vector): vector | Functional equivalent of + |
sub | (other: vector): vector | Functional equivalent of - |
mul | (other: vector|number): vector | Functional equivalent of * |
div | (other: vector|number): vector | Functional equivalent of / |
dist | (other: vector): number | Returns distance to another point |
length | (): number | Returns the magnitude of the vector |
dot | (other: vector): number | Returns the dot product |
normalize | (): vector | Returns 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