Skip to content

Commit

Permalink
AxisAlignedBB: clean up code in calculateIntercept()
Browse files Browse the repository at this point in the history
  • Loading branch information
dktapps committed Aug 3, 2023
1 parent 44fd696 commit d2beb9d
Showing 1 changed file with 12 additions and 19 deletions.
31 changes: 12 additions & 19 deletions src/AxisAlignedBB.php
Original file line number Diff line number Diff line change
Expand Up @@ -465,35 +465,28 @@ public function calculateIntercept(Vector3 $pos1, Vector3 $pos2) : ?RayTraceResu

$vector = null;
$distance = PHP_INT_MAX;

foreach([$v1, $v2, $v3, $v4, $v5, $v6] as $v){
$face = -1;

foreach([
Facing::WEST => $v1,
Facing::EAST => $v2,
Facing::DOWN => $v3,
Facing::UP => $v4,
Facing::NORTH => $v5,
Facing::SOUTH => $v6
] as $f => $v){
if($v !== null and ($d = $pos1->distanceSquared($v)) < $distance){
$vector = $v;
$distance = $d;
$face = $f;
}
}

if($vector === null){
return null;
}

$f = -1;

if($vector === $v1){
$f = Facing::WEST;
}elseif($vector === $v2){
$f = Facing::EAST;
}elseif($vector === $v3){
$f = Facing::DOWN;
}elseif($vector === $v4){
$f = Facing::UP;
}elseif($vector === $v5){
$f = Facing::NORTH;
}elseif($vector === $v6){
$f = Facing::SOUTH;
}

return new RayTraceResult($this, $f, $vector);
return new RayTraceResult($this, $face, $vector);
}

public function __toString(){
Expand Down

0 comments on commit d2beb9d

Please sign in to comment.