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:
Reference
Operators
+
Component-wise addition
-
Unary minus (negation)
*
Multiply vector by scalar
/
Divide vector by scalar
==
Equality check
tostring
String representation "[x,y]"
new
Create a new Vector2
Parameters
x(number): x componenty(number): y component
set
Set values
Parameters
x(number): x componenty(number): y component
get
Get values
Returns
array: y component
toData
Get values of vector
Returns
table: values
sqrMagnitude
Return the squared magnitude of the vector
Returns
number: magnitude
clone
Create a clone of the vector
Returns
Vector2: new vector
normalized
Return the normalized version of this vector
Returns
Vector2: normalized vector
normalize
Normalize the vector
dot
Calculate dot product of 2 vectors
Parameters
lhs(Vector2): first vectorrhs(Vector2): second vector
Returns
number: result
cross
Calculate cross product of 2 vectors
Parameters
lhs(Vector2): first vectorrhs(Vector2): second vector
Returns
number: result
angle
Calculate angle between 2 vectors
Parameters
from(Vector2): first vectorto(Vector2): second vector
Returns
number: angle in degrees
magnitude
Calculate the magnitude of the vector
Returns
number: magnitude
div
Divide this vector by a scalar. Result is stored in this vector.
Parameters
d(number): the divisor
Returns
Vector2: itself
mul
Multiply this vector with a number. Result is stored in this vector.
Parameters
d(number): the factor
Returns
Vector2: itself
add
Adds another vector to this vector. Result is stored in this vector
Parameters
b(Vector2): the summand
Returns
Vector2: itself
sub
Subtract another vector from this vector. Result is stored in this vector
Parameters
b(Vector2): the subtrahend
Returns
Vector2: itself