Added some documentation to AxisAlignedBB

This commit is contained in:
Dylan K. Taylor 2017-10-12 12:22:13 +01:00
parent 7e9b89e48a
commit cc7ed7a28f

View File

@ -60,6 +60,17 @@ class AxisAlignedBB{
return $this; return $this;
} }
/**
* Returns a new AxisAlignedBB extended by the specified X, Y and Z.
* If each of X, Y and Z are positive, the relevant max bound will be increased. If negative, the relevant min
* bound will be decreased.
*
* @param float $x
* @param float $y
* @param float $z
*
* @return AxisAlignedBB
*/
public function addCoord(float $x, float $y, float $z) : AxisAlignedBB{ public function addCoord(float $x, float $y, float $z) : AxisAlignedBB{
$minX = $this->minX; $minX = $this->minX;
$minY = $this->minY; $minY = $this->minY;
@ -89,10 +100,27 @@ class AxisAlignedBB{
return new AxisAlignedBB($minX, $minY, $minZ, $maxX, $maxY, $maxZ); return new AxisAlignedBB($minX, $minY, $minZ, $maxX, $maxY, $maxZ);
} }
/**
* Returns a new AxisAlignedBB with bounds outset by the specified X, Y and Z.
*
* @param float $x
* @param float $y
* @param float $z
*
* @return AxisAlignedBB
*/
public function grow(float $x, float $y, float $z) : AxisAlignedBB{ public function grow(float $x, float $y, float $z) : AxisAlignedBB{
return new AxisAlignedBB($this->minX - $x, $this->minY - $y, $this->minZ - $z, $this->maxX + $x, $this->maxY + $y, $this->maxZ + $z); return new AxisAlignedBB($this->minX - $x, $this->minY - $y, $this->minZ - $z, $this->maxX + $x, $this->maxY + $y, $this->maxZ + $z);
} }
/**
* Performs the same operation as grow() but operates on itself instead of returning a new object.
* @param float $x
* @param float $y
* @param float $z
*
* @return $this
*/
public function expand(float $x, float $y, float $z){ public function expand(float $x, float $y, float $z){
$this->minX -= $x; $this->minX -= $x;
$this->minY -= $y; $this->minY -= $y;
@ -104,6 +132,15 @@ class AxisAlignedBB{
return $this; return $this;
} }
/**
* Performs the same operation as getOffsetBoundingBox(), but operates on itself instead of returning a new object.
*
* @param float $x
* @param float $y
* @param float $z
*
* @return $this
*/
public function offset(float $x, float $y, float $z){ public function offset(float $x, float $y, float $z){
$this->minX += $x; $this->minX += $x;
$this->minY += $y; $this->minY += $y;
@ -115,10 +152,28 @@ class AxisAlignedBB{
return $this; return $this;
} }
/**
* Returns a new AxisAlignedBB with bounds inset by the specified X, Y and Z.
*
* @param float $x
* @param float $y
* @param float $z
*
* @return AxisAlignedBB
*/
public function shrink(float $x, float $y, float $z) : AxisAlignedBB{ public function shrink(float $x, float $y, float $z) : AxisAlignedBB{
return new AxisAlignedBB($this->minX + $x, $this->minY + $y, $this->minZ + $z, $this->maxX - $x, $this->maxY - $y, $this->maxZ - $z); return new AxisAlignedBB($this->minX + $x, $this->minY + $y, $this->minZ + $z, $this->maxX - $x, $this->maxY - $y, $this->maxZ - $z);
} }
/**
* Performs the same operation as shrink(), but operates on itself instead of returning a new object.
*
* @param float $x
* @param float $y
* @param float $z
*
* @return $this
*/
public function contract(float $x, float $y, float $z){ public function contract(float $x, float $y, float $z){
$this->minX += $x; $this->minX += $x;
$this->minY += $y; $this->minY += $y;
@ -130,6 +185,12 @@ class AxisAlignedBB{
return $this; return $this;
} }
/**
* Sets the bounding box's bounds from another AxisAlignedBB, and returns itself
*
* @param AxisAlignedBB $bb
* @return $this
*/
public function setBB(AxisAlignedBB $bb){ public function setBB(AxisAlignedBB $bb){
$this->minX = $bb->minX; $this->minX = $bb->minX;
$this->minY = $bb->minY; $this->minY = $bb->minY;
@ -140,7 +201,16 @@ class AxisAlignedBB{
return $this; return $this;
} }
public function getOffsetBoundingBox($x, $y, $z) : AxisAlignedBB{ /**
* Returns a new AxisAlignedBB shifted by the specified X, Y and Z
*
* @param float $x
* @param float $y
* @param float $z
*
* @return AxisAlignedBB
*/
public function getOffsetBoundingBox(float $x, float $y, float $z) : AxisAlignedBB{
return new AxisAlignedBB($this->minX + $x, $this->minY + $y, $this->minZ + $z, $this->maxX + $x, $this->maxY + $y, $this->maxZ + $z); return new AxisAlignedBB($this->minX + $x, $this->minY + $y, $this->minZ + $z, $this->maxX + $x, $this->maxY + $y, $this->maxZ + $z);
} }
@ -213,6 +283,12 @@ class AxisAlignedBB{
return $z; return $z;
} }
/**
* Returns whether any part of the specified AABB is inside (intersects with) this one.
*
* @param AxisAlignedBB $bb
* @return bool
*/
public function intersectsWith(AxisAlignedBB $bb) : bool{ public function intersectsWith(AxisAlignedBB $bb) : bool{
if($bb->maxX > $this->minX and $bb->minX < $this->maxX){ if($bb->maxX > $this->minX and $bb->minX < $this->maxX){
if($bb->maxY > $this->minY and $bb->minY < $this->maxY){ if($bb->maxY > $this->minY and $bb->minY < $this->maxY){
@ -223,6 +299,12 @@ class AxisAlignedBB{
return false; return false;
} }
/**
* Returns whether the specified vector is within the bounds of this AABB on all axes.
*
* @param Vector3 $vector
* @return bool
*/
public function isVectorInside(Vector3 $vector) : bool{ public function isVectorInside(Vector3 $vector) : bool{
if($vector->x <= $this->minX or $vector->x >= $this->maxX){ if($vector->x <= $this->minX or $vector->x >= $this->maxX){
return false; return false;
@ -234,23 +316,49 @@ class AxisAlignedBB{
return $vector->z > $this->minZ and $vector->z < $this->maxZ; return $vector->z > $this->minZ and $vector->z < $this->maxZ;
} }
/**
* Returns the mean average of the AABB's X, Y and Z lengths.
* @return float
*/
public function getAverageEdgeLength() : float{ public function getAverageEdgeLength() : float{
return ($this->maxX - $this->minX + $this->maxY - $this->minY + $this->maxZ - $this->minZ) / 3; return ($this->maxX - $this->minX + $this->maxY - $this->minY + $this->maxZ - $this->minZ) / 3;
} }
/**
* Returns whether the specified vector is within the Y and Z bounds of this AABB.
*
* @param Vector3 $vector
* @return bool
*/
public function isVectorInYZ(Vector3 $vector) : bool{ public function isVectorInYZ(Vector3 $vector) : bool{
return $vector->y >= $this->minY and $vector->y <= $this->maxY and $vector->z >= $this->minZ and $vector->z <= $this->maxZ; return $vector->y >= $this->minY and $vector->y <= $this->maxY and $vector->z >= $this->minZ and $vector->z <= $this->maxZ;
} }
/**
* Returns whether the specified vector is within the X and Z bounds of this AABB.
*
* @param Vector3 $vector
* @return bool
*/
public function isVectorInXZ(Vector3 $vector) : bool{ public function isVectorInXZ(Vector3 $vector) : bool{
return $vector->x >= $this->minX and $vector->x <= $this->maxX and $vector->z >= $this->minZ and $vector->z <= $this->maxZ; return $vector->x >= $this->minX and $vector->x <= $this->maxX and $vector->z >= $this->minZ and $vector->z <= $this->maxZ;
} }
/**
* Returns whether the specified vector is within the X and Y bounds of this AABB.
*
* @param Vector3 $vector
* @return bool
*/
public function isVectorInXY(Vector3 $vector) : bool{ public function isVectorInXY(Vector3 $vector) : bool{
return $vector->x >= $this->minX and $vector->x <= $this->maxX and $vector->y >= $this->minY and $vector->y <= $this->maxY; return $vector->x >= $this->minX and $vector->x <= $this->maxX and $vector->y >= $this->minY and $vector->y <= $this->maxY;
} }
/** /**
* Performs a ray-trace and calculates the point on the AABB's edge nearest the start position that the ray-trace
* collided with. Returns a MovingObjectPosition with colliding vector closest to the start position.
* Returns null if no colliding point was found.
*
* @param Vector3 $pos1 * @param Vector3 $pos1
* @param Vector3 $pos2 * @param Vector3 $pos2
* *