Skip to content

Vector2

The Vector2 class represents a 2D vector and related vector math helpers.


Properties

xnumber

The x component of the vector.


ynumber

The y component of the vector.


Methods

constructor(x?: number, y?: number)

Creates a new Vector2 instance.

  • xnumber (optional) - The x component of the vector. Defaults to 0.
  • ynumber (optional) - The y component of the vector. Defaults to 0.

add(v: Vector2)Vector2

Adds another vector to this vector.

Returned:

  • Vector2 - A new vector that is the result of the addition.

sub(v: Vector2)Vector2

Subtracts another vector from this vector.

Returned:

  • Vector2 - A new vector that is the result of the subtraction.

mul(v: Vector2)Vector2

Multiplies this vector by another vector.

  • vVector2 - The vector to multiply by.

Returned:

  • Vector2 - A new vector that is the result of the multiplication.

div(v: Vector2)Vector2

Divides this vector by another vector.

  • vVector2 - The vector to divide by.

Returned:

  • Vector2 - A new vector that is the result of the division.

Scale(factor: number)void

Scales this vector by a factor.

  • factornumber - The factor to scale by.

Scaled(factor: number)Vector2

Scales this vector by a factor.

  • factornumber - The factor to scale by.

Returned:

  • Vector2 - A new vector that is the result of the scaling.

Length()number

Returns the length of this vector.

Returned:

  • number - The length of this vector.

LengthSqr()number

Returns the squared length of this vector.

Returned:

  • number - The squared length of this vector.

Normalize()void

Normalizes this vector.

IMPORTANT

This vector is modified.


Normalized()Vector2

Returns a normalized copy of this vector.

Returned:

  • Vector2 - A normalized copy of this vector.

Dot(v: Vector2)number

Returns the dot product of this vector and another vector.

Returned:

  • number - The dot product of this vector and another vector.

Angle(v: Vector2)number

Returns the angle between this vector and another vector.

  • vVector2 - The vector to get the angle to.

Returned:

  • number - The angle between this vector and another vector.

Distance(v: Vector2)number

Returns the distance between this vector and another vector.

  • vVector2 - The vector to get the distance to.

Returned:

  • number - The distance between this vector and another vector.

DistanceSqr(v: Vector2)number

Returns the squared distance between this vector and another vector.

  • vVector2 - The vector to get the distance to.

Returned:

  • number - The squared distance between this vector and another vector.

Clone()Vector2

Clone the vector.

Returned:


ToVector()Vector

Converts this vector to a 3D vector.

Returned:


toString()string

Returns a string representation of this vector.

Returned:

  • string - A string representation of this vector.