Skip to content

Pos3D ​

com.jsmacrosce.jsmacros.api.math.Pos3D

Since1.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 and omit z; use the three-coordinate overloads to retain all coordinates. The toVector(Pos2D) and 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 ​

new Pos3D(Vec3 vec)Since1.2.6#
Copies the coordinates from a vanilla three-component vector.

Parameters:

  • vec: the vector to copy the coordinates out of
new Pos3D(Pos3D pos)Since1.2.6#
Copies another position into a separate object.

Parameters:

  • pos: the position to copy
new Pos3D(double x, double y, double z)Since1.2.6#
Creates a position with the given coordinates.

Parameters:

  • x: the x coordinate
  • y: the y coordinate
  • z: the z coordinate

Fields ​

z ​

z: double#
The mutable z coordinate.

ZERO ​

static ZERO: Pos3Dread-only#
Mutable origin used by no-argument vector conversions; changes affect later conversions.

Methods ​

add ​

add(Pos3D pos): Pos3DSince1.2.6#
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
add(double x, double y, double z): Pos3DSince1.6.3#
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
add(Pos2D pos): Pos2DSince1.2.6#
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
add(double x, double y): Pos2DSince1.6.3#
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

compareTo ​

compareTo(Pos3D o): intSince1.2.6#
Compares positions lexicographically by x, y, then z using Double.compare(double, double). 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
compareTo(Pos2D o): intSince1.2.6#
Compares x first, then y using Double.compare(double, double). When a Pos3D is typed as Pos2D, this overload compares only x and y; use the 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

divide ​

divide(Pos3D pos): Pos3DSince1.8.4#
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 instead selects the inherited 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.
divide(double x, double y, double z): Pos3DSince1.8.4#
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.
divide(Pos2D pos): Pos2DSince1.8.4#
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.
divide(double x, double y): Pos2DSince1.8.4#
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.

equals ​

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): 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 ​

getZ(): doubleSince1.2.6#
Returns the current z coordinate.
Returns: the z coordinate

hashCode ​

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 ​

multiply(Pos3D pos): Pos3DSince1.2.6#
Returns a new position with each coordinate multiplied by the corresponding coordinate of pos. Passing a plain Pos2D instead selects the inherited 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
multiply(double x, double y, double z): Pos3DSince1.6.3#
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
multiply(Pos2D pos): Pos2DSince1.2.6#
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
multiply(double x, double y): Pos2DSince1.6.3#
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).

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

scale ​

scale(double scale): Pos3DSince1.6.3#
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

sub ​

sub(Pos3D pos): Pos3DSince1.8.4#
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.
sub(double x, double y, double z): Pos3DSince1.8.4#
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.
sub(Pos2D pos): Pos2DSince1.8.4#
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.
sub(double x, double y): Pos2DSince1.8.4#
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.

toMojangDoubleVector ​

toMojangDoubleVector(): Vec3Since1.8.4#
Returns a raw Minecraft vector with the same coordinates as this position.
Returns: a new raw vanilla vector at x, y, z

toRawBlockPos ​

toRawBlockPos(): BlockPosSince1.8.0#
Returns the raw block position containing this position, flooring each coordinate.
Returns: a new raw block position of the block containing this position

toReverseVector ​

toReverseVector(): Vec3DSince1.6.4#
Returns a new vector from this position to the origin, opposite to toVector().
Returns: a new Vec3D from this position to 0, 0, 0
toReverseVector(Pos2D end_pos): Vec3DSince1.6.4#
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 from this position to end_pos.to3D()
toReverseVector(Pos3D end_pos): Vec3DSince1.6.4#
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 from this position to end_pos
toReverseVector(double end_x, double end_y, double end_z): Vec3DSince1.6.4#
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 from this position to end_x, end_y, end_z
toReverseVector(double end_x, double end_y): Vec2DSince1.6.4#
Returns a vector from this position to the given coordinates, opposite to 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 from this position to end_x, end_y

toString ​

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 ​

toVector(): Vec3DSince1.2.6#
Returns a vector from ZERO to this position. The shared ZERO object is mutable, so changing it affects subsequent calls.
Returns: a new Vec3D from 0, 0, 0 to this position
toVector(Pos2D start_pos): Vec3DSince1.6.4#
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 from start_pos.to3D() to this position
toVector(Pos3D start_pos): Vec3DSince1.6.4#
Returns a vector from the given position to this position.

Parameters:

  • start_pos: the position the vector starts at
Returns: a new Vec3D from start_pos to this position
toVector(double start_x, double start_y, double start_z): Vec3DSince1.6.4#
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 from start_x, start_y, start_z to this position
toVector(double start_x, double start_y): Vec2DSince1.6.4#
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 from start_x, start_y to this position