Interaction checks happen using the plane and not 3d space

This commit is contained in:
Shoghi Cervantes 2015-05-07 15:38:22 +02:00
parent 82b0dbfe8e
commit 563f6f8e4f
4 changed files with 19 additions and 9 deletions

View File

@ -80,6 +80,7 @@ use pocketmine\level\Location;
use pocketmine\level\Position; use pocketmine\level\Position;
use pocketmine\level\sound\LaunchSound; use pocketmine\level\sound\LaunchSound;
use pocketmine\math\AxisAlignedBB; use pocketmine\math\AxisAlignedBB;
use pocketmine\math\Vector2;
use pocketmine\math\Vector3; use pocketmine\math\Vector3;
use pocketmine\metadata\MetadataValue; use pocketmine\metadata\MetadataValue;
use pocketmine\nbt\NBT; 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){ public function canInteract(Vector3 $pos, $maxDistance, $maxDiff = 0.5){
$dV = $this->getDirectionVector(); if($this->distanceSquared($pos) > $maxDistance ** 2){
$dot = $dV->dot($this); return false;
$dot1 = $dV->dot($pos); }
return ($dot1 - $dot) >= -$maxDiff and $this->distanceSquared($pos) <= $maxDistance ** 2;
$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;
} }
/** /**

View File

@ -40,6 +40,7 @@ use pocketmine\level\Location;
use pocketmine\level\Position; use pocketmine\level\Position;
use pocketmine\math\AxisAlignedBB; use pocketmine\math\AxisAlignedBB;
use pocketmine\math\Math; use pocketmine\math\Math;
use pocketmine\math\Vector2;
use pocketmine\math\Vector3; use pocketmine\math\Vector3;
use pocketmine\metadata\Metadatable; use pocketmine\metadata\Metadatable;
use pocketmine\metadata\MetadataValue; use pocketmine\metadata\MetadataValue;
@ -834,7 +835,11 @@ abstract class Entity extends Location implements Metadatable{
$x = -$xz * sin(deg2rad($this->yaw)); $x = -$xz * sin(deg2rad($this->yaw));
$z = $xz * cos(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){ public function onUpdate($currentTick){

View File

@ -115,9 +115,9 @@ class Vector2{
} }
public function normalize(){ public function normalize(){
$len = $this->length(); $len = $this->lengthSquared();
if($len != 0){ if($len != 0){
return $this->divide($len); return $this->divide(sqrt($len));
} }
return new Vector2(0, 0); return new Vector2(0, 0);

View File

@ -206,9 +206,9 @@ class Vector3{
* @return Vector3 * @return Vector3
*/ */
public function normalize(){ public function normalize(){
$len = $this->length(); $len = $this->lengthSquared();
if($len != 0){ if($len != 0){
return $this->divide($len); return $this->divide(sqrt($len));
} }
return new Vector3(0, 0, 0); return new Vector3(0, 0, 0);