dot big bang

Developer API
Menu

Class Vector2

All
  • Public
  • Public/Protected
  • All

Hierarchy

  • Vector2

Index

Constructors

constructor

  • new Vector2(x?: number, y?: number): Vector2
  • Creates a new Vector3

    Parameters

    • Optional x: number

      The x component of the vector. Default is 0.

    • Optional y: number

      The y component of the vector. Default is 0.

    Returns Vector2

Properties

x

x: number

The x component of the vector.

y

y: number

The y component of the vector.

Accessors

height

  • get height(): number
  • set height(value: number): void
  • Alias for y.

    Returns number

  • Alias for y.

    Parameters

    • value: number

    Returns void

width

  • get width(): number
  • set width(value: number): void
  • Alias for x.

    Returns number

  • Alias for x.

    Parameters

    • value: number

    Returns void

Methods

add

  • Adds the vector v to this vector.

    Parameters

    • v: Vector2

      The vector to add to this one.

    Returns Vector2

addScalar

  • Adds the scalar value s to the x, y and z components of this vector.

    Parameters

    • s: number

      The scalar value to add.

    Returns Vector2

addScaledVector

  • Adds the vector v multiplied by the scalar s to this vector.

    Parameters

    • v: Vector2

      The vector to scale by s and add to this vector.

    • s: number

      The scale factor to apply to v before adding it to this vector.

    Returns Vector2

addVectors

  • Sets this vector to a + b.

    Parameters

    • a: Vector2

      The vector to add to b before setting the result to this vector.

    • b: Vector2

      The vector to add to a before setting the result to this vector.

    Returns Vector2

angle

  • angle(): number
  • Computes the angle in radians of this vector with respect to the positive x-axis.

    Returns number

ceil

  • Rounds this vector's components up to the nearest integer value.

    Returns Vector2

clamp

  • If this vector's x or y value is greater than the max vector's x or y value, it is replaced by the corresponding value. If this vector's x or y value is less than the min vector's x or y value, it is replaced by the corresponding value.

    Parameters

    • min: Vector2

      The vector with the minimum x, y and z values used for clamping this vector.

    • max: Vector2

      The vector with the maximum x, y and z values used for clamping this vector.

    Returns Vector2

clampLength

  • clampLength(min: number, max: number): Vector2
  • If this vector's length is greater than the max value, the vector will be scaled down so its length is the max value. If this vector's length is less than the min value, the vector will be scaled up so its length is the min value.

    Parameters

    • min: number

      The minimum value the length will be clamped to.

    • max: number

      The maximum value the length will be clamped to.

    Returns Vector2

clampScalar

  • clampScalar(minVal: number, maxVal: number): Vector2
  • If this vector's x or y values are greater than the max value, they are replaced by the maxVal value. If this vector's x or y values are less than the min value, they are replaced by the minVal value.

    Parameters

    • minVal: number

      The minimum value used for clamping this vector.

    • maxVal: number

      The maximum value used for clamping this vector.

    Returns Vector2

clone

  • Returns a new vector with the same x and y values as this one.

    Returns Vector2

copy

  • Copies the provided vector v into this one.

    Parameters

    Returns Vector2

distanceTo

  • Calculates the distance from this vector to v.

    Parameters

    • v: Vector2

      The second vector to calculate the distance to (the first one being this vector).

    Returns number

distanceToSquared

  • distanceToSquared(v: Vector2): number
  • Calculates the squared distance from this vector to v. If you are just comparing the distance with another distance, you should compare the distance squared instead as it is slightly more efficient to calculate.

    Parameters

    • v: Vector2

      The second to calculate the squared distance to (the first one being this vector).

    Returns number

divide

  • Divides this vector by the other vector v. Each component of this vector is divided by the corresponding one of v.

    Parameters

    • v: Vector2

      The vector to divide this one with.

    Returns Vector2

divideScalar

  • Divides this vector by the scalar s. Each component of this vector is divided by the same value s. Set this vector to (0, 0) if s == 0.

    Parameters

    • s: number

      The value to divide each component of this vector with.

    Returns Vector2

dot

  • Calculates the dot product of this vector and v.

    Parameters

    • v: Vector2

      The other vector to calculate the dot product with.

    Returns number

equals

  • Checks for strict equality of this vector and v.

    Parameters

    • v: Vector2

      The other vector to compare this one to.

    Returns boolean

floor

  • Rounds this vector's components down to the nearest integer value.

    Returns Vector2

fromArray

  • fromArray(array: number[], offset?: number): Vector2
  • Sets this vector's x value to be array[offset+0] amd y value to be array[offset+1].

    Parameters

    • array: number[]

      The array of format [x, y] used to construct this vector.

    • Optional offset: number

      An offset into the array from where to start reading the components.

    Returns Vector2

getComponent

  • getComponent(index: number): number
  • Gets the value of the index-th component of the vector. Index 0 corresponds to the x component and 1 is y.

    Parameters

    • index: number

      The index (0 or 1) of the component to get.

    Returns number

length

  • length(): number
  • Computes the Euclidean length (straight-line length) from (0, 0, 0) to this vector's (x, y). If you are comparing the lengths of vectors, you should compare the length squared instead as it is slightly more efficient to calculate.

    Returns number

lengthManhattan

  • lengthManhattan(): number

lengthSq

  • lengthSq(): number
  • Computes the square of the Euclidean length (straight-line length) from (0, 0) to this vector's (x, y). If you are comparing the lengths of vectors, you should compare the length squared instead as it is slightly more efficient to calculate.

    Returns number

lerp

  • Linearly interpolates between this vector and v by alpha amount (alpha=0 returns this vector, while alpha=1 returns v) and stores the result into this vector.

    Parameters

    • v: Vector2

      The vector to interpolate this one towards.

    • alpha: number

      The interpolation factor, typically in the closed interval [0, 1].

    Returns Vector2

lerpVectors

  • Linearly interpolates between v1 and v2 by alpha amount (alpha=0 returns v1, while alpha=1 returns v2) and stores the result into this vector.

    Parameters

    • v1: Vector2
    • v2: Vector2
    • alpha: number

      The interpolation factor, typically in the closed interval [0, 1].

    Returns Vector2

max

  • If this vector's x or y value is less than v's x or y value, replaces that value with the corresponding max value.

    Parameters

    Returns Vector2

min

  • If this vector's x or y value is greater than v's x or y value, replaces that value with the corresponding min value.

    Parameters

    • v: Vector2

      The vector with the values to compare to.

    Returns Vector2

multiply

  • Multiplies this vector by the vector w. Each component of this vector is multiplied by the corresponding one of w.

    Parameters

    • w: Vector2

      The vector to multiply to this one.

    Returns Vector2

multiplyScalar

  • multiplyScalar(s: number): Vector2
  • Multiplies this vector by the scalar s. Each component of this vector is multiplied by the same value s.

    Parameters

    • s: number

      The value to multiply each component of this vector by.

    Returns Vector2

negate

  • Inverts this vector, i.e. sets x=-x and y=-y.

    Returns Vector2

normalize

  • Converts this vector to a unit vector, that is, sets it equal to a vector with the same direction as this one, but with a length of 1.

    Returns Vector2

rotateAround

  • Rotates this vector around center by angle radians.

    Parameters

    • center: Vector2

      The point around which to rotate.

    • angle: number

      The angle to rotate, in radians.

    Returns Vector2

round

  • Rounds this vector's components to the nearest integer value.

    Returns Vector2

roundToZero

  • Rounds this vector's components towards zero (up if negative, down if positive) to an integer value.

    Returns Vector2

set

  • set(x: number, y: number): Vector2
  • Sets the x and y components of this vector to the provided ones.

    Parameters

    • x: number

      The x component of the vector.

    • y: number

      The y component of the vector.

    Returns Vector2

setComponent

  • setComponent(index: number, value: number): void
  • Sets the index-th component of this vector to the provided value. Index 0 corresponds to the x component and 1 is y.

    Parameters

    • index: number

      The index (0 or 1) of the component to set.

    • value: number

      The value to set the component of this vector to.

    Returns void

setLength

  • Set this vector to a vector with the same direction as this one, but a length of l.

    Parameters

    • l: number

      The length to set this vector to.

    Returns Vector2

setScalar

  • setScalar(scalar: number): Vector2
  • Sets the x and y components of this vector to the same scalar value.

    Parameters

    • scalar: number

      The value to set all the components of this vector to.

    Returns Vector2

setX

  • Sets this vector's x component to the provided value.

    Parameters

    • x: number

      The value to set the x component to.

    Returns Vector2

setY

  • Sets this vector's y component to the provided value.

    Parameters

    • y: number

      The value to set the y component to.

    Returns Vector2

sub

  • Subtracts the vector v from this vector.

    Parameters

    • v: Vector2

      The vector to subtract from this vector.

    Returns Vector2

subScalar

  • Subtracts the scalar s from this vector's x, y and z components.

    Parameters

    • s: number

      The value to subtract to the x and y components of this vector.

    Returns Vector2

subVectors

  • Sets this vector to a - b.

    Parameters

    • a: Vector2

      The vector from which b is subtracted before setting the result to this vector.

    • b: Vector2

      The vector to subtract from a before setting the result to this vector.

    Returns Vector2

toArray

  • toArray(array?: number[], offset?: number): number[]
  • Returns the components of this vector in an array of format [x, y].

    Parameters

    • Optional array: number[]

      The array to store the compoments of the vector in. If not specified, a new array will be created.

    • Optional offset: number

      An offset into the array where to write the elements of this vector.

    Returns number[]