PocketMine-MP 5.15.1 git-5ef247620a7c6301a849b54e5ef1009217729fc8
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 floor;
27use const INF;
28
29final class VoxelRayTrace{
30 private function __construct(){
31 //NOOP
32 }
33
43 public static function inDirection(Vector3 $start, Vector3 $directionVector, float $maxDistance) : \Generator{
44 return self::betweenPoints($start, $start->addVector($directionVector->multiply($maxDistance)));
45 }
46
67 public static function betweenPoints(Vector3 $start, Vector3 $end) : \Generator{
68 $currentBlock = $start->floor();
69
70 $directionVector = $end->subtractVector($start)->normalize();
71 if($directionVector->lengthSquared() <= 0){
72 throw new \InvalidArgumentException("Start and end points are the same, giving a zero direction vector");
73 }
74
75 $radius = $start->distance($end);
76
77 $stepX = $directionVector->x <=> 0;
78 $stepY = $directionVector->y <=> 0;
79 $stepZ = $directionVector->z <=> 0;
80
81 //Initialize the step accumulation variables depending how far into the current block the start position is. If
82 //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);
86
87 //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;
91
92 while(true){
93 yield $currentBlock;
94
95 // tMaxX stores the t-value at which we cross a cube boundary along the
96 // X axis, and similarly for Y and Z. Therefore, choosing the least tMax
97 // chooses the closest cube boundary.
98 if($tMaxX < $tMaxY and $tMaxX < $tMaxZ){
99 if($tMaxX > $radius){
100 break;
101 }
102 $currentBlock = $currentBlock->add($stepX, 0, 0);
103 $tMaxX += $tDeltaX;
104 }elseif($tMaxY < $tMaxZ){
105 if($tMaxY > $radius){
106 break;
107 }
108 $currentBlock = $currentBlock->add(0, $stepY, 0);
109 $tMaxY += $tDeltaY;
110 }else{
111 if($tMaxZ > $radius){
112 break;
113 }
114 $currentBlock = $currentBlock->add(0, 0, $stepZ);
115 $tMaxZ += $tDeltaZ;
116 }
117 }
118 }
119
134 private static function rayTraceDistanceToBoundary(float $s, float $ds) : float{
135 if($ds == 0){
136 return INF;
137 }
138
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;
150 }
151}
static betweenPoints(Vector3 $start, Vector3 $end)
static inDirection(Vector3 $start, Vector3 $directionVector, float $maxDistance)