Pos3D
com.jsmacrosce.jsmacros.api.math.Pos3D
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
Copies the coordinates from a vanilla three-component vector.
Parameters:
vec: the vector to copy the coordinates out of
Copies another position into a separate object.
Parameters:
pos: the position to copy
Creates a position with the given coordinates.
Parameters:
x: the x coordinatey: the y coordinatez: the z coordinate
Fields
z
z: double#The mutable z coordinate.
ZERO
Methods
add
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.zReturns a new position with the given amounts added to its coordinates; this position is unchanged.
Parameters:
x: the amount to add to the x coordinatey: the amount to add to the y coordinatez: the amount to add to the z coordinate
Returns: a new position at
this.x + x, this.y + y, this.z + zReturns 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
compareTo
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
oCompares 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
odivide
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.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 byy: the y coordinate to divide byz: the z coordinate to divide by
Returns: a new position at
this.x / x, this.y / y, this.z / z.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.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 byy: the y coordinate to divide by
Returns: a new position at
this.x / x, this.y / y.equals
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 otherwisegetZ
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
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.zReturns a new position with each coordinate multiplied by its corresponding factor.
Parameters:
x: the factor for the x coordinatey: the factor for the y coordinatez: the factor for the z coordinate
Returns: a new position at
this.x * x, this.y * y, this.z * zReturns 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.yReturns 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 coordinatey: the factor for the y coordinate
Returns: a new position at
this.x * x, this.y * yscale
sub
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.Returns a new position with the given amounts subtracted coordinate by coordinate.
Parameters:
x: the x coordinate to subtracty: the y coordinate to subtractz: the z coordinate to subtract
Returns: a new position at
this.x - x, this.y - y, this.z - z.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.toMojangDoubleVector
toRawBlockPos
toReverseVector
Returns a new vector from this position to the origin, opposite to toVector().
Returns: a new Vec3D from this position to
0, 0, 0Returns 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 as0
Returns: a new Vec3D from this position to
end_pos.to3D()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_posReturns a vector from this position to the given coordinates; this position is the start.
Parameters:
end_x: the x coordinate the vector ends atend_y: the y coordinate the vector ends atend_z: the z coordinate the vector ends at
Returns: a new Vec3D from this position to
end_x, end_y, end_zReturns 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 atend_y: the y coordinate the vector ends at
Returns: a new Vec2D from this position to
end_x, end_ytoString
toVector
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 as0
Returns: a new Vec3D from
start_pos.to3D() to this positionReturns 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 positionReturns a vector from the given coordinates to this position.
Parameters:
start_x: the x coordinate the vector starts atstart_y: the y coordinate the vector starts atstart_z: the z coordinate the vector starts at
Returns: a new Vec3D from
start_x, start_y, start_z to this position