gameify. Vector2d

A 2d vector. Usually used to represent things such as distance, direction, position, velocity, etc. You can use two numbers, a formatted string, or another Vector2d to create a new Vector2d

Constructor

new Vector2d(x, yopt)

Parameters:
NameTypeAttributesDescription
xNumber | gameify.Vector2d | String

x coordinate OR an existing vector

yNumber<optional>

y coordinate

Example
// A vector from two numbers
let vectorA = new gameify.Vector2d(5, 8);
// A vector from a formatted string
let vectorB = new gameify.Vector2d("<4, 6>");
// A vector from another vector (copying it)
let vectorC = new gameify.Vector2d(vectorA)

Members

x :Number

The x (a) point of the vector

Type:
  • Number

y :Number

The y (b) point of the vector

Type:
  • Number

(static, readonly) I

The i vector <1, 0> for reference and calculation

(static, readonly) J

The j vector <0, 1> for reference and calculation

(static, readonly) ZERO

A zero vector for reference and calculation

Example
let vectorA = new gameify.Vector2d(3, 2);
// Interpolate vectorA towards zero (using gameify.Vector2d.ZERO to avoid having to make a new vector)
let vectorB = vectorA.linearInterpolate(gameify.Vector2d.ZERO, 0.5); // vectorB = <1.5, 1>

Methods

add(vectorB) → {gameify.Vector2d}

Adds this vector and another one, and returns the result as a new vector

Parameters:
NameTypeDescription
vectorBgameify.Vector2d

The vector to add

Returns:
Type: 
gameify.Vector2d
Example
let vectorA = new gameify.Vector2d(3, 2);
let vectorB = new gameify.Vector2d(7, -3);
let vectorC = vectorA.add(vectorB); // vectorC = <10, -1>

copy() → {gameify.Vector2d}

Returns a copy of the vector

Returns:
Type: 
gameify.Vector2d

distanceTo(point) → {Number}

Calculate the distance between a this and another vector (From this vector's coordinates to the other vector's coordinates)

Parameters:
NameTypeDescription
pointgameify.Vector2d

The start of the line segment

Returns:

The distance to the vector

Type: 
Number

distanceTo(segmentStart, segmentEnd) → {Number}

Calculate the distance between a this and a line segment (From this vector's coordinates to the closest point on the line segment)

Parameters:
NameTypeDescription
segmentStartgameify.Vector2d

The start of the line segment

segmentEndgameify.Vector2d

The end of the line segment (start/end order does not matter)

Returns:

The distance between this point and the line segment

Type: 
Number

getAngle() → {Number}

Find the angle from the vector (in radians)

Returns:
Type: 
Number

getAngleDegrees() → {Number}

Find the angle from the vector (in degrees)

Returns:
Type: 
Number

getMagnitude()

Returns the length (magnitude) of the vector.

getNormalized() → {gameify.Vector2d}

Returns a normalized copy of the vector (Sets the length equal to one while maintaining the direction)

Returns:
Type: 
gameify.Vector2d

linearInterpolate(vectorB, t) → {gameify.Vector2d}

Linear interpolation from this vector to another

Parameters:
NameTypeDescription
vectorBgameify.Vector2d

The vector to interpolate to

tNumber

A number from 0 to 1, with larger values closer to vectorB

Returns:
Type: 
gameify.Vector2d
Example
let vectorA = new gameify.Vector2d(3, 2);
let vectorB = new gameify.Vector2d(7, 12);
// Get a vector half way between A and B
let vectorC = vectorA.linearInterpolate(vectorB, 0.5); // vectorC = <5, 7>

multiply(value) → {gameify.Vector2d}

Multiplies this vector by an amount, and returns the result as a new vector

Parameters:
NameTypeDescription
valueNumber

The amount to multiply by

Returns:
Type: 
gameify.Vector2d
Example
let vectorA = new gameify.Vector2d(3, 2);
let vectorB = vectorA.multiply(4); // vectorB = <12, -8>

multiplyComponents(vector) → {gameify.Vector2d}

Multiplies the components of this and another vector, and returns the result as a new vector

Parameters:
NameTypeDescription
vectorgameify.Vector2d

The vector to multiply

Returns:
Type: 
gameify.Vector2d
Example
let vectorA = new gameify.Vector2d(3, 2);
let vectorB = new gameify.Vector2d(4, -8)
let vectorC = vectorA.multiplyComponents(vectorB); // vectorC = <12, -16>

normalize()

Normalizes the vector (Sets the length equal to one while maintaining the direction)

rotated(angle) → {gameify.Vector2d}

Returns a copy of this vector rotated by an angle, in radians (counterclockwise)

Parameters:
NameTypeDescription
angleNumber

The angle to rotate by, in degrees

Returns:
Type: 
gameify.Vector2d
Example
let vectorA = new gameify.Vector2d(3, 2);
vectorA.rotated(Math.PI/2); // vectorA = <-2, 3>

rotatedAbout(angle, point) → {gameify.Vector2d}

Returns a copy of this vector, rotated around a point by an angle, in radians (counterclockwise)

Parameters:
NameTypeDescription
angleNumber

The angle to rotate by, in radians

pointgameify.Vector2d

The point to rotate around

Returns:
Type: 
gameify.Vector2d

rotatedDegrees(angle) → {gameify.Vector2d}

Returns a copy of this vector rotated by an angle, in degrees (counterclockwise)

Parameters:
NameTypeDescription
angleNumber

The angle to rotate by, in degrees

Returns:
Type: 
gameify.Vector2d
Example
let vectorA = new gameify.Vector2d(3, 2);
vectorA.rotatedDegrees(90); // vectorA = <-2, 3>

rounded() → {gameify.Vector2d}

Returns a copy of the vector with both coordinates to the nearest integer

Returns:
Type: 
gameify.Vector2d

subtract(vectorB) → {gameify.Vector2d}

Subtracts this vector and another one, and returns the result as a new vector

Parameters:
NameTypeDescription
vectorBgameify.Vector2d

The vector to subtract

Returns:
Type: 
gameify.Vector2d
Example
let vectorA = new gameify.Vector2d(3, 2);
let vectorB = new gameify.Vector2d(7, -3);
let vectorC = vectorA.add(vectorB); // vectorC = <10, -1>

toJSON()

Returns a JSON representation of the vector

toRawString()

Same as toString, but does not truncate the string.

toString() → {String}

Returns a string representing the vector in the form "<x, y>". Truncated to three decimal places.

Returns:
Type: 
String

truncated(precisionopt) → {gameify.Vector2d}

Returns a copy of this vector, width the x and y values truncated to a certain precision

Parameters:
NameTypeAttributesDefaultDescription
precisionNumber<optional>
0

Values after the decimal to keep

Returns:

The vector with truncated values

Type: 
gameify.Vector2d

valueOf()

Returns a string representation of the vector, equivalent to toString

xComponent() → {gameify.Vector2d}

Returns the x component of this vector

Returns:
Type: 
gameify.Vector2d

yComponent() → {gameify.Vector2d}

Returns the y component of this vector

Returns:
Type: 
gameify.Vector2d

(static) assertIsCompatibleVector(vector)

Checks if a vector is compatible with operations in this one, and throws if not.

Parameters:
NameTypeDescription
vectorgameify.Vector2d

(static) from(x, yopt) → {gameify.Vector2d}

Creates a vector from an x and y, string, or existing vector

Parameters:
NameTypeAttributesDescription
xNumber | gameify.Vector2d | String

x coordinate OR an existing vector

yNumber<optional>

y coordinate

Returns:
Type: 
gameify.Vector2d

(static) isCompatibleVector(vector)

Checks if a vector is compatible with operations in this one

Parameters:
NameTypeDescription
vectorgameify.Vector2d

(static) longestOf(vectorA, vectorB)

Given two vectors, returns the one with the greatest magnitude (length)

Parameters:
NameTypeDescription
vectorAgameify.Vector2d
vectorBgameify.Vector2d

(static) segmentsIntersect(startA, endA, startB, endB, toleranceopt, collinearIntersectsopt) → {Boolean}

Check if two line segments intersect

Parameters:
NameTypeAttributesDefaultDescription
startAgameify.Vector2d

The start of the first line segment

endAgameify.Vector2d

The end of the first line segment

startBgameify.Vector2d

The start of the second line segment

endBgameify.Vector2d

The end of the second line segment

toleranceNumber<optional>
1e-8

Maximum tolerance (because of floating-point errors)

collinearIntersectsBoolean<optional>
true

Whether collinear lines should count as intersecting

Returns:

True if the line segments intersect, false otherwise

Type: 
Boolean

(static) shortestOf(vectorA, vectorB)

Given two vectors, returns the one with the least magnitude (length)

Parameters:
NameTypeDescription
vectorAgameify.Vector2d
vectorBgameify.Vector2d