Skip to content

Vector2

The Vector2 class represents a two-dimensional vector with x and y components, providing essential functionalities for vector operations.

Usage

To use this class, add the following require at the top of your script:

local Vector2 = require 'engine/math/vector2'

Reference

Operators

+

+(Vector2, Vector2): Vector2

Component-wise addition

-

-(): Vector2

Unary minus (negation)

*

*(Vector2, number): Vector2

Multiply vector by scalar

/

/(Vector2, number): Vector2

Divide vector by scalar

==

==(Vector2, Vector2): boolean

Equality check

tostring

tostring(): string

String representation "[x,y]"

new

static new(x, y)

Create a new Vector2

Parameters

  • x (number): x component
  • y (number): y component

set

set(x, y)

Set values

Parameters

  • x (number): x component
  • y (number): y component

get

get()

Get values

Returns

  • array: y component

toData

toData()

Get values of vector

Returns

  • table: values

sqrMagnitude

sqrMagnitude()

Return the squared magnitude of the vector

Returns

  • number: magnitude

clone

clone()

Create a clone of the vector

Returns

  • Vector2: new vector

normalized

normalized()

Return the normalized version of this vector

Returns

  • Vector2: normalized vector

normalize

normalize()

Normalize the vector

dot

static dot(lhs, rhs)

Calculate dot product of 2 vectors

Parameters

  • lhs (Vector2): first vector
  • rhs (Vector2): second vector

Returns

  • number: result

cross

static cross(lhs, rhs)

Calculate cross product of 2 vectors

Parameters

  • lhs (Vector2): first vector
  • rhs (Vector2): second vector

Returns

  • number: result

angle

static angle(from, to)

Calculate angle between 2 vectors

Parameters

  • from (Vector2): first vector
  • to (Vector2): second vector

Returns

  • number: angle in degrees

magnitude

magnitude()

Calculate the magnitude of the vector

Returns

  • number: magnitude

div

div(d)

Divide this vector by a scalar. Result is stored in this vector.

Parameters

  • d (number): the divisor

Returns

  • Vector2: itself

mul

mul(d)

Multiply this vector with a number. Result is stored in this vector.

Parameters

  • d (number): the factor

Returns

  • Vector2: itself

add

add(b)

Adds another vector to this vector. Result is stored in this vector

Parameters

  • b (Vector2): the summand

Returns

  • Vector2: itself

sub

sub(b)

Subtract another vector from this vector. Result is stored in this vector

Parameters

  • b (Vector2): the subtrahend

Returns

  • Vector2: itself