Vector

Vector

2D Vector

Constructor

new Vector(x, y)

Source:
Parameters:
Name Type Default Description
x number 0
y number 0

Methods

(static) add(a, b) → {Vector}

Source:

Add two vectors or a vector and a number and return the resulting new vector

Parameters:
Name Type Description
a Vector
b Vector | number
Returns:
Type
Vector

(static) angleBetween()

Source:

Absolute angular measure between the two vectors Calculated using the bearing angles (signed angle of rotation measured counter-clockwise from positive x)

(static) create(x, y) → {Vector}

Source:

Create a vector

Parameters:
Name Type Description
x number
y number
Returns:
Type
Vector

(static) dist(a, b) → {number}

Source:

Get the distance between two vectors Note: use distLessThan or distGreaterThan for comparing distances for more performance

Parameters:
Name Type Description
a Vector
b Vector
Returns:
Type
number

(static) distGreaterThan(a, b, c, equal) → {boolean}

Source:

Check if the distance between two vectors is greater than the provided value

Parameters:
Name Type Default Description
a Vector
b Vector
c number
equal boolean false

also return true if the distance is exactly c

Returns:
Type
boolean

(static) distLessThan(a, b, c, equal) → {boolean}

Source:

Check if the distance between two vectors is less than the provided value

Parameters:
Name Type Default Description
a Vector
b Vector
c number
equal boolean false

also return true if the distance is exactly c

Returns:
Type
boolean

(static) divide(a, b) → {Vector}

Source:

Divide two vectors or a vector and a number and return the resulting new vector

Parameters:
Name Type Description
a Vector
b Vector | number
Returns:
Type
Vector

(static) dotAngleBetween()

Source:

Relative magnitude of the angular gap between the vectors this will always return positive values, it does not reveal the direction of rotation

(static) fi_lerp(a, b, p, dt, targetFPS) → {Vector}

Source:

return the linear interpolation between a and b at percent t

Parameters:
Name Type Description
a Vector

from vector

b Vector

to vector

p number

percentage

dt Number

delta time in ms since last frame

targetFPS Number

target (context) fps

Returns:
Type
Vector

(static) fromCardinalDirection(dir) → {Vector}

Source:

Get a vector from a cardinal direction

Parameters:
Name Type Description
dir string

for example n,e,sw,nw

Returns:
Type
Vector

(static) fromDirection(dir) → {Vector}

Source:

Get a vector from a direction

Parameters:
Name Type Description
dir string

for example up, down, left, right

Returns:
Type
Vector

(static) lerp(a, b, t) → {Vector}

Source:

return the linear interpolation between a and b at percent t

Parameters:
Name Type Description
a Vector
b Vector
t number
Returns:
Type
Vector

(static) multiply(a, b) → {Vector}

Source:

Multiply two vectors or a vector and a number and return the resulting new vector

Parameters:
Name Type Description
a Vector
b Vector | number
Returns:
Type
Vector

(static) projectionPoint(p, a, b) → {Vector}

Source:

Get the orthogonal projected Point on the line A -> B
Note: the resulting point isn't necessarily between A and B

Parameters:
Name Type Description
p Vector

the point to project onto the line

a Vector
b Vector
Returns:
Type
Vector

(static) random(min, max) → {Vector}

Source:

Get a random vector

Parameters:
Name Type Description
min number

minimum for x and y

max number

maximum for x and y

Returns:
Type
Vector

(static) subtract(a, b) → {Vector}

Source:

Subtract two vectors or a vector and a number and return the resulting new vector

Parameters:
Name Type Description
a Vector
b Vector | number
Returns:
Type
Vector

add(v) → {Vector}

Source:

Add a vector or a number

Parameters:
Name Type Description
v Vector | number
Returns:
Type
Vector

clone() → {Vector}

Source:

get a clone of this Vector

Returns:
Type
Vector

cross(v) → {number}

Source:

Get the cross product of v and the vector

Parameters:
Name Type Description
v Vector
Returns:
Type
number

divide(v) → {Vector}

Source:

Divide by a vector or a scalar value

Parameters:
Name Type Description
v Vector | number
Returns:
Type
Vector

dot(v) → {number}

Source:

Get the dot product of v and the vector

Parameters:
Name Type Description
v Vector
Returns:
Type
number

equals(v) → {boolean}

Source:

Compare coordinates

Parameters:
Name Type Description
v Vector
Returns:
Type
boolean

fixed(d) → {Vector}

Source:

cut of digits

Parameters:
Name Type Default Description
d number 3

amount of digits

Returns:
Type
Vector

length() → {number}

Source:

get the length of this vector

Returns:
Type
number

limit(len) → {Vector}

Source:

Limit the vector to the passed maximum length

Parameters:
Name Type Description
len number
Returns:
Type
Vector

max() → {number}

Source:

Get the biggest value of both coordinates

Returns:
Type
number

min() → {number}

Source:

Get the smallest value of both coordinates

Returns:
Type
number

multiply(v) → {Vector}

Source:

Multiply with a vector or a scalar value

Parameters:
Name Type Description
v Vector | number
Returns:
Type
Vector

negative() → {Vector}

Source:

Negate the vector

Returns:
Type
Vector

normalize() → {Vector}

Source:

Normalize the vector (set length to 1)

Returns:
Type
Vector

set(x, y) → {Vector}

Source:

Set x and y
If y is undefined, x is treated as a Vector and its coordinates are applied

Parameters:
Name Type Description
x number
y number
Returns:
Type
Vector

subtract(v) → {Vector}

Source:

Subtract a vector or a number

Parameters:
Name Type Description
v Vector | number
Returns:
Type
Vector

toAngles() → {number}

Source:

Radian representation of this vector
0 = a vector pointing to the right -0.5*Math.PI = a vector pointing up

Returns:
Type
number

toArray() → {Array}

Source:

get x and y as array

Returns:
Type
Array

toDirection(full) → {string}

Source:

Direction as char, u=up, d=down, l=left, r=right

Parameters:
Name Type Default Description
full boolean false

when true, its the full name i.e. "down" instead of "d"

Returns:
Type
string