PocketMine-MP 5.33.2 git-919492bdcad8510eb6606439eb77e1c604f1d1ea
Loading...
Searching...
No Matches
VoxelRayTrace.php
1<?php
2
3/*
4 *
5 * ____ _ _ __ __ _ __ __ ____
6 * | _ \ ___ ___| | _____| |_| \/ (_)_ __ ___ | \/ | _ \
7 * | |_) / _ \ / __| |/ / _ \ __| |\/| | | '_ \ / _ \_____| |\/| | |_) |
8 * | __/ (_) | (__| < __/ |_| | | | | | | | __/_____| | | | __/
9 * |_| \___/ \___|_|\_\___|\__|_| |_|_|_| |_|\___| |_| |_|_|
10 *
11 * This program is free software: you can redistribute it and/or modify
12 * it under the terms of the GNU Lesser General Public License as published by
13 * the Free Software Foundation, either version 3 of the License, or
14 * (at your option) any later version.
15 *
16 * @author PocketMine Team
17 * @link http://www.pocketmine.net/
18 *
19 *
20*/
21
22declare(strict_types=1);
23
24namespace pocketmine\math;
25
26use function floatval;
27use function floor;
28use const INF;
29
30final class VoxelRayTrace{
31 private function __construct(){
32 //NOOP
33 }
34
44 public static function inDirection(Vector3 $start, Vector3 $directionVector, float $maxDistance) : \Generator{
45 return self::betweenPoints($start, $start->addVector($directionVector->multiply($maxDistance)));
46 }
47
68 public static function betweenPoints(Vector3 $start, Vector3 $end) : \Generator{
69 $currentBlock = $start->floor();
70
71 $directionVector = $end->subtractVector($start)->normalize();
72 if($directionVector->lengthSquared() <= 0){
73 throw new \InvalidArgumentException("Start and end points are the same, giving a zero direction vector");
74 }
75
76 $radius = $start->distance($end);
77
78 $stepX = $directionVector->x <=> 0;
79 $stepY = $directionVector->y <=> 0;
80 $stepZ = $directionVector->z <=> 0;
81
82 //Initialize the step accumulation variables depending how far into the current block the start position is. If
83 //the start position is on the corner of the block, these will be zero.
84 $tMaxX = self::distanceFactorToBoundary($start->x, $directionVector->x);
85 $tMaxY = self::distanceFactorToBoundary($start->y, $directionVector->y);
86 $tMaxZ = self::distanceFactorToBoundary($start->z, $directionVector->z);
87
88 //The change in t on each axis when taking a step on that axis (always positive).
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;
92
93 while(true){
94 yield $currentBlock;
95
96 // tMaxX stores the t-value at which we cross a cube boundary along the
97 // X axis, and similarly for Y and Z. Therefore, choosing the least tMax
98 // chooses the closest cube boundary.
99 if($tMaxX < $tMaxY and $tMaxX < $tMaxZ){
100 if($tMaxX > $radius){
101 break;
102 }
103 $currentBlock = $currentBlock->add($stepX, 0, 0);
104 $tMaxX += $tDeltaX;
105 }elseif($tMaxY < $tMaxZ){
106 if($tMaxY > $radius){
107 break;
108 }
109 $currentBlock = $currentBlock->add(0, $stepY, 0);
110 $tMaxY += $tDeltaY;
111 }else{
112 if($tMaxZ > $radius){
113 break;
114 }
115 $currentBlock = $currentBlock->add(0, 0, $stepZ);
116 $tMaxZ += $tDeltaZ;
117 }
118 }
119 }
120
135 private static function distanceFactorToBoundary(float $s, float $ds) : float{
136 if($ds === 0.0){
137 return INF;
138 }
139
140 return $ds < 0 ?
141 ($s - floor($s)) / -$ds :
142 (1 - ($s - floor($s))) / $ds;
143 }
144}
static betweenPoints(Vector3 $start, Vector3 $end)
static inDirection(Vector3 $start, Vector3 $directionVector, float $maxDistance)