Create a 2D vector with the x and y passed in, can also be created with vec2()
x number optional, default 0X axis location
y number optional, default 0Y axis location
let a = new Vector2(2, 3); // vector with coordinates (2, 3)
let b = new Vector2; // vector with coordinates (0, 0)
let c = vec2(4, 2); // use the vec2 function to make a Vector2
let d = a.add(b).scale(5); // operators can be chained
Returns a copy of this vector with absolute values
Returns a copy of this vector plus the vector passed in
v Vector2other vector
Returns the clockwise angle of this vector, up is angle 0
Returns the area this vector covers as a rectangle
Returns true if this vector is within the bounds of an array size passed in
arraySize Vector2
Returns a new vector clamped to length passed in
length number optional, default 1
Returns a new vector that is a copy of this
Returns the cross product of this and the vector passed in
v Vector2other vector
Returns the integer direction of this vector, corresponding to multiples of 90 degree rotation (0-3)
Returns the distance from this vector to vector passed in
v Vector2other vector
Returns the distance squared from this vector to vector passed in
v Vector2other vector
Returns a copy of this vector divided by the vector passed in
v Vector2other vector
Returns the dot product of this and the vector passed in
v Vector2other vector
Returns a copy of this vector with each axis floored
Checks if this is a valid vector
Returns the length of this vector
Returns the length of this vector squared
Returns a new vector that is p percent between this and the vector passed in, percent is clamped to 0-1
v Vector2other vector
percent number
Returns new vec2 with modded values
divisor number optional, default 1
Returns a copy of this vector times the vector passed in
v Vector2other vector
Returns a new vector in same direction as this one with the length passed in
- A zero vector has no direction, so it normalizes to straight up
length number optional, default 1
Returns a copy this vector reflected by the surface normal
normal Vector2surface normal (should be normalized)
restitution number optional, default 1how much to bounce, 1 is perfect bounce, 0 is no bounce
Returns copy of this vector rotated by the clockwise angle passed in
angle number
Returns a copy of this vector with each axis rounded
Returns a copy of this vector scaled by the number passed in
s numberscale
Sets values of this vector and returns self
x number optional, default 0X axis location
y number optional, default 0Y axis location
Sets this vector with clockwise angle and length passed in
angle number optional, default 0length number optional, default 1
Sets this vector to point in the specified integer direction (0-3), corresponding to multiples of 90 degree rotation
direction numberlength number optional, default 1
Sets this vector from another vector and returns self
v Vector2other vector
Returns a copy of this vector snapped down to a grid. Note that grid is
the number of snap steps per unit (so grid=2 snaps to halves and
grid=0.5 snaps to twos), not the cell size.
grid numbersnap steps per unit
Returns a copy of this vector minus the vector passed in
v Vector2other vector
Returns this vector expressed as a string
digits number optional, default 3precision to display