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
| Parameter | Type | Default value |
|---|---|---|
x | number | 0 |
y | number | 0 |
Returns
Vector2
Properties
| Property | Modifier | Type |
|---|---|---|
x | public | number |
y | public | number |
Methods
Add()
ts
Add(v): this;Parameters
| Parameter | Type |
|---|---|
v | Vector2 |
Returns
this
AddOf()
ts
static AddOf(a, b): Vector2;Parameters
| Parameter | Type |
|---|---|
a | Vector2 |
b | Vector2 |
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
| Parameter | Type |
|---|---|
v | Vector2 |
Returns
number
ClampLength()
ts
ClampLength(max): this;Clamp this vector's length to max (in place). No-op if already shorter.
Parameters
| Parameter | Type |
|---|---|
max | number |
Returns
this
Clone()
ts
Clone(): Vector2;Returns
Vector2
Copy()
ts
Copy(v): this;Parameters
| Parameter | Type |
|---|---|
v | Vector2 |
Returns
this
Cross()
ts
Cross(v): number;2D pseudo-cross — scalar z-component of the 3D cross product.
Parameters
| Parameter | Type |
|---|---|
v | Vector2 |
Returns
number
DistanceTo()
ts
DistanceTo(v): number;Parameters
| Parameter | Type |
|---|---|
v | Vector2 |
Returns
number
DistanceToSq()
ts
DistanceToSq(v): number;Parameters
| Parameter | Type |
|---|---|
v | Vector2 |
Returns
number
Dot()
ts
Dot(v): number;Parameters
| Parameter | Type |
|---|---|
v | Vector2 |
Returns
number
Equals()
ts
Equals(v, epsilon?): boolean;Parameters
| Parameter | Type | Default value |
|---|---|---|
v | Vector2 | undefined |
epsilon | number | 0 |
Returns
boolean
FromAngle()
ts
static FromAngle(radians, length?): Vector2;Build a unit vector pointing at radians. Allocates — prefer SetFromAngle in hot loops.
Parameters
| Parameter | Type | Default value |
|---|---|---|
radians | number | undefined |
length | number | 1 |
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
| Parameter | Type |
|---|---|
v | Vector2 |
t | number |
Returns
this
LerpOf()
ts
static LerpOf(
a,
b,
t
): Vector2;Parameters
| Parameter | Type |
|---|---|
a | Vector2 |
b | Vector2 |
t | number |
Returns
Vector2
Negate()
ts
Negate(): this;Returns
this
Normalize()
ts
Normalize(): this;Returns
this
One()
ts
static One(): Vector2;Returns
Vector2
Right()
ts
static Right(): Vector2;Returns
Vector2
Rotate()
ts
Rotate(radians): this;Rotate this vector by radians around the origin (in place).
Parameters
| Parameter | Type |
|---|---|
radians | number |
Returns
this
Scale()
ts
Scale(s): this;Parameters
| Parameter | Type |
|---|---|
s | number |
Returns
this
Set()
ts
Set(x, y): this;Parameters
| Parameter | Type |
|---|---|
x | number |
y | number |
Returns
this
SetFromAngle()
ts
SetFromAngle(radians, length?): this;Set this vector to (cos a, sin a) * length. Length defaults to 1 (unit direction).
Parameters
| Parameter | Type | Default value |
|---|---|---|
radians | number | undefined |
length | number | 1 |
Returns
this
Sub()
ts
Sub(v): this;Parameters
| Parameter | Type |
|---|---|
v | Vector2 |
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
| Parameter | Type |
|---|---|
a | Vector2 |
b | Vector2 |
Returns
Vector2
Up()
ts
static Up(): Vector2;Returns
Vector2
Zero()
ts
static Zero(): Vector2;Returns
Vector2