38 public function __construct(
float $minX,
float $minY,
float $minZ,
float $maxX,
float $maxY,
float $maxZ){
40 throw new \InvalidArgumentException(
"minX $minX is larger than maxX $maxX");
43 throw new \InvalidArgumentException(
"minY $minY is larger than maxY $maxY");
46 throw new \InvalidArgumentException(
"minZ $minZ is larger than maxZ $maxZ");
87 return new AxisAlignedBB($minX, $minY, $minZ, $maxX, $maxY, $maxZ);
122 [$offsetX, $offsetY, $offsetZ] =
Facing::OFFSET[$face->value];
124 return $this->offsetCopy($offsetX * $distance, $offsetY * $distance, $offsetZ * $distance);
155 Facing::DOWN => $minY -= $distance,
156 Facing::UP => $maxY += $distance,
157 Facing::NORTH => $minZ -= $distance,
158 Facing::SOUTH => $maxZ += $distance,
159 Facing::WEST => $minX -= $distance,
160 Facing::EAST => $maxX += $distance,
163 return new AxisAlignedBB($minX, $minY, $minZ, $maxX, $maxY, $maxZ);
173 return $this->extendedCopy($face, -$distance);
189 if($axis === Axis::Y){
192 }elseif($axis === Axis::Z){
195 }elseif($axis === Axis::X){
200 return new AxisAlignedBB($minX, $minY, $minZ, $maxX, $maxY, $maxZ);
208 return $this->stretchedCopy($axis, -$distance);
211 public function calculateXOffset(
AxisAlignedBB $bb,
float $x) : float{
212 if($bb->maxY <= $this->minY or $bb->minY >= $this->maxY){
215 if($bb->maxZ <= $this->minZ or $bb->minZ >= $this->maxZ){
218 if($x > 0 and $bb->maxX <= $this->minX){
219 $x1 = $this->minX - $bb->maxX;
223 }elseif($x < 0 and $bb->minX >= $this->maxX){
224 $x2 = $this->maxX - $bb->minX;
233 public function calculateYOffset(AxisAlignedBB $bb,
float $y) : float{
234 if($bb->maxX <= $this->minX or $bb->minX >= $this->maxX){
237 if($bb->maxZ <= $this->minZ or $bb->minZ >= $this->maxZ){
240 if($y > 0 and $bb->maxY <= $this->minY){
241 $y1 = $this->minY - $bb->maxY;
245 }elseif($y < 0 and $bb->minY >= $this->maxY){
246 $y2 = $this->maxY - $bb->minY;
255 public function calculateZOffset(AxisAlignedBB $bb,
float $z) : float{
256 if($bb->maxX <= $this->minX or $bb->minX >= $this->maxX){
259 if($bb->maxY <= $this->minY or $bb->minY >= $this->maxY){
262 if($z > 0 and $bb->maxZ <= $this->minZ){
263 $z1 = $this->minZ - $bb->maxZ;
267 }elseif($z < 0 and $bb->minZ >= $this->maxZ){
268 $z2 = $this->maxZ - $bb->minZ;
281 if($bb->maxX - $this->minX > $epsilon and $this->maxX - $bb->minX > $epsilon){
282 if($bb->maxY - $this->minY > $epsilon and $this->maxY - $bb->minY > $epsilon){
283 return $bb->maxZ - $this->minZ > $epsilon and $this->maxZ - $bb->minZ > $epsilon;
294 if($vector->x <= $this->minX or $vector->x >= $this->maxX){
297 if($vector->y <= $this->minY or $vector->y >= $this->maxY){
301 return $vector->z > $this->minZ and $vector->z < $this->maxZ;
308 return ($this->maxX - $this->minX + $this->maxY - $this->minY + $this->maxZ - $this->minZ) / 3;
311 public function getXLength() : float{ return $this->maxX - $this->minX; }
313 public function getYLength() : float{ return $this->maxY - $this->minY; }
315 public function getZLength() : float{ return $this->maxZ - $this->minZ; }
317 public function isCube(
float $epsilon = 0.000001) : bool{
318 [$xLen, $yLen, $zLen] = [$this->getXLength(), $this->getYLength(), $this->getZLength()];
319 return abs($xLen - $yLen) < $epsilon && abs($yLen - $zLen) < $epsilon;
326 return ($this->maxX - $this->minX) * ($this->maxY - $this->minY) * ($this->maxZ - $this->minZ);
333 return $vector->y >= $this->minY and $vector->y <= $this->maxY and $vector->z >= $this->minZ and $vector->z <= $this->maxZ;
340 return $vector->x >= $this->minX and $vector->x <= $this->maxX and $vector->z >= $this->minZ and $vector->z <= $this->maxZ;
347 return $vector->x >= $this->minX and $vector->x <= $this->maxX and $vector->y >= $this->minY and $vector->y <= $this->maxY;
356 $v1 = $pos1->getIntermediateWithXValue($pos2, $this->minX);
363 if($v1 !==
null and !$this->isVectorInYZ($v1)){
367 if($v2 !==
null and !$this->isVectorInYZ($v2)){
371 if($v3 !==
null and !$this->isVectorInXZ($v3)){
375 if($v4 !==
null and !$this->isVectorInXZ($v4)){
379 if($v5 !==
null and !$this->isVectorInXY($v5)){
383 if($v6 !==
null and !$this->isVectorInXY($v6)){
387 $distance = PHP_INT_MAX;
388 $vectorAndFace =
null;
395 [Facing::NORTH, $v5],
396 [Facing::SOUTH, $v6],
398 if($v !==
null and ($d = $pos1->distanceSquared($v)) < $distance){
400 $vectorAndFace = [$v, $f];
404 if($vectorAndFace ===
null){
407 [$vector, $face] = $vectorAndFace;
409 return new RayTraceResult($this, $face, $vector);
412 public function __toString() : string{
413 return
"AxisAlignedBB({$this->minX}, {$this->minY}, {$this->minZ}, {$this->maxX}, {$this->maxY}, {$this->maxZ})";