Skip to content

Commit

Permalink
Merge branch 'stable' into major-next
Browse files Browse the repository at this point in the history
  • Loading branch information
dktapps committed Jan 3, 2025
2 parents f291b4d + c22fa06 commit 0638c0b
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 31 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ jobs:
if: "!contains(github.event.head_commit.message, '[ci skip]')"
strategy:
matrix:
php: ['8.2', '8.3']
php: ['8.2', '8.3', '8.4']
name: PHP ${{ matrix.php }}
steps:
- uses: actions/checkout@v4
Expand Down
4 changes: 2 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@
"php-64bit": "*"
},
"require-dev": {
"phpstan/phpstan": "~1.10.3",
"phpstan/phpstan": "2.1.0",
"phpstan/extension-installer": "^1.0",
"phpstan/phpstan-strict-rules": "^1.0",
"phpstan/phpstan-strict-rules": "^2.0",
"phpunit/phpunit": "^10.0 || ^11.0"
},
"autoload": {
Expand Down
2 changes: 1 addition & 1 deletion src/Math.php
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ public static function solveQuadratic(float $a, float $b, float $c) : array{
(-$b + $sqrtDiscriminant) / (2 * $a),
(-$b - $sqrtDiscriminant) / (2 * $a)
];
}elseif($discriminant == 0){ //1 real root
}elseif($discriminant === 0.0){ //1 real root
return [
-$b / (2 * $a)
];
Expand Down
6 changes: 5 additions & 1 deletion src/Vector3.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@

use function abs;
use function ceil;
use function floatval;
use function floor;
use function iterator_to_array;
use function max;
Expand Down Expand Up @@ -253,7 +254,10 @@ public function cross(Vector3 $v) : Vector3{
}

public function equals(Vector3 $v) : bool{
return $this->x == $v->x and $this->y == $v->y and $this->z == $v->z;
return
floatval($this->x) === floatval($v->x) and
floatval($this->y) === floatval($v->y) and
floatval($this->z) === floatval($v->z);
}

/**
Expand Down
45 changes: 19 additions & 26 deletions src/VoxelRayTrace.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@

namespace pocketmine\math;

use function floatval;
use function floor;
use const INF;

Expand Down Expand Up @@ -80,14 +81,14 @@ public static function betweenPoints(Vector3 $start, Vector3 $end) : \Generator{

//Initialize the step accumulation variables depending how far into the current block the start position is. If
//the start position is on the corner of the block, these will be zero.
$tMaxX = self::rayTraceDistanceToBoundary($start->x, $directionVector->x);
$tMaxY = self::rayTraceDistanceToBoundary($start->y, $directionVector->y);
$tMaxZ = self::rayTraceDistanceToBoundary($start->z, $directionVector->z);
$tMaxX = self::distanceFactorToBoundary($start->x, $directionVector->x);
$tMaxY = self::distanceFactorToBoundary($start->y, $directionVector->y);
$tMaxZ = self::distanceFactorToBoundary($start->z, $directionVector->z);

//The change in t on each axis when taking a step on that axis (always positive).
$tDeltaX = $directionVector->x == 0 ? 0 : $stepX / $directionVector->x;
$tDeltaY = $directionVector->y == 0 ? 0 : $stepY / $directionVector->y;
$tDeltaZ = $directionVector->z == 0 ? 0 : $stepZ / $directionVector->z;
$tDeltaX = floatval($directionVector->x) === 0.0 ? 0 : $stepX / $directionVector->x;
$tDeltaY = floatval($directionVector->y) === 0.0 ? 0 : $stepY / $directionVector->y;
$tDeltaZ = floatval($directionVector->z) === 0.0 ? 0 : $stepZ / $directionVector->z;

while(true){
yield $currentBlock;
Expand Down Expand Up @@ -118,34 +119,26 @@ public static function betweenPoints(Vector3 $start, Vector3 $end) : \Generator{
}

/**
* Returns the distance that must be travelled on an axis from the start point with the direction vector component to
* cross a block boundary.
* Used to decide which direction to move in first when beginning a ray trace.
*
* For example, given an X coordinate inside a block and the X component of a direction vector, will return the distance
* travelled by that direction component to reach a block with a different X coordinate.
*
* Find the smallest positive t such that s+t*ds is an integer.
* Examples:
* s=0.25, ds=0.5 -> 0.25 + 1.5(0.5) = 1 -> returns 1.5
* s=0.25, ds=-0.5 -> 0.25 + 0.5(-0.5) = 0 -> returns 0.5
* s=1 ds=0.5 -> 1 + 2(0.5) = 2 -> returns 2
* s=1 ds=-0.5 -> 1 + 0(-0.5) = 1 -> returns 0 (ds is negative and any subtraction will change 1 to 0.x)
*
* @param float $s Starting coordinate
* @param float $ds Direction vector component of the relevant axis
*
* @return float Distance along the ray trace that must be travelled to cross a boundary.
* @return float Number of times $ds must be added to $s to change its whole-number component.
*/
private static function rayTraceDistanceToBoundary(float $s, float $ds) : float{
if($ds == 0){
private static function distanceFactorToBoundary(float $s, float $ds) : float{
if($ds === 0.0){
return INF;
}

if($ds < 0){
$s = -$s;
$ds = -$ds;

if(floor($s) == $s){ //exactly at coordinate, will leave the coordinate immediately by moving negatively
return 0;
}
}

// problem is now s+t*ds = 1
return (1 - ($s - floor($s))) / $ds;
return $ds < 0 ?
($s - floor($s)) / -$ds :
(1 - ($s - floor($s))) / $ds;
}
}

0 comments on commit 0638c0b

Please sign in to comment.