Skip to content

Class: Vector2 ​

Lightweight mutable 2D vector. Math methods return this for chaining and mutate in place unless they're explicitly named with *Of (which create a new vector from inputs) or have a separate static counterpart.

For per-frame hot paths, prefer reusing one Vector2 (v.Set(x, y).Add(...)) over allocating fresh ones — the GC pressure of small object churn dominates real workloads.

Constructors ​

Constructor ​

ts
new Vector2(x?, y?): Vector2;

Parameters ​

ParameterTypeDefault value
xnumber0
ynumber0

Returns ​

Vector2

Properties ​

PropertyModifierType
xpublicnumber
ypublicnumber

Methods ​

Add() ​

ts
Add(v): this;

Parameters ​

ParameterType
vVector2

Returns ​

this


AddOf() ​

ts
static AddOf(a, b): Vector2;

Parameters ​

ParameterType
aVector2
bVector2

Returns ​

Vector2


AngleOf() ​

ts
AngleOf(): number;

Signed angle of this vector in radians, atan2 of (y, x). 0 = +X axis.

Returns ​

number


AngleTo() ​

ts
AngleTo(v): number;

Signed shortest angle from this → v, in [-PI, PI]. Sign matches CCW convention.

Parameters ​

ParameterType
vVector2

Returns ​

number


ClampLength() ​

ts
ClampLength(max): this;

Clamp this vector's length to max (in place). No-op if already shorter.

Parameters ​

ParameterType
maxnumber

Returns ​

this


Clone() ​

ts
Clone(): Vector2;

Returns ​

Vector2


Copy() ​

ts
Copy(v): this;

Parameters ​

ParameterType
vVector2

Returns ​

this


Cross() ​

ts
Cross(v): number;

2D pseudo-cross — scalar z-component of the 3D cross product.

Parameters ​

ParameterType
vVector2

Returns ​

number


DistanceTo() ​

ts
DistanceTo(v): number;

Parameters ​

ParameterType
vVector2

Returns ​

number


DistanceToSq() ​

ts
DistanceToSq(v): number;

Parameters ​

ParameterType
vVector2

Returns ​

number


Dot() ​

ts
Dot(v): number;

Parameters ​

ParameterType
vVector2

Returns ​

number


Equals() ​

ts
Equals(v, epsilon?): boolean;

Parameters ​

ParameterTypeDefault value
vVector2undefined
epsilonnumber0

Returns ​

boolean


FromAngle() ​

ts
static FromAngle(radians, length?): Vector2;

Build a unit vector pointing at radians. Allocates — prefer SetFromAngle in hot loops.

Parameters ​

ParameterTypeDefault value
radiansnumberundefined
lengthnumber1

Returns ​

Vector2


Length() ​

ts
Length(): number;

Returns ​

number


LengthSq() ​

ts
LengthSq(): number;

Returns ​

number


Lerp() ​

ts
Lerp(v, t): this;

Linear interpolation toward v by t in [0, 1].

Parameters ​

ParameterType
vVector2
tnumber

Returns ​

this


LerpOf() ​

ts
static LerpOf(
   a, 
   b, 
   t
): Vector2;

Parameters ​

ParameterType
aVector2
bVector2
tnumber

Returns ​

Vector2


Negate() ​

ts
Negate(): this;

Returns ​

this


Normalize() ​

ts
Normalize(): this;

Returns ​

this


One() ​

ts
static One(): Vector2;

Returns ​

Vector2


ts
static Right(): Vector2;

Returns ​

Vector2


Rotate() ​

ts
Rotate(radians): this;

Rotate this vector by radians around the origin (in place).

Parameters ​

ParameterType
radiansnumber

Returns ​

this


Scale() ​

ts
Scale(s): this;

Parameters ​

ParameterType
snumber

Returns ​

this


Set() ​

ts
Set(x, y): this;

Parameters ​

ParameterType
xnumber
ynumber

Returns ​

this


SetFromAngle() ​

ts
SetFromAngle(radians, length?): this;

Set this vector to (cos a, sin a) * length. Length defaults to 1 (unit direction).

Parameters ​

ParameterTypeDefault value
radiansnumberundefined
lengthnumber1

Returns ​

this


Sub() ​

ts
Sub(v): this;

Parameters ​

ParameterType
vVector2

Returns ​

this


SubOf() ​

ts
static SubOf(a, b): Vector2;

Returns a new vector (a - b). Allocates — prefer in-place a.Sub(b) in hot loops.

Parameters ​

ParameterType
aVector2
bVector2

Returns ​

Vector2


Up() ​

ts
static Up(): Vector2;

Returns ​

Vector2


Zero() ​

ts
static Zero(): Vector2;

Returns ​

Vector2

© 2026 PlayAdGentic