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¶
- class Vector2¶
- module:
- static new(x, y)¶
Create a new Vector2
- Parameters:
x (
number) – x componenty (
number) – y component
- set(x, y)¶
Set values
- Parameters:
x (
number) – x componenty (
number) – y component
- get()¶
Get values
- Returns:
x, y component
- Return type:
array
- toData()¶
Get values of vector
- Returns:
the values
- Return type:
table
- sqrMagnitude()¶
Return the squared magnitude of the vector
- Returns:
squared magnitude
- Return type:
number
- normalized()¶
Return the normalized version of this vector
- Returns:
the normalized vector
- Return type:
- normalize()¶
Normalize the vector
- static dot(lhs, rhs)¶
Calculate dot product of 2 vectors
- static angle(from, to)¶
Calculate angle between 2 vectors
- magnitude()¶
Calculate the magnitude of the vector
- Returns:
the magnitude
- Return type:
number
- div(d)¶
Divide this vector by a scalar. Result is stored in this vector.
- Parameters:
d (
number) – the divisor- Returns:
returns itself
- Return type:
- mul(d)¶
Multiply this vector with a number. Result is stored in this vector.
- Parameters:
d (
number) – the factor- Returns:
returns itself
- Return type:
- add(b)¶
Adds another vector to this vector. Result is stored in this vector