A simple and flexible 2D vector class written in Lua.
- Create 2D vectors with
x
,y
components and precomputedmagnitude
- Operator overloading:
- Addition (
+
) - Division (
/
) - Subtraction (
-
) - Multiplication (
*
) - Equality check (
==
)
- Addition (
- In-place operations:
:iadd(vector)
— modifies the current vector by adding another:isub(vector)
— modifies the current vector by subtracting another:imul(vector)
— modifies the current vector by multiplying by another
- Convert a vector to a formatted string with
:tostring()
Creates a new vector with the specified x and y values. Parameters:
- x (number): The x-component of the vector.
- y (number): The y-component of the vector.
Returns:
- A new vector2d instance.
Calculates the magnitude of the vector using the formula: √(x² + y²). This is called automatically when creating a new vector or modifying it with operations.
Normalizes the vector (scales it to have a magnitude of 1), unless the vector is (0, 0).
Returns:
- The normalized vector2d instance (modifies the current vector).
-
vector2d:iadd(v) Adds another vector v to the current vector, modifying the original vector.
-
vector2d:isub(v) Subtracts another vector v from the current vector, modifying the original vector.
-
vector2d:imul(v) Multiplies the current vector by another vector v, modifying the original vector.
-
vector2d:idiv(v) Divides the current vector by another vector v, modifying the original vector.
Returns the vector as a formatted string in the format: (x, y) Returns:
- A string representation of the vector.
local vector2d = require("vector2d")
-- Creating vectors
local a = vector2d:new(3, 4)
local b = vector2d:new(1, 2)
-- Operator-based (returns new vectors)
local sum = a + b
local diff = a - b
local prod = a * 10
print(sum:tostring()) -- Output: (4.000000, 6.000000)
print(diff:tostring()) -- Output: (2.000000, 2.000000)
print(prod:tostring()) -- Output: (30.000000, 40.000000)
print(a == vector2d:new(3, 4)) -- Output: true
print("Magnitude of a:", a.magnitude) -- Output: 5
-- In-place modification (modifies the object and returns self)
a:iadd(b):isub(b):imul(10)
print("Modified a:", a:tostring())
Using love2d I created an example of use, just run love2 in the examples folder
love examples
output: