From e4d1898613c087abeafa3deb63fbc5928d31adec Mon Sep 17 00:00:00 2001 From: GameHerobrine Date: Thu, 23 Feb 2023 00:04:25 +0300 Subject: [PATCH] tweaks to headYaw rotation, so it should be nearly perfect now --- src/entity/Entity.php | 8 +++----- src/entity/Living.php | 1 + src/entity/ai/MobController.php | 19 +++++++++++++------ .../protocol/packet/RotateHeadPacket.php | 2 +- src/utils/Utils.php | 12 ++++++++---- 5 files changed, 26 insertions(+), 16 deletions(-) diff --git a/src/entity/Entity.php b/src/entity/Entity.php index e5958e9c..f88ad221 100644 --- a/src/entity/Entity.php +++ b/src/entity/Entity.php @@ -733,6 +733,9 @@ class Entity extends Position if($this->closed === true){ return false; } + if($this->lastHeadYaw != $this->headYaw){ + $this->sendHeadYaw(); + } $now = microtime(true); if($this->isStatic === false and ($this->lastX != $this->x or $this->lastY != $this->y or $this->lastZ != $this->z or $this->lastYaw != $this->yaw or $this->lastPitch != $this->pitch or $this->lastHeadYaw != $this->headYaw)){ if($this->class === ENTITY_PLAYER or ($this->last[5] + 8) < $now){ @@ -770,15 +773,10 @@ class Entity extends Position } } else{ $this->updatePosition(); - if($this->lastHeadYaw != $this->headYaw){ - $this->sendHeadYaw(); - } $this->updateLast(); } - } - $this->lastUpdate = $now; } diff --git a/src/entity/Living.php b/src/entity/Living.php index faf99cc0..2c103d93 100644 --- a/src/entity/Living.php +++ b/src/entity/Living.php @@ -33,6 +33,7 @@ abstract class Living extends Entity implements Damageable, Pathfindable{ if(!$this->dead && Entity::$allowedAI && $this->idleTime <= 0) { $this->ai->updateTasks(); } + $this->ai->mobController->rotateTick(); if($this->onGround){ //if(!$this->hasPath() && $this->pathFinder instanceof ITileNavigator){ // $this->path = $this->pathFinder->navigate(new PathTileXYZ($this->x, $this->y, $this->z, $this->level), new PathTileXYZ($this->x + mt_rand(-10, 10), $this->y + mt_rand(-1, 1), $this->z + mt_rand(-10, 10), $this->level), 10); diff --git a/src/entity/ai/MobController.php b/src/entity/ai/MobController.php index 6a7fde35..c5db87f5 100644 --- a/src/entity/ai/MobController.php +++ b/src/entity/ai/MobController.php @@ -6,6 +6,10 @@ class MobController * @var Entity */ public $entity; + + public $finalYaw, $finalPitch, $finalHeadYaw; + + public function __construct($e){ $this->entity = $e; } @@ -36,16 +40,19 @@ class MobController } } } - + $this->faceEntity($this->entity->add($ox, $oy, $oz)); if($this->entity->knockbackTime <= 0){ $this->entity->moveEntityWithOffset($ox, $oy, $oz); } - - - $this->faceEntity($this->entity->add($ox, $oy, $oz)); return true; } + public function rotateTick(){ //TODO handle more rotation + $w180 = Utils::wrapAngleTo180($this->finalHeadYaw - $this->entity->headYaw); + $w180min = min(abs($w180), 20)*Utils::getSign($w180); + $this->entity->headYaw = Utils::wrapAngleTo360($this->entity->headYaw + $w180min); + } + public function moveTo($x, $y, $z){ return $this->moveNonInstant($x - floor($this->entity->x), $y - floor($this->entity->y), $z - floor($this->entity->z)); } @@ -58,7 +65,7 @@ class MobController $tan = $dz == 0 ? ($dx < 0 ? 180 : 0) : (90 - rad2deg(atan($dx / $dz))); $thetaOffset = $dz < 0 ? 90 : 270; $calcYaw = ($thetaOffset + $tan); - $this->entity->headYaw = $this->entity->yaw = $calcYaw; + $this->finalHeadYaw = $this->entity->yaw = $calcYaw; } public function lookOffset($x, $y, $z, $pitch = true){ @@ -66,7 +73,7 @@ class MobController $thetaOffset = $z < 0 ? 90 : 270; $calcYaw = $tan + $thetaOffset; - $this->entity->yaw = $this->entity->headYaw = $calcYaw; + $this->entity->yaw = $this->finalHeadYaw = $calcYaw; if($pitch){ $diff = sqrt($x * $x + $z * $z); diff --git a/src/network/protocol/packet/RotateHeadPacket.php b/src/network/protocol/packet/RotateHeadPacket.php index ea1ea298..21cc7cf0 100644 --- a/src/network/protocol/packet/RotateHeadPacket.php +++ b/src/network/protocol/packet/RotateHeadPacket.php @@ -23,7 +23,7 @@ class RotateHeadPacket extends RakNetDataPacket{ public function encode(){ $this->reset(); $this->putInt($this->eid); - @$this->putByte($this->rawYaw ? $this->yaw : $this->yaw / 360 / 0.0039062); //dont ask me why does chr count float TODO find a better way + $this->putByte($this->rawYaw ? $this->yaw : ($this->yaw / 360 / 0.0039062)); //wraps 360 angle to 0xff } } \ No newline at end of file diff --git a/src/utils/Utils.php b/src/utils/Utils.php index 538324b6..d933d319 100644 --- a/src/utils/Utils.php +++ b/src/utils/Utils.php @@ -39,12 +39,16 @@ class Utils{ return $id; } - public static function wrapAngleTo180_float($par0) + public static function wrapAngleTo360($angle) { - $par0 %= 360.0; - return $par0 >= 180 ? $par0 - 360 : (($par0 < -180) ? ($par0 + 360) : $par0); + $angle %= 360; + return $angle < 0 ? $angle + 360 : $angle; + } + public static function wrapAngleTo180($angle) + { + $angle = fmod($angle+180, 360); + return $angle < 0 ? $angle + 360 : $angle - 180; } - public static function getSign($v){ return $v > 0 ? 1 : ($v < 0 ? -1 : 0); }