Skip to content

API reference / @jsprismarine/math / Vector2

Class: Vector2 ​

Defined in: Vector2.ts:4

2D Vector.

Extended by ​

Constructors ​

Constructor ​

new Vector2(x?, z?): Vector2

Defined in: Vector2.ts:19

Create a new Vector2 instance.

Parameters ​

x? ​

number = 0

The X coordinate.

z? ​

number = 0

The Z coordinate.

Returns ​

Vector2

Example ​

typescript
const vector = new Vector2(10, 20);

Properties ​

ZERO ​

readonly static ZERO: Vector2

Defined in: Vector2.ts:8

Returns a Vector2 with 0 on all axis.

Methods ​

equals() ​

equals(vector): boolean

Defined in: Vector2.ts:97

Compare an instance of Vector2 with another.

Compares coordinates and nothing else. The previous implementation compared JSON.stringify of both sides, which dragged in whatever fields a subclass had added - and for a subclass holding a reference back into the object graph, such as a position carrying its world, serialising it threw on the circular structure.

Parameters ​

vector ​

Vector2

The Vector2 to compare to.

Returns ​

boolean

true if they're equal otherwise false.


floor() ​

floor(): Vector2

Defined in: Vector2.ts:79

Returns ​

Vector2


getX() ​

getX(): number

Defined in: Vector2.ts:67

Get the x coordinate.

Returns ​

number

The x coordinate's value.


getZ() ​

getZ(): number

Defined in: Vector2.ts:75

Get the z coordinate.

Returns ​

number

The z coordinate's value.


toString() ​

toString(): string

Defined in: Vector2.ts:24

Returns ​

string


trunc() ​

trunc(): Vector2

Defined in: Vector2.ts:83

Returns ​

Vector2


withX() ​

withX(x): Vector2

Defined in: Vector2.ts:46

Returns a copy with a different X coordinate.

Parameters ​

x ​

number

The X coordinate.

Returns ​

Vector2

A new vector; this one is left alone.

Example ​

typescript
const moved = vector.withX(10);

withZ() ​

withZ(z): Vector2

Defined in: Vector2.ts:59

Returns a copy with a different Z coordinate.

Parameters ​

z ​

number

The Z coordinate.

Returns ​

Vector2

A new vector; this one is left alone.

Example ​

typescript
const moved = vector.withZ(10);

fromObject() ​

static fromObject(obj): Vector2

Defined in: Vector2.ts:33

Creates a new Vector2 instance from an object with x and z properties.

Parameters ​

obj ​

The object containing x and z properties.

x ​

number

z ​

number

Returns ​

Vector2

A new Vector2 instance.