Skip to content

Commit 0638c0b

Browse files
committed
Merge branch 'stable' into major-next
2 parents f291b4d + c22fa06 commit 0638c0b

File tree

5 files changed

+28
-31
lines changed

5 files changed

+28
-31
lines changed

.github/workflows/ci.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ jobs:
88
if: "!contains(github.event.head_commit.message, '[ci skip]')"
99
strategy:
1010
matrix:
11-
php: ['8.2', '8.3']
11+
php: ['8.2', '8.3', '8.4']
1212
name: PHP ${{ matrix.php }}
1313
steps:
1414
- uses: actions/checkout@v4

composer.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,9 @@
88
"php-64bit": "*"
99
},
1010
"require-dev": {
11-
"phpstan/phpstan": "~1.10.3",
11+
"phpstan/phpstan": "2.1.0",
1212
"phpstan/extension-installer": "^1.0",
13-
"phpstan/phpstan-strict-rules": "^1.0",
13+
"phpstan/phpstan-strict-rules": "^2.0",
1414
"phpunit/phpunit": "^10.0 || ^11.0"
1515
},
1616
"autoload": {

src/Math.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ public static function solveQuadratic(float $a, float $b, float $c) : array{
6565
(-$b + $sqrtDiscriminant) / (2 * $a),
6666
(-$b - $sqrtDiscriminant) / (2 * $a)
6767
];
68-
}elseif($discriminant == 0){ //1 real root
68+
}elseif($discriminant === 0.0){ //1 real root
6969
return [
7070
-$b / (2 * $a)
7171
];

src/Vector3.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525

2626
use function abs;
2727
use function ceil;
28+
use function floatval;
2829
use function floor;
2930
use function iterator_to_array;
3031
use function max;
@@ -253,7 +254,10 @@ public function cross(Vector3 $v) : Vector3{
253254
}
254255

255256
public function equals(Vector3 $v) : bool{
256-
return $this->x == $v->x and $this->y == $v->y and $this->z == $v->z;
257+
return
258+
floatval($this->x) === floatval($v->x) and
259+
floatval($this->y) === floatval($v->y) and
260+
floatval($this->z) === floatval($v->z);
257261
}
258262

259263
/**

src/VoxelRayTrace.php

Lines changed: 19 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323

2424
namespace pocketmine\math;
2525

26+
use function floatval;
2627
use function floor;
2728
use const INF;
2829

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

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

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

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

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

139-
if($ds < 0){
140-
$s = -$s;
141-
$ds = -$ds;
142-
143-
if(floor($s) == $s){ //exactly at coordinate, will leave the coordinate immediately by moving negatively
144-
return 0;
145-
}
146-
}
147-
148-
// problem is now s+t*ds = 1
149-
return (1 - ($s - floor($s))) / $ds;
140+
return $ds < 0 ?
141+
($s - floor($s)) / -$ds :
142+
(1 - ($s - floor($s))) / $ds;
150143
}
151144
}

0 commit comments

Comments
 (0)