Cloneable
, ConfigurationSerializable
public class BoundingBox extends Object implements Cloneable, ConfigurationSerializable
This basically represents a rectangular box (specified by minimum and maximum corners) that can for example be used to describe the position and extents of an object (such as an entity, block, or rectangular region) in 3D space. Its edges and faces are parallel to the axes of the cartesian coordinate system.
The bounding box may be degenerate (one or more sides having the length 0).
Because bounding boxes are mutable, storing them long term may be dangerous
if they get modified later. If you want to keep around a bounding box, it may
be wise to call clone()
in order to get a copy.
Constructor | Description |
---|---|
BoundingBox() |
Creates a new (degenerate) bounding box with all corner coordinates at
0 . |
BoundingBox(double x1,
double y1,
double z1,
double x2,
double y2,
double z2) |
Creates a new bounding box from the given corner coordinates.
|
Modifier and Type | Method | Description |
---|---|---|
BoundingBox |
clone() |
Creates a copy of this bounding box.
|
boolean |
contains(double x,
double y,
double z) |
Checks if this bounding box contains the specified position.
|
boolean |
contains(BoundingBox other) |
Checks if this bounding box fully contains the given bounding box.
|
boolean |
contains(Vector position) |
Checks if this bounding box contains the specified position.
|
boolean |
contains(Vector min,
Vector max) |
Checks if this bounding box fully contains the bounding box that is
defined by the given corners.
|
BoundingBox |
copy(BoundingBox other) |
Copies another bounding box.
|
static BoundingBox |
deserialize(Map<String,Object> args) |
|
boolean |
equals(Object obj) |
|
BoundingBox |
expand(double expansion) |
Expands this bounding box uniformly by the given value in all directions.
|
BoundingBox |
expand(double x,
double y,
double z) |
Expands this bounding box uniformly by the given values in both positive
and negative directions.
|
BoundingBox |
expand(double dirX,
double dirY,
double dirZ,
double expansion) |
Expands this bounding box in the specified direction.
|
BoundingBox |
expand(double negativeX,
double negativeY,
double negativeZ,
double positiveX,
double positiveY,
double positiveZ) |
Expands this bounding box by the given values in the corresponding
directions.
|
BoundingBox |
expand(BlockFace blockFace,
double expansion) |
Expands this bounding box in the direction specified by the given block
face.
|
BoundingBox |
expand(Vector expansion) |
Expands this bounding box uniformly by the given values in both positive
and negative directions.
|
BoundingBox |
expand(Vector direction,
double expansion) |
Expands this bounding box in the specified direction.
|
BoundingBox |
expandDirectional(double dirX,
double dirY,
double dirZ) |
Expands this bounding box in the specified direction.
|
BoundingBox |
expandDirectional(Vector direction) |
Expands this bounding box in the specified direction.
|
Vector |
getCenter() |
Gets the center of the bounding box.
|
double |
getCenterX() |
Gets the x coordinate of the center of the bounding box.
|
double |
getCenterY() |
Gets the y coordinate of the center of the bounding box.
|
double |
getCenterZ() |
Gets the z coordinate of the center of the bounding box.
|
double |
getHeight() |
Gets the height of the bounding box.
|
Vector |
getMax() |
Gets the maximum corner as vector.
|
double |
getMaxX() |
Gets the maximum x value.
|
double |
getMaxY() |
Gets the maximum y value.
|
double |
getMaxZ() |
Gets the maximum z value.
|
Vector |
getMin() |
Gets the minimum corner as vector.
|
double |
getMinX() |
Gets the minimum x value.
|
double |
getMinY() |
Gets the minimum y value.
|
double |
getMinZ() |
Gets the minimum z value.
|
double |
getVolume() |
Gets the volume of the bounding box.
|
double |
getWidthX() |
Gets the width of the bounding box in the x direction.
|
double |
getWidthZ() |
Gets the width of the bounding box in the z direction.
|
int |
hashCode() |
|
BoundingBox |
intersection(BoundingBox other) |
Resizes this bounding box to represent the intersection of this and the
given bounding box.
|
static BoundingBox |
of(Block block) |
Creates a new 1x1x1 sized bounding box containing the given block.
|
static BoundingBox |
of(Block corner1,
Block corner2) |
Creates a new bounding box using the coordinates of the given blocks as
corners.
|
static BoundingBox |
of(Location center,
double x,
double y,
double z) |
Creates a new bounding box using the given center and extents.
|
static BoundingBox |
of(Location corner1,
Location corner2) |
Creates a new bounding box using the coordinates of the given locations
as corners.
|
static BoundingBox |
of(Vector center,
double x,
double y,
double z) |
Creates a new bounding box using the given center and extents.
|
static BoundingBox |
of(Vector corner1,
Vector corner2) |
Creates a new bounding box using the coordinates of the given vectors as
corners.
|
boolean |
overlaps(BoundingBox other) |
Checks if this bounding box overlaps with the given bounding box.
|
boolean |
overlaps(Vector min,
Vector max) |
Checks if this bounding box overlaps with the bounding box that is
defined by the given corners.
|
RayTraceResult |
rayTrace(Vector start,
Vector direction,
double maxDistance) |
Calculates the intersection of this bounding box with the specified line
segment.
|
BoundingBox |
resize(double x1,
double y1,
double z1,
double x2,
double y2,
double z2) |
Resizes this bounding box.
|
Map<String,Object> |
serialize() |
Creates a Map representation of this class.
|
BoundingBox |
shift(double shiftX,
double shiftY,
double shiftZ) |
Shifts this bounding box by the given amounts.
|
BoundingBox |
shift(Location shift) |
Shifts this bounding box by the given amounts.
|
BoundingBox |
shift(Vector shift) |
Shifts this bounding box by the given amounts.
|
String |
toString() |
|
BoundingBox |
union(double posX,
double posY,
double posZ) |
Expands this bounding box to contain (or border) the specified position.
|
BoundingBox |
union(Location position) |
Expands this bounding box to contain (or border) the specified position.
|
BoundingBox |
union(BoundingBox other) |
Expands this bounding box to contain both this and the given bounding
box.
|
BoundingBox |
union(Vector position) |
Expands this bounding box to contain (or border) the specified position.
|
public BoundingBox()
0
.public BoundingBox(double x1, double y1, double z1, double x2, double y2, double z2)
x1
- the first corner's x valuey1
- the first corner's y valuez1
- the first corner's z valuex2
- the second corner's x valuey2
- the second corner's y valuez2
- the second corner's z value@NotNull public static BoundingBox of(@NotNull Vector corner1, @NotNull Vector corner2)
corner1
- the first cornercorner2
- the second corner@NotNull public static BoundingBox of(@NotNull Location corner1, @NotNull Location corner2)
corner1
- the first cornercorner2
- the second corner@NotNull public static BoundingBox of(@NotNull Block corner1, @NotNull Block corner2)
The bounding box will be sized to fully contain both blocks.
corner1
- the first corner blockcorner2
- the second corner block@NotNull public static BoundingBox of(@NotNull Block block)
block
- the block@NotNull public static BoundingBox of(@NotNull Vector center, double x, double y, double z)
center
- the centerx
- 1/2 the size of the bounding box along the x axisy
- 1/2 the size of the bounding box along the y axisz
- 1/2 the size of the bounding box along the z axis@NotNull public static BoundingBox of(@NotNull Location center, double x, double y, double z)
center
- the centerx
- 1/2 the size of the bounding box along the x axisy
- 1/2 the size of the bounding box along the y axisz
- 1/2 the size of the bounding box along the z axis@NotNull public BoundingBox resize(double x1, double y1, double z1, double x2, double y2, double z2)
x1
- the first corner's x valuey1
- the first corner's y valuez1
- the first corner's z valuex2
- the second corner's x valuey2
- the second corner's y valuez2
- the second corner's z valuepublic double getMinX()
public double getMinY()
public double getMinZ()
@NotNull public Vector getMin()
public double getMaxX()
public double getMaxY()
public double getMaxZ()
@NotNull public Vector getMax()
public double getWidthX()
public double getWidthZ()
public double getHeight()
public double getVolume()
public double getCenterX()
public double getCenterY()
public double getCenterZ()
@NotNull public Vector getCenter()
@NotNull public BoundingBox copy(@NotNull BoundingBox other)
other
- the other bounding box@NotNull public BoundingBox expand(double negativeX, double negativeY, double negativeZ, double positiveX, double positiveY, double positiveZ)
Negative values will shrink the bounding box in the corresponding direction. Shrinking will be limited to the point where the affected opposite faces would meet if the they shrank at uniform speeds.
negativeX
- the amount of expansion in the negative x directionnegativeY
- the amount of expansion in the negative y directionnegativeZ
- the amount of expansion in the negative z directionpositiveX
- the amount of expansion in the positive x directionpositiveY
- the amount of expansion in the positive y directionpositiveZ
- the amount of expansion in the positive z direction@NotNull public BoundingBox expand(double x, double y, double z)
Negative values will shrink the bounding box. Shrinking will be limited to the bounding box's current size.
x
- the amount of expansion in both positive and negative x
directiony
- the amount of expansion in both positive and negative y
directionz
- the amount of expansion in both positive and negative z
direction@NotNull public BoundingBox expand(@NotNull Vector expansion)
Negative values will shrink the bounding box. Shrinking will be limited to the bounding box's current size.
expansion
- the expansion values@NotNull public BoundingBox expand(double expansion)
A negative value will shrink the bounding box. Shrinking will be limited to the bounding box's current size.
expansion
- the amount of expansion@NotNull public BoundingBox expand(double dirX, double dirY, double dirZ, double expansion)
The magnitude of the direction will scale the expansion. A negative expansion value will shrink the bounding box in this direction. Shrinking will be limited to the bounding box's current size.
dirX
- the x direction componentdirY
- the y direction componentdirZ
- the z direction componentexpansion
- the amount of expansion@NotNull public BoundingBox expand(@NotNull Vector direction, double expansion)
The magnitude of the direction will scale the expansion. A negative expansion value will shrink the bounding box in this direction. Shrinking will be limited to the bounding box's current size.
direction
- the directionexpansion
- the amount of expansion@NotNull public BoundingBox expand(@NotNull BlockFace blockFace, double expansion)
A negative expansion value will shrink the bounding box in this direction. Shrinking will be limited to the bounding box's current size.
blockFace
- the block faceexpansion
- the amount of expansion@NotNull public BoundingBox expandDirectional(double dirX, double dirY, double dirZ)
Negative values will expand the bounding box in the negative direction, positive values will expand it in the positive direction. The magnitudes of the direction components determine the corresponding amounts of expansion.
dirX
- the x direction componentdirY
- the y direction componentdirZ
- the z direction component@NotNull public BoundingBox expandDirectional(@NotNull Vector direction)
Negative values will expand the bounding box in the negative direction, positive values will expand it in the positive direction. The magnitude of the direction vector determines the amount of expansion.
direction
- the direction and magnitude of the expansion@NotNull public BoundingBox union(double posX, double posY, double posZ)
posX
- the x position valueposY
- the y position valueposZ
- the z position valuecontains(double, double, double)
@NotNull public BoundingBox union(@NotNull Vector position)
position
- the positioncontains(double, double, double)
@NotNull public BoundingBox union(@NotNull Location position)
position
- the positioncontains(double, double, double)
@NotNull public BoundingBox union(@NotNull BoundingBox other)
other
- the other bounding box@NotNull public BoundingBox intersection(@NotNull BoundingBox other)
other
- the other bounding boxIllegalArgumentException
- if the bounding boxes don't overlap@NotNull public BoundingBox shift(double shiftX, double shiftY, double shiftZ)
shiftX
- the shift in x directionshiftY
- the shift in y directionshiftZ
- the shift in z direction@NotNull public BoundingBox shift(@NotNull Vector shift)
shift
- the shift@NotNull public BoundingBox shift(@NotNull Location shift)
shift
- the shiftpublic boolean overlaps(@NotNull BoundingBox other)
Bounding boxes that are only intersecting at the borders are not considered overlapping.
other
- the other bounding boxtrue
if overlappingpublic boolean overlaps(@NotNull Vector min, @NotNull Vector max)
Bounding boxes that are only intersecting at the borders are not considered overlapping.
min
- the first cornermax
- the second cornertrue
if overlappingpublic boolean contains(double x, double y, double z)
Positions exactly on the minimum borders of the bounding box are considered to be inside the bounding box, while positions exactly on the maximum borders are considered to be outside. This allows bounding boxes to reside directly next to each other with positions always only residing in exactly one of them.
x
- the position's x coordinatesy
- the position's y coordinatesz
- the position's z coordinatestrue
if the bounding box contains the positionpublic boolean contains(@NotNull Vector position)
Positions exactly on the minimum borders of the bounding box are considered to be inside the bounding box, while positions exactly on the maximum borders are considered to be outside. This allows bounding boxes to reside directly next to each other with positions always only residing in exactly one of them.
position
- the positiontrue
if the bounding box contains the positionpublic boolean contains(@NotNull BoundingBox other)
other
- the other bounding boxtrue
if the bounding box contains the given bounding
boxpublic boolean contains(@NotNull Vector min, @NotNull Vector max)
min
- the first cornermax
- the second cornertrue
if the bounding box contains the specified
bounding box@Nullable public RayTraceResult rayTrace(@NotNull Vector start, @NotNull Vector direction, double maxDistance)
Intersections at edges and corners yield one of the affected block faces as hit result, but it is not defined which of them.
start
- the start positiondirection
- the ray directionmaxDistance
- the maximum distancenull
if there is no hit@NotNull public BoundingBox clone()
@NotNull public Map<String,Object> serialize()
ConfigurationSerializable
This class must provide a method to restore this class, as defined in
the ConfigurationSerializable
interface javadocs.
serialize
in interface ConfigurationSerializable
@NotNull public static BoundingBox deserialize(@NotNull Map<String,Object> args)
Copyright © 2019. All rights reserved.