Vector2

2D Vector object with vector math library

  • Functions do not change this so they can be chained together

Constructor

new Vector2(xopt, yopt)

Create a 2D vector with the x and y passed in, can also be created with vec2()

Parameters:
NameTypeAttributesDefaultDescription
xNumber<optional>
0

X axis location

yNumber<optional>
0

Y axis location

Example
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

Members

x

Properties
TypeDescription
Number

X axis location

y

Properties
TypeDescription
Number

Y axis location

Methods

add(v) → {Vector2}

Returns a copy of this vector plus the vector passed in

Parameters:
NameTypeDescription
vVector2

other vector

Returns:
Type: 
Vector2

angle() → {Number}

Returns the angle of this vector, up is angle 0

Returns:
Type: 
Number

area() → {Number}

Returns the area this vector covers as a rectangle

Returns:
Type: 
Number

arrayCheck(arraySize) → {Boolean}

Returns true if this vector is within the bounds of an array size passed in

Parameters:
NameTypeDescription
arraySizeVector2
Returns:
Type: 
Boolean

clampLength(lengthopt) → {Vector2}

Returns a new vector clamped to length passed in

Parameters:
NameTypeAttributesDefaultDescription
lengthNumber<optional>
1
Returns:
Type: 
Vector2

copy() → {Vector2}

Returns a new vector that is a copy of this

Returns:
Type: 
Vector2

cross(v) → {Number}

Returns the cross product of this and the vector passed in

Parameters:
NameTypeDescription
vVector2

other vector

Returns:
Type: 
Number

direction() → {Number}

Returns the integer direction of this vector, corrosponding to multiples of 90 degree rotation (0-3)

Returns:
Type: 
Number

distance(v) → {Number}

Returns the distance from this vector to vector passed in

Parameters:
NameTypeDescription
vVector2

other vector

Returns:
Type: 
Number

distanceSquared(v) → {Number}

Returns the distance squared from this vector to vector passed in

Parameters:
NameTypeDescription
vVector2

other vector

Returns:
Type: 
Number

divide(v) → {Vector2}

Returns a copy of this vector divided by the vector passed in

Parameters:
NameTypeDescription
vVector2

other vector

Returns:
Type: 
Vector2

dot(v) → {Number}

Returns the dot product of this and the vector passed in

Parameters:
NameTypeDescription
vVector2

other vector

Returns:
Type: 
Number

floor() → {Vector2}

Returns a copy of this vector with each axis floored

Returns:
Type: 
Vector2

invert() → {Vector2}

Returns a copy of this vector that has been inverted

Returns:
Type: 
Vector2

isValid() → {Boolean}

Checks if this is a valid vector

Returns:
Type: 
Boolean

length() → {Number}

Returns the length of this vector

Returns:
Type: 
Number

lengthSquared() → {Number}

Returns the length of this vector squared

Returns:
Type: 
Number

lerp(v, percent) → {Vector2}

Returns a new vector that is p percent between this and the vector passed in

Parameters:
NameTypeDescription
vVector2

other vector

percentNumber
Returns:
Type: 
Vector2

multiply(v) → {Vector2}

Returns a copy of this vector times the vector passed in

Parameters:
NameTypeDescription
vVector2

other vector

Returns:
Type: 
Vector2

normalize(lengthopt) → {Vector2}

Returns a new vector in same direction as this one with the length passed in

Parameters:
NameTypeAttributesDefaultDescription
lengthNumber<optional>
1
Returns:
Type: 
Vector2

rotate(angle) → {Vector2}

Returns copy of this vector rotated by the angle passed in

Parameters:
NameTypeDescription
angleNumber
Returns:
Type: 
Vector2

scale(s) → {Vector2}

Returns a copy of this vector scaled by the vector passed in

Parameters:
NameTypeDescription
sNumber

scale

Returns:
Type: 
Vector2

set(xopt, yopt) → {Vector2}

Sets values of this vector and returns self

Parameters:
NameTypeAttributesDefaultDescription
xNumber<optional>
0

X axis location

yNumber<optional>
0

Y axis location

Returns:
Type: 
Vector2

setAngle(angleopt, lengthopt) → {Vector2}

Sets this vector with angle and length passed in

Parameters:
NameTypeAttributesDefaultDescription
angleNumber<optional>
0
lengthNumber<optional>
1
Returns:
Type: 
Vector2

setDirection(directionopt, lengthopt)

Set the integer direction of this vector, corrosponding to multiples of 90 degree rotation (0-3)

Parameters:
NameTypeAttributesDefaultDescription
directionNumber<optional>
lengthNumber<optional>
1

subtract(v) → {Vector2}

Returns a copy of this vector minus the vector passed in

Parameters:
NameTypeDescription
vVector2

other vector

Returns:
Type: 
Vector2

toString(digits) → {String}

Returns this vector expressed as a string

Parameters:
NameTypeDefaultDescription
digitsNumber3

precision to display

Returns:
Type: 
String