tweaks to headYaw rotation, so it should be nearly perfect now

This commit is contained in:
GameHerobrine 2023-02-23 00:04:25 +03:00
parent a5a0c0e000
commit e4d1898613
5 changed files with 26 additions and 16 deletions

View file

@ -733,6 +733,9 @@ class Entity extends Position
if($this->closed === true){ if($this->closed === true){
return false; return false;
} }
if($this->lastHeadYaw != $this->headYaw){
$this->sendHeadYaw();
}
$now = microtime(true); $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->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){ if($this->class === ENTITY_PLAYER or ($this->last[5] + 8) < $now){
@ -770,15 +773,10 @@ class Entity extends Position
} }
} else{ } else{
$this->updatePosition(); $this->updatePosition();
if($this->lastHeadYaw != $this->headYaw){
$this->sendHeadYaw();
}
$this->updateLast(); $this->updateLast();
} }
} }
$this->lastUpdate = $now; $this->lastUpdate = $now;
} }

View file

@ -33,6 +33,7 @@ abstract class Living extends Entity implements Damageable, Pathfindable{
if(!$this->dead && Entity::$allowedAI && $this->idleTime <= 0) { if(!$this->dead && Entity::$allowedAI && $this->idleTime <= 0) {
$this->ai->updateTasks(); $this->ai->updateTasks();
} }
$this->ai->mobController->rotateTick();
if($this->onGround){ if($this->onGround){
//if(!$this->hasPath() && $this->pathFinder instanceof ITileNavigator){ //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); // $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);

View file

@ -6,6 +6,10 @@ class MobController
* @var Entity * @var Entity
*/ */
public $entity; public $entity;
public $finalYaw, $finalPitch, $finalHeadYaw;
public function __construct($e){ public function __construct($e){
$this->entity = $e; $this->entity = $e;
} }
@ -36,16 +40,19 @@ class MobController
} }
} }
} }
$this->faceEntity($this->entity->add($ox, $oy, $oz));
if($this->entity->knockbackTime <= 0){ if($this->entity->knockbackTime <= 0){
$this->entity->moveEntityWithOffset($ox, $oy, $oz); $this->entity->moveEntityWithOffset($ox, $oy, $oz);
} }
$this->faceEntity($this->entity->add($ox, $oy, $oz));
return true; 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){ public function moveTo($x, $y, $z){
return $this->moveNonInstant($x - floor($this->entity->x), $y - floor($this->entity->y), $z - floor($this->entity->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))); $tan = $dz == 0 ? ($dx < 0 ? 180 : 0) : (90 - rad2deg(atan($dx / $dz)));
$thetaOffset = $dz < 0 ? 90 : 270; $thetaOffset = $dz < 0 ? 90 : 270;
$calcYaw = ($thetaOffset + $tan); $calcYaw = ($thetaOffset + $tan);
$this->entity->headYaw = $this->entity->yaw = $calcYaw; $this->finalHeadYaw = $this->entity->yaw = $calcYaw;
} }
public function lookOffset($x, $y, $z, $pitch = true){ public function lookOffset($x, $y, $z, $pitch = true){
@ -66,7 +73,7 @@ class MobController
$thetaOffset = $z < 0 ? 90 : 270; $thetaOffset = $z < 0 ? 90 : 270;
$calcYaw = $tan + $thetaOffset; $calcYaw = $tan + $thetaOffset;
$this->entity->yaw = $this->entity->headYaw = $calcYaw; $this->entity->yaw = $this->finalHeadYaw = $calcYaw;
if($pitch){ if($pitch){
$diff = sqrt($x * $x + $z * $z); $diff = sqrt($x * $x + $z * $z);

View file

@ -23,7 +23,7 @@ class RotateHeadPacket extends RakNetDataPacket{
public function encode(){ public function encode(){
$this->reset(); $this->reset();
$this->putInt($this->eid); $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
} }
} }

View file

@ -39,12 +39,16 @@ class Utils{
return $id; return $id;
} }
public static function wrapAngleTo180_float($par0) public static function wrapAngleTo360($angle)
{ {
$par0 %= 360.0; $angle %= 360;
return $par0 >= 180 ? $par0 - 360 : (($par0 < -180) ? ($par0 + 360) : $par0); 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){ public static function getSign($v){
return $v > 0 ? 1 : ($v < 0 ? -1 : 0); return $v > 0 ? 1 : ($v < 0 ? -1 : 0);
} }