diff --git a/src/pocketmine/Player.php b/src/pocketmine/Player.php index 170182b1a..26d419f0e 100644 --- a/src/pocketmine/Player.php +++ b/src/pocketmine/Player.php @@ -80,6 +80,7 @@ use pocketmine\level\Location; use pocketmine\level\Position; use pocketmine\level\sound\LaunchSound; use pocketmine\math\AxisAlignedBB; +use pocketmine\math\Vector2; use pocketmine\math\Vector3; use pocketmine\metadata\MetadataValue; use pocketmine\nbt\NBT; @@ -1455,10 +1456,14 @@ class Player extends Human implements CommandSender, InventoryHolder, IPlayer{ } public function canInteract(Vector3 $pos, $maxDistance, $maxDiff = 0.5){ - $dV = $this->getDirectionVector(); - $dot = $dV->dot($this); - $dot1 = $dV->dot($pos); - return ($dot1 - $dot) >= -$maxDiff and $this->distanceSquared($pos) <= $maxDistance ** 2; + if($this->distanceSquared($pos) > $maxDistance ** 2){ + return false; + } + + $dV = $this->getDirectionPlane(); + $dot = $dV->dot(new Vector2($this->x, $this->z)); + $dot1 = $dV->dot(new Vector2($pos->x, $pos->z)); + return ($dot1 - $dot) >= -$maxDiff; } /** diff --git a/src/pocketmine/entity/Entity.php b/src/pocketmine/entity/Entity.php index 05ae54bc1..ea105e62f 100644 --- a/src/pocketmine/entity/Entity.php +++ b/src/pocketmine/entity/Entity.php @@ -40,6 +40,7 @@ use pocketmine\level\Location; use pocketmine\level\Position; use pocketmine\math\AxisAlignedBB; use pocketmine\math\Math; +use pocketmine\math\Vector2; use pocketmine\math\Vector3; use pocketmine\metadata\Metadatable; use pocketmine\metadata\MetadataValue; @@ -834,7 +835,11 @@ abstract class Entity extends Location implements Metadatable{ $x = -$xz * sin(deg2rad($this->yaw)); $z = $xz * cos(deg2rad($this->yaw)); - return new Vector3($x, $y, $z); + return (new Vector3($x, $y, $z))->normalize(); + } + + public function getDirectionPlane(){ + return (new Vector2(-cos(deg2rad($this->yaw) - M_PI_2), -sin(deg2rad($this->yaw) - M_PI_2)))->normalize(); } public function onUpdate($currentTick){ diff --git a/src/pocketmine/math/Vector2.php b/src/pocketmine/math/Vector2.php index 0d7a4cfcc..e6b600f79 100644 --- a/src/pocketmine/math/Vector2.php +++ b/src/pocketmine/math/Vector2.php @@ -115,9 +115,9 @@ class Vector2{ } public function normalize(){ - $len = $this->length(); + $len = $this->lengthSquared(); if($len != 0){ - return $this->divide($len); + return $this->divide(sqrt($len)); } return new Vector2(0, 0); diff --git a/src/pocketmine/math/Vector3.php b/src/pocketmine/math/Vector3.php index a7f3871ee..546088e4d 100644 --- a/src/pocketmine/math/Vector3.php +++ b/src/pocketmine/math/Vector3.php @@ -206,9 +206,9 @@ class Vector3{ * @return Vector3 */ public function normalize(){ - $len = $this->length(); + $len = $this->lengthSquared(); if($len != 0){ - return $this->divide($len); + return $this->divide(sqrt($len)); } return new Vector3(0, 0, 0);