# Pos3D

`com.jsmacrosce.jsmacros.api.math.Pos3D`

**Since:** 1.2.6

A mutable position represented by three `double` coordinates. Arithmetic methods return
new positions without changing the receiver. Inherited two-coordinate arithmetic methods return
[Pos2D](Pos2D.md) and omit z; use the three-coordinate overloads to retain all coordinates. The
[toVector(Pos2D)](#com.jsmacrosce.jsmacros.api.math.Pos3D_toVector-Pos2D-) and [toReverseVector(Pos2D)](#com.jsmacrosce.jsmacros.api.math.Pos3D_toReverseVector-Pos2D-) overloads treat the 2D argument's z
coordinate as `0`.  
```js
// Player.getPlayer() is null outside a world, so there is nothing to measure there
const player = Player.getPlayer();  
if (player !== null) {
  const feet = player.getPos();  
  const eyes = player.getEyePos();  
  // one block east, one block up and two south, along the world axes rather than
  // in front of wherever the player happens to be looking
  const target = feet.add(1, 1, 2);  
  // the length of the segment from the eyes down to the feet, in blocks
  const eyeHeight = eyes.toVector(feet).getMagnitude();  
  Chat.log(`that lands on ${target}, and the eyes sit ${eyeHeight} up`);  
}
```

## Constructors

### new Pos3D {#com.jsmacrosce.jsmacros.api.math.Pos3D_constructor-Vec3-}

```js
new Pos3D(Vec3 vec)
```

Copies the coordinates from a vanilla three-component vector.

**Parameters:**
- `vec`: the vector to copy the coordinates out of

**Since:** 1.2.6

### new Pos3D {#com.jsmacrosce.jsmacros.api.math.Pos3D_constructor-Pos3D-}

```js
new Pos3D(Pos3D pos)
```

Copies another position into a separate object.

**Parameters:**
- `pos`: the position to copy

**Since:** 2.1.0

### new Pos3D {#com.jsmacrosce.jsmacros.api.math.Pos3D_constructor-double-double-double-}

```js
new Pos3D(double x, double y, double z)
```

Creates a position with the given coordinates.

**Parameters:**
- `x`: the x coordinate
- `y`: the y coordinate
- `z`: the z coordinate

**Since:** 1.2.6

## Fields

### z {#com.jsmacrosce.jsmacros.api.math.Pos3D_z}

```js
z: double
```

The mutable z coordinate.

### ZERO {#com.jsmacrosce.jsmacros.api.math.Pos3D_ZERO}

```js
static ZERO: Pos3D
```

Mutable origin used by no-argument vector conversions; changes affect later conversions.

## Methods

### add {#com.jsmacrosce.jsmacros.api.math.Pos3D_add-Pos3D-}

```js
add(Pos3D pos): Pos3D
```

Returns a new position with the corresponding coordinates added; this position is unchanged.

**Parameters:**
- `pos`: the position to add to this one

**Returns:** a new position at `x + pos.x, y + pos.y, z + pos.z`

**Since:** 1.2.6

### add {#com.jsmacrosce.jsmacros.api.math.Pos3D_add-double-double-double-}

```js
add(double x, double y, double z): Pos3D
```

Returns a new position with the given amounts added to its coordinates; this position is unchanged.

**Parameters:**
- `x`: the amount to add to the x coordinate
- `y`: the amount to add to the y coordinate
- `z`: the amount to add to the z coordinate

**Returns:** a new position at `this.x + x, this.y + y, this.z + z`

**Since:** 1.6.3

### add {#com.jsmacrosce.jsmacros.api.math.Pos3D_add-Pos2D-}

```js
add(Pos2D pos): Pos2D
```

Returns a new position with the corresponding coordinates added; this position is unchanged.

**Parameters:**
- `pos`: the position to add to this one

**Returns:** the coordinate-wise sum

**Since:** 1.2.6

### add {#com.jsmacrosce.jsmacros.api.math.Pos3D_add-double-double-}

```js
add(double x, double y): Pos2D
```

Returns a new position with the given amounts added to its coordinates; this position is unchanged.

**Parameters:**
- `x`: the amount to add to the x coordinate
- `y`: the amount to add to the y coordinate

**Returns:** a new position at `this.x + x, this.y + y`

**Since:** 1.6.3

### compareTo {#com.jsmacrosce.jsmacros.api.math.Pos3D_compareTo-Pos3D-}

```js
compareTo(Pos3D o): int
```

Compares positions lexicographically by x, y, then z using [Double.compare(double, double)](https://docs.oracle.com/en/java/javase/25/docs/api/java.base/java/lang/Double.html).  
This overload compares z only when the argument is typed as `Pos3D`; a `Pos2D`
argument selects the inherited comparison, which ignores z.

**Parameters:**
- `o`: the position to compare this one against

**Returns:** a negative number, zero, or a positive number as this position is before, the same
        as, or after `o`

**Since:** 1.2.6

### compareTo {#com.jsmacrosce.jsmacros.api.math.Pos3D_compareTo-Pos2D-}

```js
compareTo(Pos2D o): int
```

Compares x first, then y using [Double.compare(double, double)](https://docs.oracle.com/en/java/javase/25/docs/api/java.base/java/lang/Double.html). When a `Pos3D`
is typed as `Pos2D`, this overload compares only x and y; use the
[Pos3D.compareTo(Pos3D)](#com.jsmacrosce.jsmacros.api.math.Pos3D_compareTo-Pos3D-) overload with `Pos3D` arguments to include z.

**Parameters:**
- `o`: the position to compare this one against

**Returns:** a negative number, zero, or a positive number as this position is before, the same
        as, or after `o`

**Since:** 1.2.6

### divide {#com.jsmacrosce.jsmacros.api.math.Pos3D_divide-Pos3D-}

```js
divide(Pos3D pos): Pos3D
```

Returns a new position with each coordinate divided by the corresponding coordinate of
`pos`. Division follows Java `double` semantics, including division by zero.  

Passing a plain [Pos2D](Pos2D.md) instead selects the inherited
[divide(Pos2D)](Pos2D.md#com.jsmacrosce.jsmacros.api.math.Pos2D_divide-Pos2D-) and hands back a
`Pos2D` with the z coordinate gone.

**Parameters:**
- `pos`: the position to divide by, coordinate for coordinate

**Returns:** a new position at `x / pos.x, y / pos.y, z / pos.z`.

**Since:** 1.8.4

### divide {#com.jsmacrosce.jsmacros.api.math.Pos3D_divide-double-double-double-}

```js
divide(double x, double y, double z): Pos3D
```

Returns a new position with each coordinate divided by its corresponding divisor. Division
follows Java `double` semantics, including division by zero.

**Parameters:**
- `x`: the x coordinate to divide by
- `y`: the y coordinate to divide by
- `z`: the z coordinate to divide by

**Returns:** a new position at `this.x / x, this.y / y, this.z / z`.

**Since:** 1.8.4

### divide {#com.jsmacrosce.jsmacros.api.math.Pos3D_divide-Pos2D-}

```js
divide(Pos2D pos): Pos2D
```

Returns a new position with each coordinate divided by the corresponding coordinate of `pos`; this position is unchanged. Division by zero follows Java floating-point rules.

**Parameters:**
- `pos`: the position to divide by, coordinate for coordinate

**Returns:** a new position at `x / pos.x, y / pos.y`.

**Since:** 1.8.4

### divide {#com.jsmacrosce.jsmacros.api.math.Pos3D_divide-double-double-}

```js
divide(double x, double y): Pos2D
```

Returns a new position with its coordinates divided by the respective divisors; this position is unchanged. Division by zero follows Java floating-point rules.

**Parameters:**
- `x`: the x coordinate to divide by
- `y`: the y coordinate to divide by

**Returns:** a new position at `this.x / x, this.y / y`.

**Since:** 1.8.4

### equals {#com.jsmacrosce.jsmacros.api.math.Pos3D_equals-Object-}

```js
equals(Object o): boolean
```

Compares this position with another object using exact runtime-class and coordinate equality.  
Each coordinate is compared with [Double.compare(double, double)](https://docs.oracle.com/en/java/javase/25/docs/api/java.base/java/lang/Double.html): NaN values compare equal,  
while positive and negative zero compare differently.

**Parameters:**
- `o`: the object to compare with this position

**Returns:** `true` if `o` is this object or has the same runtime class and equal coordinates; `false` otherwise

### getZ {#com.jsmacrosce.jsmacros.api.math.Pos3D_getZ-}

```js
getZ(): double
```

Returns the current z coordinate.

**Returns:** the z coordinate

**Since:** 1.2.6

### hashCode {#com.jsmacrosce.jsmacros.api.math.Pos3D_hashCode-}

```js
hashCode(): int
```

Computes a hash from the current x, y, and z coordinates. Because this position is mutable,  
its hash can change when its coordinates change; avoid using it as a hash key while mutating it.

**Returns:** the hash code for the current coordinates

### multiply {#com.jsmacrosce.jsmacros.api.math.Pos3D_multiply-Pos3D-}

```js
multiply(Pos3D pos): Pos3D
```

Returns a new position with each coordinate multiplied by the corresponding coordinate of
`pos`.  

Passing a plain [Pos2D](Pos2D.md) instead selects the inherited
[multiply(Pos2D)](Pos2D.md#com.jsmacrosce.jsmacros.api.math.Pos2D_multiply-Pos2D-) and hands
back a `Pos2D` with the z coordinate gone.

**Parameters:**
- `pos`: the position to multiply by, coordinate for coordinate

**Returns:** a new position at `x * pos.x, y * pos.y, z * pos.z`

**Since:** 1.2.6

### multiply {#com.jsmacrosce.jsmacros.api.math.Pos3D_multiply-double-double-double-}

```js
multiply(double x, double y, double z): Pos3D
```

Returns a new position with each coordinate multiplied by its corresponding factor.

**Parameters:**
- `x`: the factor for the x coordinate
- `y`: the factor for the y coordinate
- `z`: the factor for the z coordinate

**Returns:** a new position at `this.x * x, this.y * y, this.z * z`

**Since:** 1.6.3

### multiply {#com.jsmacrosce.jsmacros.api.math.Pos3D_multiply-Pos2D-}

```js
multiply(Pos2D pos): Pos2D
```

Returns a new position with each coordinate multiplied by the corresponding coordinate of `pos`; this position is unchanged.

**Parameters:**
- `pos`: the position to multiply by, coordinate for coordinate

**Returns:** a new position at `x * pos.x, y * pos.y`

**Since:** 1.2.6

### multiply {#com.jsmacrosce.jsmacros.api.math.Pos3D_multiply-double-double-}

```js
multiply(double x, double y): Pos2D
```

Returns a new position with its x and y coordinates multiplied by the respective factors; this position is unchanged. For one factor on both coordinates, use [scale(double)](Pos2D.md#com.jsmacrosce.jsmacros.api.math.Pos2D_scale-double-).

**Parameters:**
- `x`: the factor for the x coordinate
- `y`: the factor for the y coordinate

**Returns:** a new position at `this.x * x, this.y * y`

**Since:** 1.6.3

### scale {#com.jsmacrosce.jsmacros.api.math.Pos3D_scale-double-}

```js
scale(double scale): Pos3D
```

Returns a new position with all three coordinates multiplied by the same factor.

**Parameters:**
- `scale`: the factor to multiply all three coordinates by

**Returns:** a new position at `this.x * scale, this.y * scale, this.z * scale`

**Since:** 1.6.3

### sub {#com.jsmacrosce.jsmacros.api.math.Pos3D_sub-Pos3D-}

```js
sub(Pos3D pos): Pos3D
```

Returns a new position with the corresponding coordinates of `pos` subtracted; this position is unchanged.

**Parameters:**
- `pos`: the position to subtract

**Returns:** a new position at `x - pos.x, y - pos.y, z - pos.z`.

**Since:** 1.8.4

### sub {#com.jsmacrosce.jsmacros.api.math.Pos3D_sub-double-double-double-}

```js
sub(double x, double y, double z): Pos3D
```

Returns a new position with the given amounts subtracted coordinate by coordinate.

**Parameters:**
- `x`: the x coordinate to subtract
- `y`: the y coordinate to subtract
- `z`: the z coordinate to subtract

**Returns:** a new position at `this.x - x, this.y - y, this.z - z`.

**Since:** 1.8.4

### sub {#com.jsmacrosce.jsmacros.api.math.Pos3D_sub-Pos2D-}

```js
sub(Pos2D pos): Pos2D
```

Returns a new position with the corresponding coordinates of `pos` subtracted; this position is unchanged.

**Parameters:**
- `pos`: the position to subtract

**Returns:** a new position at `x - pos.x, y - pos.y`.

**Since:** 1.8.4

### sub {#com.jsmacrosce.jsmacros.api.math.Pos3D_sub-double-double-}

```js
sub(double x, double y): Pos2D
```

Returns a new position with the given amounts subtracted from its coordinates; this position is unchanged.

**Parameters:**
- `x`: the x coordinate to subtract
- `y`: the y coordinate to subtract

**Returns:** a new position at `this.x - x, this.y - y`.

**Since:** 1.8.4

### toMojangDoubleVector {#com.jsmacrosce.jsmacros.api.math.Pos3D_toMojangDoubleVector-}

```js
toMojangDoubleVector(): Vec3
```

Returns a raw Minecraft vector with the same coordinates as this position.

**Returns:** a new raw vanilla vector at `x, y, z`

**Since:** 1.8.4

### toRawBlockPos {#com.jsmacrosce.jsmacros.api.math.Pos3D_toRawBlockPos-}

```js
toRawBlockPos(): BlockPos
```

Returns the raw block position containing this position, flooring each coordinate.

**Returns:** a new raw block position of the block containing this position

**Since:** 1.8.0

### toReverseVector {#com.jsmacrosce.jsmacros.api.math.Pos3D_toReverseVector-}

```js
toReverseVector(): Vec3D
```

Returns a new vector from this position to the origin, opposite to [toVector()](#com.jsmacrosce.jsmacros.api.math.Pos3D_toVector-).

**Returns:** a new [Vec3D](Vec3D.md) from this position to `0, 0, 0`

**Since:** 1.6.4

### toReverseVector {#com.jsmacrosce.jsmacros.api.math.Pos3D_toReverseVector-Pos2D-}

```js
toReverseVector(Pos2D end_pos): Vec3D
```

Returns a vector from this position (including its own z) to the given 2D position lifted to z `0`.

**Parameters:**
- `end_pos`: the position the vector ends at, with its z taken as `0`

**Returns:** a new [Vec3D](Vec3D.md) from this position to `end_pos.to3D()`

**Since:** 1.6.4

### toReverseVector {#com.jsmacrosce.jsmacros.api.math.Pos3D_toReverseVector-Pos3D-}

```js
toReverseVector(Pos3D end_pos): Vec3D
```

Returns a vector from this position to the given 3D position; this position is the start.

**Parameters:**
- `end_pos`: the position the vector ends at

**Returns:** a new [Vec3D](Vec3D.md) from this position to `end_pos`

**Since:** 1.6.4

### toReverseVector {#com.jsmacrosce.jsmacros.api.math.Pos3D_toReverseVector-double-double-double-}

```js
toReverseVector(double end_x, double end_y, double end_z): Vec3D
```

Returns a vector from this position to the given coordinates; this position is the start.

**Parameters:**
- `end_x`: the x coordinate the vector ends at
- `end_y`: the y coordinate the vector ends at
- `end_z`: the z coordinate the vector ends at

**Returns:** a new [Vec3D](Vec3D.md) from this position to `end_x, end_y, end_z`

**Since:** 1.6.4

### toReverseVector {#com.jsmacrosce.jsmacros.api.math.Pos3D_toReverseVector-double-double-}

```js
toReverseVector(double end_x, double end_y): Vec2D
```

Returns a vector from this position to the given coordinates, opposite to
[toVector(double, double)](Pos2D.md#com.jsmacrosce.jsmacros.api.math.Pos2D_toVector-double-double-) with the same coordinates.

**Parameters:**
- `end_x`: the x coordinate the vector ends at
- `end_y`: the y coordinate the vector ends at

**Returns:** a new [Vec2D](Vec2D.md) from this position to `end_x, end_y`

**Since:** 1.6.4

### toString {#com.jsmacrosce.jsmacros.api.math.Pos3D_toString-}

```js
toString(): String
```

Formats the coordinates in x, y, z order; finite coordinates have six fractional digits.  
The decimal separator follows the default formatting locale; this is display text, not a stable serialization.

**Returns:** the formatted coordinates

### toVector {#com.jsmacrosce.jsmacros.api.math.Pos3D_toVector-}

```js
toVector(): Vec3D
```

Returns a vector from [ZERO](#com.jsmacrosce.jsmacros.api.math.Pos3D_ZERO) to this position. The shared `ZERO` object is
mutable, so changing it affects subsequent calls.

**Returns:** a new [Vec3D](Vec3D.md) from `0, 0, 0` to this position

**Since:** 1.2.6

### toVector {#com.jsmacrosce.jsmacros.api.math.Pos3D_toVector-Pos2D-}

```js
toVector(Pos2D start_pos): Vec3D
```

Returns a vector from the given 2D position, lifted to z `0`, to this position.

**Parameters:**
- `start_pos`: the position the vector starts at, with its z taken as `0`

**Returns:** a new [Vec3D](Vec3D.md) from `start_pos.to3D()` to this position

**Since:** 1.6.4

### toVector {#com.jsmacrosce.jsmacros.api.math.Pos3D_toVector-Pos3D-}

```js
toVector(Pos3D start_pos): Vec3D
```

Returns a vector from the given position to this position.

**Parameters:**
- `start_pos`: the position the vector starts at

**Returns:** a new [Vec3D](Vec3D.md) from `start_pos` to this position

**Since:** 1.6.4

### toVector {#com.jsmacrosce.jsmacros.api.math.Pos3D_toVector-double-double-double-}

```js
toVector(double start_x, double start_y, double start_z): Vec3D
```

Returns a vector from the given coordinates to this position.

**Parameters:**
- `start_x`: the x coordinate the vector starts at
- `start_y`: the y coordinate the vector starts at
- `start_z`: the z coordinate the vector starts at

**Returns:** a new [Vec3D](Vec3D.md) from `start_x, start_y, start_z` to this position

**Since:** 1.6.4

### toVector {#com.jsmacrosce.jsmacros.api.math.Pos3D_toVector-double-double-}

```js
toVector(double start_x, double start_y): Vec2D
```

Returns a vector from the given coordinates to this position.

**Parameters:**
- `start_x`: the x coordinate the vector starts at
- `start_y`: the y coordinate the vector starts at

**Returns:** a new [Vec2D](Vec2D.md) from `start_x, start_y` to this position

**Since:** 1.6.4
