Dev » Lua API » vector2
← Back to all classes

vector2

Math 2 Properties 4 Methods

vector2

The vector2 class represents 2D floating point coordinates (X, Y). It supports arithmetic operator overloading (+, -, *), magnitude calculation, normalization, and dot products.

Overview

Properties (2)

Property Type Summary
x number The X coordinate of the 2D vector.
y number The Y coordinate of the 2D vector.

Methods (4)

Method Signature Returns Summary
vector2:dot(other) number Calculates the dot product with another 2D vector.
vector2:length() number Returns the length (magnitude) of the vector.
vector2.new(x, y) vector2 Constructs a new 2D vector.
vector2:normalized() vector2 Returns a normalized unit vector pointing in the same direction.

Detailed Reference

# x PROPERTY
vector2.x
The X coordinate of the 2D vector.
Parameters
None
Returns
number

The X coordinate component.

Description

Reads or writes the X component of the vector.

# y PROPERTY
vector2.y
The Y coordinate of the 2D vector.
Parameters
None
Returns
number

The Y coordinate component.

Description

Reads or writes the Y component of the vector.

# dot METHOD
vector2:dot(other)
Calculates the dot product with another 2D vector.
Parameters
ParameterTypeDefaultDescription
othervector2

The other vector.

Returns
number

The scalar dot product.

Description

Returns x1 x2 + y1 y2.

# length METHOD
vector2:length()
Returns the length (magnitude) of the vector.
Parameters
None
Returns
number

Magnitude / Euclidean length.

Description

Calculates the Euclidean length sqrt(x^2 + y^2).

# new METHOD
vector2.new(x, y)
Constructs a new 2D vector.
Parameters
ParameterTypeDefaultDescription
xnumber

The X coordinate.

ynumber

The Y coordinate.

Returns
vector2

Newly constructed 2D vector.

Description

Creates a new 2D vector with the specified X and Y coordinates.

local v = vector2.new(10.5, 20.0)
# normalized METHOD
vector2:normalized()
Returns a normalized unit vector pointing in the same direction.
Parameters
None
Returns
vector2

Unit vector with length 1.0.

Description

Scales the vector to unit length (1.0). Returns (0, 0) if the vector is zero.