pocketmine\entity\Entity::getDirectionVector PHP Method

getDirectionVector() public method

public getDirectionVector ( ) : Vector3
return pocketmine\math\Vector3
    public function getDirectionVector()
    {
        $y = -sin(deg2rad($this->pitch));
        $xz = cos(deg2rad($this->pitch));
        $x = -$xz * sin(deg2rad($this->yaw));
        $z = $xz * cos(deg2rad($this->yaw));
        return $this->temporalVector->setComponents($x, $y, $z)->normalize();
    }

Usage Example

 public static function getCrosshairTarget(Entity $entity, $accuracy = 0.5, $max = PHP_INT_MAX)
 {
     $found = null;
     $direction = $entity->getDirectionVector()->multiply($accuracy);
     /** @var Vector3 $last */
     for ($last = null, $pos = $entity->add($direction), $i = 1; $i * $accuracy <= $max; $last = $pos->floor(), $pos = $entity->add($direction->multiply(++$i))) {
         if ($last instanceof Vector3) {
             if ($last->x === $pos->getFloorX() and $last->y === $pos->getFloorY() and $last->z === $pos->getFloorZ()) {
                 continue;
             }
             if ($pos->y < 0) {
                 break;
             }
             $maxY = 127;
             if (defined($path = "pemapmodder\\worldeditart\\MAX_WORLD_HEIGHT")) {
                 $maxY = constant($path);
             }
             if ($pos->y > $maxY + 1) {
                 break;
             }
             $block = $entity->getLevel()->getBlock($pos);
             if (!$block instanceof Block) {
                 break;
             }
             if ($block instanceof Air) {
                 continue;
             }
             $found = $block;
             break;
         }
     }
     return $found;
 }