Skip to content

API reference / @jsprismarine/math / Vector3

Class: Vector3 ​

Defined in: Vector3.ts:6

3D Vector.

Extends ​

Extended by ​

Constructors ​

Constructor ​

new Vector3(x, y, z): Vector3

Defined in: Vector3.ts:22

Create a new Vector3 instance.

Parameters ​

x ​

number

The X coordinate.

y ​

number

The Y coordinate.

z ​

number

The Z coordinate.

Returns ​

Vector3

Example ​

typescript
const vector = new Vector3(10, 20, 30);

Overrides ​

Vector2.constructor

Properties ​

ZERO ​

readonly static ZERO: Vector3

Defined in: Vector3.ts:10

Returns a Vector3 with 0 on all axis.

Overrides ​

Vector2.ZERO

Methods ​

distanceSquaredTo() ​

distanceSquaredTo(vector): number

Defined in: Vector3.ts:150

The squared distance to another vector.

Squared, and offered first, because almost every caller compares a distance against another distance - and a comparison of squares gives the same answer without the square root. Prefer this to Vector3.distanceTo wherever the number itself is not shown to anyone.

Parameters ​

vector ​

Vector3

The vector to measure to.

Returns ​

number

The squared distance.

Example ​

typescript
if (a.distanceSquaredTo(b) <= range * range) {
    // within range, with no square root taken
}

distanceTo() ​

distanceTo(vector): number

Defined in: Vector3.ts:166

The distance to another vector.

Parameters ​

vector ​

Vector3

The vector to measure to.

Returns ​

number

The distance.

Example ​

typescript
const meters = player.getPosition().distanceTo(target.getPosition());

equals() ​

equals(vector): boolean

Defined in: Vector3.ts:130

Compare an instance of Vector3 with another.

The parameter type matches the base declaration because ahead-of-time compilation only lowers overrides with identical signatures; a plain Vector2 is never equal to a Vector3.

Parameters ​

vector ​

Vector2

The vector to compare to.

Returns ​

boolean

true if they're equal otherwise false.

Overrides ​

Vector2.equals


floor() ​

floor(): this

Defined in: Vector3.ts:109

Returns a new Vector3 with each component rounded down to the nearest integer.

Returns ​

this

A new vector with rounded down components.

Overrides ​

Vector2.floor


getX() ​

getX(): number

Defined in: Vector2.ts:67

Get the x coordinate.

Returns ​

number

The x coordinate's value.

Inherited from ​

Vector2.getX


getY() ​

getY(): number

Defined in: Vector3.ts:101

Get the y coordinate.

Returns ​

number

The y coordinate's value.


getZ() ​

getZ(): number

Defined in: Vector2.ts:75

Get the z coordinate.

Returns ​

number

The z coordinate's value.

Inherited from ​

Vector2.getZ


toString() ​

toString(): string

Defined in: Vector3.ts:30

Returns ​

string

Overrides ​

Vector2.toString


trunc() ​

trunc(): this

Defined in: Vector3.ts:117

Returns a new Vector3 with each component truncated to the nearest integer.

Returns ​

this

A new vector with truncated axis.

Overrides ​

Vector2.trunc


withX() ​

withX(x): this

Defined in: Vector3.ts:67

Returns a copy with a different X coordinate.

Parameters ​

x ​

number

The X coordinate.

Returns ​

this

A new vector; this one is left alone.

Example ​

typescript
const moved = vector.withX(10);

Overrides ​

Vector2.withX


withY() ​

withY(y): this

Defined in: Vector3.ts:80

Returns a copy with a different Y coordinate.

Parameters ​

y ​

number

The Y coordinate.

Returns ​

this

A new vector; this one is left alone.

Example ​

typescript
const moved = vector.withY(10);

withZ() ​

withZ(z): this

Defined in: Vector3.ts:93

Returns a copy with a different Z coordinate.

Parameters ​

z ​

number

The Z coordinate.

Returns ​

this

A new vector; this one is left alone.

Example ​

typescript
const moved = vector.withZ(10);

Overrides ​

Vector2.withZ


fromObject() ​

static fromObject(obj): Vector3

Defined in: Vector3.ts:39

Creates a new Vector3 instance from an object with x, y, and z properties.

Parameters ​

obj ​

The object containing x, y, and z properties.

x ​

number

y ​

number

z ​

number

Returns ​

Vector3

A new Vector3 instance.

Overrides ​

Vector2.fromObject