Skip to main content

Vector2

Defined in: src/lib/math.ts:488

A 2D vector.

Extends​

  • BaseVector<Vector2>

Constructors​

Constructor​

protected new Vector2(_array: Float32Array): Vector2;

Defined in: src/lib/math.ts:50

Parameters​

ParameterType
_arrayFloat32Array

Returns​

Vector2

Inherited from​

BaseVector\<Vector2\>.constructor

Properties​

PropertyModifierTypeDefault valueDescriptionInherited fromDefined in
_arrayprotectedFloat32Arrayundefined-BaseVector._arraysrc/lib/math.ts:50
_immutableprotectedbooleanfalse-BaseVector._immutablesrc/lib/math.ts:48
ONEreadonlyReadonly<Vector2>undefinedA vector with all components set to 1. This is a truly immutable constant - any mutation attempt will throw an ImmutableVectorException. Clone before modifying.-src/lib/math.ts:501
ZEROreadonlyReadonly<Vector2>undefinedA vector with all components set to 0. This is a truly immutable constant - any mutation attempt will throw an ImmutableVectorException. Clone before modifying.-src/lib/math.ts:494

Accessors​

immutable​

Get Signature​

get immutable(): boolean;

Defined in: src/lib/math.ts:55

Whether this vector is immutable.

Returns​

boolean

Inherited from​

BaseVector.immutable

length​

Get Signature​

get length(): number;

Defined in: src/lib/math.ts:86

Returns​

number

Inherited from​

BaseVector.length

x​

Get Signature​

get x(): number;

Defined in: src/lib/math.ts:518

The x coordinate.

Returns​

number

Set Signature​

set x(value: number): void;

Defined in: src/lib/math.ts:524

Throws​

if this vector is immutable.

Parameters​
ParameterType
valuenumber
Returns​

void


y​

Get Signature​

get y(): number;

Defined in: src/lib/math.ts:532

The y coordinate.

Returns​

number

Set Signature​

set y(value: number): void;

Defined in: src/lib/math.ts:538

Throws​

if this vector is immutable.

Parameters​
ParameterType
valuenumber
Returns​

void

Methods​

_createInstance()​

protected _createInstance(array: Float32Array\<ArrayBufferLike\> | number[]): Vector2;

Defined in: src/lib/math.ts:564

Parameters​

ParameterType
arrayFloat32Array<ArrayBufferLike> | number[]

Returns​

Vector2

Inherit Doc​

Overrides​

BaseVector._createInstance

_throwIfImmutable()​

protected _throwIfImmutable(operation: string): void;

Defined in: src/lib/math.ts:66

Throws an error if this vector is immutable.

Parameters​

ParameterTypeDescription
operationstringDescription of the attempted operation for the error message.

Returns​

void

Throws​

if this vector is immutable.

Inherited from​

BaseVector._throwIfImmutable

[iterator]()​

iterator: Generator<number, void, unknown>;

Defined in: src/lib/math.ts:354

Make this vector iterable for destructuring.

Returns​

Generator<number, void, unknown>

Inherited from​

BaseVector.[iterator]

add()​

add(other: Vector2): Vector2;

Defined in: src/lib/math.ts:136

Add another vector to this vector.

Parameters​

ParameterTypeDescription
otherVector2The vector to add to this vector.

Returns​

Vector2

A new vector with the result of the addition.

Throws​

if the index is out of bounds.

Inherited from​

BaseVector.add

angle()​

angle(): number;

Defined in: src/lib/math.ts:557

Calculate the angle of this vector.

Returns​

number

The angle of this vector.


at()​

at(index: number): number;

Defined in: src/lib/math.ts:98

Get the value at the given index.

Parameters​

ParameterTypeDescription
indexnumberThe index to get the value at.

Returns​

number

The value at the given index.

Throws​

if the index is out of bounds.

Inherited from​

BaseVector.at

clone()​

clone(): Vector2;

Defined in: src/lib/math.ts:317

Clone this vector.

Returns​

Vector2

A new vector with the same values.

Inherited from​

BaseVector.clone

distance()​

distance(other: Vector2): number;

Defined in: src/lib/math.ts:308

Calculate the distance between this vector and another vector.

Parameters​

ParameterTypeDescription
otherVector2The vector to calculate the distance to.

Returns​

number

The distance between this vector and the other vector.

Throws​

if the index is out of bounds.

Inherited from​

BaseVector.distance

div()​

div(other: Vector2): Vector2;

Defined in: src/lib/math.ts:203

Divide this vector by another vector.

Parameters​

ParameterTypeDescription
otherVector2The vector to divide this vector by.

Returns​

Vector2

A new vector with the result of the division.

Throws​

if the index is out of bounds.

Throws​

if the divisor is zero.

Inherited from​

BaseVector.div

dot()​

dot(other: Vector2): number;

Defined in: src/lib/math.ts:230

Calculate the dot product of this vector and another vector.

Parameters​

ParameterTypeDescription
otherVector2The vector to calculate the dot product with.

Returns​

number

The dot product of this vector and the other vector.

Throws​

if the index is out of bounds.

Inherited from​

BaseVector.dot

equals()​

equals(other: Vector2, epsilon: number): boolean;

Defined in: src/lib/math.ts:329

Check if this vector is equal to another vector. (Approximately)

Parameters​

ParameterTypeDefault valueDescription
otherVector2undefinedThe vector to check equality with.
epsilonnumber1e-6The epsilon to use for the comparison.

Returns​

boolean

True if the vectors are equal, false otherwise.

Inherited from​

BaseVector.equals

freeze()​

freeze(): Readonly\<T\>;

Defined in: src/lib/math.ts:81

Mark this vector as immutable. Once frozen, any attempt to modify the vector will throw an ImmutableVectorException.

Note: The underlying Float32Array is not itself frozen; only this wrapper instance is. All mutation methods check the immutable flag and will throw.

Returns​

Readonly<T>

This vector, frozen and typed as Readonly<T>.

Inherited from​

BaseVector.freeze

magnitude()​

magnitude(): number;

Defined in: src/lib/math.ts:250

Calculate the magnitude of this vector.

Returns​

number

The magnitude of this vector.

Throws​

if the index is out of bounds.

Inherited from​

BaseVector.magnitude

mul()​

mul(other: Vector2): Vector2;

Defined in: src/lib/math.ts:180

Multiply this vector by another vector.

Parameters​

ParameterTypeDescription
otherVector2The vector to multiply this vector by.

Returns​

Vector2

A new vector with the result of the multiplication.

Throws​

if the index is out of bounds.

Inherited from​

BaseVector.mul

normalize()​

normalize(): Vector2;

Defined in: src/lib/math.ts:269

Normalize this vector.

Returns​

Vector2

A new vector with the result of the normalization.

Throws​

if the index is out of bounds.

Inherited from​

BaseVector.normalize

perpendicular()​

perpendicular(): Vector2;

Defined in: src/lib/math.ts:548

Calculate the perpendicular vector of this vector.

Returns​

Vector2

A new vector with the result of the perpendicular vector.


scale()​

scale(scalar: number): Vector2;

Defined in: src/lib/math.ts:287

Scale this vector by a scalar.

Parameters​

ParameterTypeDescription
scalarnumberThe scalar to scale this vector by.

Returns​

Vector2

A new vector with the result of the scaling.

Throws​

if the index is out of bounds.

Inherited from​

BaseVector.scale

setAt()​

setAt(index: number, value: number): void;

Defined in: src/lib/math.ts:116

Set the value at the given index.

Parameters​

ParameterTypeDescription
indexnumberThe index to set the value at.
valuenumberThe value to set at the given index.

Returns​

void

Throws​

if the index is out of bounds.

Throws​

if this vector is immutable.

Inherited from​

BaseVector.setAt

sub()​

sub(other: Vector2): Vector2;

Defined in: src/lib/math.ts:158

Subtract another vector from this vector.

Parameters​

ParameterTypeDescription
otherVector2The vector to subtract from this vector.

Returns​

Vector2

A new vector with the result of the subtraction.

Throws​

if the index is out of bounds.

Inherited from​

BaseVector.sub

create()​

static create(x: number, y: number): Vector2;

Defined in: src/lib/math.ts:511

Create a vector from individual coordinates.

Parameters​

ParameterTypeDescription
xnumberThe x coordinate.
ynumberThe y coordinate.

Returns​

Vector2

A new vector.