Vector2

Constructs a new vector with given x, y components. Code adapted from https://gist.github.com/Dalimil/3daf2a0c531d7d030deb37a7bfeff454

Constructor

new Vector2(xopt, yopt)

Parameters:
NameTypeAttributesDefaultDescription
xnumber | object<optional>
0

the x value

ynumber<optional>
x

y - the y value

Properties
NameTypeAttributesDefaultDescription
xnumber<optional>
0

The x value

ynumber<optional>
x

The y value

Examples
new Vector2(56, 78); // (56, 78)
Vector2.zero(); // (0,0)

Classes

Vector2

Methods

absolute() → {Vector2}

Change the values to absolute values

Returns:

this Vector2 updated

Type: 
Vector2
Example
new Vector2(-1, 5).absolute() // (1, 5)

add(vector) → {Vector2}

Add the values of this vector 2 with the values of the given vector2

Parameters:
NameTypeDescription
vectorVector2

the vector to add

Returns:

This Vector2

Type: 
Vector2
Example
new Vector2(1,2).add(new Vector2(10)); // (11, 12)

angle() → {number}

Calculate the angle between this Vector and the positive x-axis.

Returns:

the result

Type: 
number

clone() → {Vector2}

Return a new Vector2 with the same values of this

Returns:
Type: 
Vector2
Example
new Vector2(1, 2).clone() // (1, 2)

diferenceAngle(target) → {number}

Calculate the angle between this Vector and the given Vector.

Parameters:
NameTypeDescription
targetVector2

the vector to get the angle

Returns:
Type: 
number
Examples
new Vector2(5, 10).diferenceAngle(new Vector2(5, 10)); // 0
new Vector2(1000, 123).diferenceAngle(new Vector2(5, 10)); // -173.52080244507272
Vector2.zero().diferenceAngle(new Vector2(90, 90)); // 45

distance(vector) → {number}

Returns the distance between this vector and a given vector.

Parameters:
NameTypeDescription
vectorVector2

the vector to compare

Returns:
Type: 
number
Examples
new Vector2(5, 10).distance(); // 125
new Vector2(5, 10).distance(new Vector2(100, 20))); // 9125

distanceSqrt(vector) → {number}

Returns the squared distance between this vector and a given vector.

Parameters:
NameTypeDescription
vectorVector2

the vector to compare

Returns:
Type: 
number
Examples
new Vector2(5, 10).distanceSqrt(); // 11.180339887498949
new Vector2(5, 10).distanceSqrt(new Vector2(100, 20)); // 95.524865872714
Vector2.zero().distanceSqrt(new Vector2(100, 20)); // 101.9803902718557

divide(vector2) → {Vector2}

Divides the values of vector 2 with the values of the given vector2

Parameters:
NameTypeDescription
vector2Vector2

the vector to divide

Returns:

This Vector2

Type: 
Vector2
Example
new Vector2(10,5).divide(new Vector2(5)); // (5, 2.5)

dot(vector) → {number}

Dot product of two vectors.

Parameters:
NameTypeDescription
vectorVector2
Returns:
Type: 
number
Example
new Vector2(10,5).dot(new Vector2(5)); // 75

equals(vector) → {boolean}

Returns true if the given vector is exactly equal to this vector.

Parameters:
NameTypeDescription
vectorVector2

the vector to compare

Returns:
Type: 
boolean
Examples
new Vector2(5, 10).equals(new Vector2(5, 10)); // true
new Vector2(10, 5).equals(new Vector2(5, 10)) // false

invert() → {Vector2}

Invert the X and Y values of this Vector2

Returns:

This Vector2.

Type: 
Vector2
Example
new Vector2(-1, 5).invert() // (5, -1)

magnitude() → {number}

Returns the length of this vector.

Returns:
Type: 
number
Examples
new Vector2(0).magnitude(); // 0
new Vector2(1).magnitude(); // 2
new Vector2(2).magnitude(); // 8
new Vector2(5, 10).magnitude(); // 125

magnitudeSqr() → {number}

Returns the squared length of this vector.

Returns:
Type: 
number
Examples
new Vector2(0).magnitude(); // 0
new Vector2(1).magnitude(); // 1.4142135623730951
new Vector2(2).magnitude(); // 2.8284271247461903
new Vector2(5, 10).magnitude(); // 11.180339887498949

moveTowards(target, step) → {Vector2}

Linearly interpolates between current vector and the given vector by time.

Parameters:
NameTypeDefaultDescription
targetVector2

the vector to interpolate

stepnumber1

step, distance of the target (between 0, and 1)

Returns:

this vector changed

Type: 
Vector2
Examples
new Vector2(5,10).moveTowards(); // (0,0)
new Vector2(5,10).moveTowards(new Vector2(5, 10)); // (5,10)
new Vector2(5,10).moveTowards(new Vector2(5, 10), 0.5); // (7.5, 7.5)

multiply(vector2) → {Vector2}

Multiplies the values of vector 2 with the values of the given vector2

Parameters:
NameTypeDescription
vector2Vector2

the vector to multiply

Returns:

This Vector2

Type: 
Vector2
Example
new Vector2(1,2).multiply(new Vector2(10)); // (10, 20)

negate() → {Vector2}

Negate the x and y components of this Vector.

Returns:

this Vector2

Type: 
Vector2
Example
new Vector2(-1, 5).negate() // (1, 5)

negative() → {Vector2}

Change the values to negative values

Returns:

This Vector2.

Type: 
Vector2
Example
new Vector2(-1, 5).negative() // (-1, -5)

normalize() → {Vector2}

Returns this vector with a magnitude of 1.

Returns:

this vector normalized

Type: 
Vector2
Examples
new Vector2(5, 10).normalize()); // (0.4472135954999579, 0.8944271909999159)
new Vector2(1000, 123).normalize()); // (0.9925202644900105, 0.1220799925322713)
Vector2.zero().normalize()); // (0, 0)

rotate(radiansopt) → {Vector2}

Return a new vector rotated

Parameters:
NameTypeAttributesDefaultDescription
radiansnumber<optional>
0

the radians to rotate

Returns:
Type: 
Vector2

scale(valueopt) → {Vector2}

Multiplies the values of vector 2 with the given value.

Parameters:
NameTypeAttributesDefaultDescription
valuenumber<optional>
1

value - the value to multiply/scale

Returns:

This Vector2

Type: 
Vector2
Example
new Vector2(1,2).scale(2); // (2, 3)

set(xopt, yopt) → {Vector2}

Set x and y components of an existing Vector2.

Parameters:
NameTypeAttributesDefaultDescription
xnumber | Vector2<optional>
0

the new X value

ynumber<optional>
x

the new Y value

Returns:

This Vector2

Type: 
Vector2
Examples
new Vector2(1, 2).set(3, 4); // (3, 4)
new Vector2(1, 2).set({5.-10}); // (5, -10)
new Vector2(1, 2).set(new Vector2(-100, 55)); // (-100, 5)

subtract(vector) → {Vector2}

Subtracts the values of vector 2 with the values of the given vector2

Normally used to get the distance between two vectors.

Parameters:
NameTypeDescription
vectorVector2

the vector to subtract

Returns:

This Vector2

Type: 
Vector2
Examples
new Vector2(1,2).subtract(new Vector2(10)); // (-9, -8)
new Vector2(0).distanceVector(new Vector2(10, 5)); // (10, 5)
new Vector2(5, 10).distanceVector(new Vector2(10, 5)); // (5, -5)

toPrecision(precision) → {Vector2}

Fix the precision to the given decimal places

Parameters:
NameTypeDefaultDescription
precisionnumber1

the number of decimal places

Returns:
Type: 
Vector2
Example
new Vector2(1.234, 5.123456).toPrecision(2) // (1.23, 5.12)

toString() → {string}

Returns a formatted string for this vector.

Returns:
Type: 
string

(static) down() → {Vector2}

Shorthand for writing Vector2(0, 1).

Returns:
Type: 
Vector2
Example
Vector2.zero()

(static) left() → {Vector2}

Shorthand for writing Vector2(-1, 0).

Returns:
Type: 
Vector2
Example
Vector2.zero()

(static) negativeInfinity() → {Vector2}

Shorthand for writing Vector2(-Infinity, -Infinity).

Returns:
Type: 
Vector2
Example
Vector2.zero()

(static) one() → {Vector2}

Shorthand for writing Vector2(1, 1). * * @example Vector2.zero()

Returns:
Type: 
Vector2

(static) positiveInfinity() → {Vector2}

Shorthand for writing Vector2(Infinity, Infinity).

Returns:
Type: 
Vector2
Example
Vector2.zero()

(static) random() → {Vector2}

Creates a random vector with random normalized values

Returns:
Type: 
Vector2

(static) right() → {Vector2}

Shorthand for writing Vector2(1, 0).

Returns:
Type: 
Vector2
Example
Vector2.zero()

(static) up() → {Vector2}

Shorthand for writing Vector2(0, -1).

Returns:
Type: 
Vector2
Example
Vector2.zero()

(static) zero() → {Vector2}

Shorthand for writing Vector2(0, 0).

Returns:
Type: 
Vector2
Example
Vector2.zero()