Skip to main content

Vector2

Defines a 2D vector, that can be used to represent a position, direction or rotation of an object in two-dimensions (x,y). A common use for Vector2 is defining the position of elements in the Hud.

Constructors

Vector2.new

Creates a new Vector2 with the given x and y values.

Example

-- Creates a new Vector2 with x=1, y=2, and assigns the vector to variable "a"
local a = Vector2.new(1, 2)
local b = Vector2.new()

print(a) -- Prints '(1, 2)'
print(b) -- Prints '(0, 0)'

Properties

x

The x component of the Vector2.

Example

local a = Vector2.new(1, 2)

print(a.x) -- Prints '1'

y

The y component of the Vector2.

Example

local a = Vector2.new(1, 2)

print(a.y) -- Prints '2'

Functions

angle(a, b)

Returns the angle (in degrees) between the two vectors.

Example

local a = Vector2.new(1, 0)
local b = Vector2.new(1, 1)

print(Vector2.angle(a, b)) -- Prints '45'

dot(a, b)

Returns the dot product of the two vectors.

Example

local a = Vector2.new(1, 1)
local b = Vector2.new(2, 0)

print(Vector2.dot(a, b)) -- Prints '0.5'

cross(a, b)

Returns the cross product of the two vectors.

Example

local a = Vector2.new(1, 0)
local b = Vector2.new(0, 1)

print(Vector2.cross(a, b)) -- Prints '(0, 0, 1)'

distance(a, b)

Returns the distance between the two vectors.

Example

local a = Vector2.new(1, 1)
local b = Vector2.new(1, 2)

print(Vector2.distance(a, b)) -- Prints '1'

lerp(from, to, t)

Returns a Vector2 where the value of each component is linearly interpolated from the input vectors, by the value specified in "t" (which should be between 0 and 1).

Example

local a = Vector2.new(1, 1)
local b = Vector2.new(2, 2)

print(Vector2.lerp(a, b, 0.5)) -- Prints '(1.5, 1.5)'

magnitude(a)

Returns the length of the input vector.

Example

local a = Vector2.new(1, 1)

print(Vector2.magnitude(a)) -- Prints '1.414'

max(a, b)

Returns a Vector2 where the value of each component is the maximum value from the respective component in each of the input vectors.

Example

local a = Vector2.new(1, 2)
local b = Vector2.new(2, 1)

print(Vector2.max(a, b)) -- Prints '(2, 2)'

min(a, b)

Returns a Vector2 where the value of each component is the minimum value from the respective component in each of the input vectors.

Example

local a = Vector2.new(1, 2)
local b = Vector2.new(2, 1)

print(Vector2.min(a, b)) -- Prints '(1, 1)'

normalize(a)

Returns a Vector2 pointing in the same direction as the input vector, but with a length of 1.

Example

local a = Vector2.new(3, 4)

print(Vector2.normalize(a)) -- Prints '(0.6, 0.8)'

sqrMagnitude(a)

Returns the squared length of the input vector.

Example

local a = Vector2.new(1, 1)

print(Vector2.sqrMagnitude(a)) -- Prints '2'

Math Operations

Vector2 + Vector2

Adds each of the components of the vector.

Example

local a = Vector2.new(1, 2)
local b = Vector2.new(3, 4)

print(a + b) -- Prints '(4, 6)'

Vector2 - Vector2

Subtracts each of the components of the vector.

Example

local a = Vector2.new(1, 2)
local b = Vector2.new(3, 4)

print(a - b) -- Prints '(-2, -2)'

Vector2 * number

Multiplies each of the components of the vector by the number.

Example

local a = Vector2.new(1, 2)

print(a * 3) -- Prints '(3, 6)'

Vector2 * Vector2

Multiplies each of the components of the first vector by the respective component on the second vector.

Example

local a = Vector2.new(1, 2)
local b = Vector2.new(3, 4)

print(a * b) -- Prints '(3, 8)'

Vector2 / number

Divides each of the components of the vector by the number.

Example

local a = Vector2.new(3, 12)

print(a / 3) -- Prints '(1, 4)'

Vector2 / Vector2

Divides each of the components of the first vector by the respective component on the second vector.

Example

local a = Vector2.new(3, 12)
local b = Vector2.new(3, 4)

print(a / b) -- Prints '(1, 3)'

Vector2 ^ Vector2

Returns the cross product of the two vectors.

Example

local a = Vector2.new(1, 0)
local b = Vector2.new(0, 1)

print(a ^ b) -- Prints '(0, 0, 1)'

-Vector2

Returns the negative of the vector.

Example

local a = Vector4.new(1, -2)

print(-a) -- Prints '(-1, 2)'

#Vector2

Returns the length of the vector.

Example

local a = Vector2.new(1, 1)

print(#a) -- Prints '1.414'

Constants

Vector2.zero

Returns a Vector2 where the value of each component is 0.

Example

print(Vector2.zero)    -- Prints '(0, 0)'

Vector2.one

Returns a Vector2 where the value of each component is 1.

Example

print(Vector2.one)    -- Prints '(1, 1)'

Vector2.up

Returns a Vector2 where the value of the y component is 1.

Example

print(Vector2.up)    -- Prints '(0, 1)'

Vector2.down

Returns a Vector2 where the value of the y component is -1.

Example

print(Vector2.down)    -- Prints '(0, -1)'

Vector2.left

Returns a Vector2 where the value of the x component is -1.

Example

print(Vector2.left)    -- Prints '(-1, 0)'

Vector2.right

Returns a Vector2 where the value of the x component is 1.

Example

print(Vector2.right)    -- Prints '(1, 0)'