From fc334e7c993170b0230dfa8f7f2784c67d80d64a Mon Sep 17 00:00:00 2001 From: GameHerobrine Date: Thu, 15 Feb 2024 04:34:31 +0300 Subject: [PATCH] better collisions between entities --- src/Player.php | 2 +- src/PocketMinecraftServer.php | 1 + src/entity/Living.php | 23 +++++++++++------------ src/entity/monster/Zombie.php | 2 +- 4 files changed, 14 insertions(+), 14 deletions(-) diff --git a/src/Player.php b/src/Player.php index 5d2276d1..01ef1a90 100755 --- a/src/Player.php +++ b/src/Player.php @@ -1216,7 +1216,7 @@ class Player{ $moveSent = false; $headSent = false; if($e->speedX != 0 || $e->speedY != 0 || $e->speedZ != 0 || $e->speedY != $e->lastSpeedY || $e->speedX != $e->lastSpeedX || $e->speedZ != $e->lastSpeedZ){ - if(!($e->speedY < 0 && $e->onGround)){ + if(!($e->speedY < 0 && $e->onGround) || $e->speedX != 0 || $e->speedZ != 0 || $e->speedY != $e->lastSpeedY || $e->speedX != $e->lastSpeedX || $e->speedZ != $e->lastSpeedZ){ $motion = new SetEntityMotionPacket(); $motion->eid = $e->eid; $motion->speedX = $e->speedX; diff --git a/src/PocketMinecraftServer.php b/src/PocketMinecraftServer.php index bc2c0d7f..979596cc 100644 --- a/src/PocketMinecraftServer.php +++ b/src/PocketMinecraftServer.php @@ -622,6 +622,7 @@ class PocketMinecraftServer{ try{ $return = @call_user_func($this->schedule[$cid][0] ?? function(){}, $this->schedule[$cid][1], $this->schedule[$cid][2]); //somehow args can be null }catch(TypeError $e){ + ConsoleAPI::error($e->getTraceAsString()); $return = false; } }else{ diff --git a/src/entity/Living.php b/src/entity/Living.php index c1f09d43..3828711c 100644 --- a/src/entity/Living.php +++ b/src/entity/Living.php @@ -63,11 +63,11 @@ abstract class Living extends Entity implements Pathfindable{ return true; } public function collideHandler(){ - $rd = ceil($this->radius); - $minChunkX = ((int)($this->x - $rd)) >> 4; - $minChunkZ = ((int)($this->z - $rd)) >> 4; - $maxChunkX = ((int)($this->x + $rd)) >> 4; - $maxChunkZ = ((int)($this->z + $rd)) >> 4; + $bb = $this->boundingBox->expand(0.2, 0, 0.2); + $minChunkX = ((int)($bb->minX)) >> 4; + $minChunkZ = ((int)($bb->minZ)) >> 4; + $maxChunkX = ((int)($bb->minX)) >> 4; + $maxChunkZ = ((int)($bb->minZ)) >> 4; //TODO also index by chunkY? for($chunkX = $minChunkX; $chunkX <= $maxChunkX; ++$chunkX){ @@ -75,7 +75,9 @@ abstract class Living extends Entity implements Pathfindable{ $ind = "$chunkX $chunkZ"; foreach($this->level->entityListPositioned[$ind] ?? [] as $entid){ if($this->level->entityList[$entid] instanceof Entity){ - $this->applyCollision($this->level->entityList[$entid]); + if($bb->intersectsWith($this->level->entityList[$entid]->boundingBox)){ + $this->applyCollision($this->level->entityList[$entid]); + } } } } @@ -83,7 +85,7 @@ abstract class Living extends Entity implements Pathfindable{ } public function applyCollision(Entity $collided){ - if($collided->boundingBox->intersectsWith($this->boundingBox) && !($this->isPlayer() && $collided->isPlayer()) && $this->eid != $collided->eid){ + if(!($this->isPlayer() && $collided->isPlayer()) && $this->eid != $collided->eid){ $diffX = $collided->x - $this->x; $diffZ = $collided->z - $this->z; $maxDiff = max(abs($diffX), abs($diffZ)); @@ -154,15 +156,12 @@ abstract class Living extends Entity implements Pathfindable{ $this->landMovementFactor *= $this->getSpeedModifer(); $this->moveEntityWithHeading($this->moveStrafing, $this->moveForward); $this->landMovementFactor = $savedLandFactor; + //Yaw rotation in 1.5 is handled in a bit different place but hopefully this will work too + $this->ai->mobController->updateHeadYaw(); if(self::$entityPushing){ $this->collideHandler(); } - - //Yaw rotation in 1.5 is handled in a bit different place but hopefully this will work too - $this->ai->mobController->updateHeadYaw(); - - } public function jump(){ diff --git a/src/entity/monster/Zombie.php b/src/entity/monster/Zombie.php index 55e3d51e..491ee857 100644 --- a/src/entity/monster/Zombie.php +++ b/src/entity/monster/Zombie.php @@ -19,7 +19,7 @@ class Zombie extends Monster{ } public function updateBurning(){ - if($this->fire > 0 or !$this->level->isDay() || $this->inWater){ //TODO fix burning in water + if($this->fire > 0 || !$this->level->isDay() || $this->inWater){ //TODO fix burning in water return false; }