From 49b4f0c79d70c156becf41060a07a0934434b007 Mon Sep 17 00:00:00 2001 From: jglrxavpok Date: Fri, 31 Jul 2020 18:55:08 +0200 Subject: [PATCH 1/3] Fixed non-smooth pathfinding by setting the velocity of the creature, therefore allowing the client to predict the position and smooth the path --- .../themode/demo/commands/SimpleCommand.java | 2 +- .../net/minestom/server/entity/Entity.java | 9 +++++++- .../server/entity/EntityCreature.java | 23 +++++++++++++------ 3 files changed, 25 insertions(+), 9 deletions(-) diff --git a/src/main/java/fr/themode/demo/commands/SimpleCommand.java b/src/main/java/fr/themode/demo/commands/SimpleCommand.java index 9f3b0d65b..4ba29dd75 100644 --- a/src/main/java/fr/themode/demo/commands/SimpleCommand.java +++ b/src/main/java/fr/themode/demo/commands/SimpleCommand.java @@ -53,7 +53,7 @@ public class SimpleCommand implements CommandProcessor { Instance instance = player.getInstance(); - ChickenCreature chickenCreature = new ChickenCreature(new Position(-10, 40, -10)); + ChickenCreature chickenCreature = new ChickenCreature(new Position(-10, 43, -10)); chickenCreature.setInstance(instance); chickenCreature.setPathTo(player.getPosition()); diff --git a/src/main/java/net/minestom/server/entity/Entity.java b/src/main/java/net/minestom/server/entity/Entity.java index 192ee0736..8d9a06d1c 100644 --- a/src/main/java/net/minestom/server/entity/Entity.java +++ b/src/main/java/net/minestom/server/entity/Entity.java @@ -428,7 +428,7 @@ public abstract class Entity implements Viewable, EventHandler, DataContainer { sendSynchronization(); if (shouldSendVelocityUpdate(time)) { - sendPacketToViewersAndSelf(getVelocityPacket()); + sendVelocityPacket(); lastVelocityUpdateTime = time; } } @@ -482,6 +482,13 @@ public abstract class Entity implements Viewable, EventHandler, DataContainer { } } + /** + * Equivalent to sendPacketsToViewers(getVelocityPacket()); + */ + public void sendVelocityPacket() { + sendPacketsToViewers(getVelocityPacket()); + } + /** * Get the number of ticks this entity has been active for * diff --git a/src/main/java/net/minestom/server/entity/EntityCreature.java b/src/main/java/net/minestom/server/entity/EntityCreature.java index 7176f0020..0001cd3cc 100644 --- a/src/main/java/net/minestom/server/entity/EntityCreature.java +++ b/src/main/java/net/minestom/server/entity/EntityCreature.java @@ -2,6 +2,7 @@ package net.minestom.server.entity; import com.extollit.gaming.ai.path.HydrazinePathFinder; import com.extollit.gaming.ai.path.model.PathObject; +import net.minestom.server.MinecraftServer; import net.minestom.server.attribute.Attribute; import net.minestom.server.collision.CollisionUtils; import net.minestom.server.entity.pathfinding.PFPathingEntity; @@ -50,8 +51,6 @@ public abstract class EntityCreature extends LivingEntity { @Override public void update(long time) { - super.update(time); - // Path finding path = pathFinder.update(); if (path != null) { @@ -61,12 +60,11 @@ public abstract class EntityCreature extends LivingEntity { } else { final float speed = getAttributeValue(Attribute.MOVEMENT_SPEED); Position targetPosition = pathingEntity.getTargetPosition(); - //targetPosition = new Position(-5.5f, 40f, -5.5f); - //System.out.println("target: " + targetPosition + " : " + (System.currentTimeMillis() - time)); - //System.out.println("current: " + getPosition()); moveTowards(targetPosition, speed); } } + + super.update(time); } @Override @@ -306,12 +304,23 @@ public abstract class EntityCreature extends LivingEntity { final float currentZ = position.getZ(); final float targetX = direction.getX(); final float targetZ = direction.getZ(); + final float dz = targetZ - currentZ; + final float dx = targetX - currentX; - final float radians = (float) Math.atan2(targetZ - currentZ, targetX - currentX); + // the purpose of these few lines is to slow down entities when they reach their destination + float distSquared = dx * dx + dz * dz; + if(speed > distSquared) { + speed = distSquared; + } + + final float radians = (float) Math.atan2(dz, dx); final float speedX = (float) (Math.cos(radians) * speed); final float speedZ = (float) (Math.sin(radians) * speed); - move(speedX, 0, speedZ, true); + // TODO: is a hard set an issue if there are other external forces at play? + final float tps = MinecraftServer.TICK_PER_SECOND; + velocity.setX(speedX * tps); + velocity.setZ(speedZ * tps); } private ItemStack getEquipmentItem(ItemStack itemStack, ArmorEquipEvent.ArmorSlot armorSlot) { From 628e87e531f4e6fc3576567cf557904c07e42c73 Mon Sep 17 00:00:00 2001 From: jglrxavpok Date: Fri, 31 Jul 2020 18:57:37 +0200 Subject: [PATCH 2/3] Potential deprecation of EntityCreature#move --- src/main/java/net/minestom/server/entity/EntityCreature.java | 1 + 1 file changed, 1 insertion(+) diff --git a/src/main/java/net/minestom/server/entity/EntityCreature.java b/src/main/java/net/minestom/server/entity/EntityCreature.java index 0001cd3cc..6d92c27d4 100644 --- a/src/main/java/net/minestom/server/entity/EntityCreature.java +++ b/src/main/java/net/minestom/server/entity/EntityCreature.java @@ -80,6 +80,7 @@ public abstract class EntityCreature extends LivingEntity { * @param updateView should the entity move its head toward the position? */ public void move(float x, float y, float z, boolean updateView) { + // TODO: remove ? Entity#tick already performs this behaviour, and syncs it properly final Position position = getPosition(); Position newPosition = new Position(); // Calculate collisions boxes From 322194c5e42b337e86eb8b11ee18e2b5be37a9b4 Mon Sep 17 00:00:00 2001 From: jglrxavpok Date: Fri, 31 Jul 2020 21:02:37 +0200 Subject: [PATCH 3/3] Make entities look along their path --- .../net/minestom/server/entity/EntityCreature.java | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/src/main/java/net/minestom/server/entity/EntityCreature.java b/src/main/java/net/minestom/server/entity/EntityCreature.java index 6d92c27d4..df4387272 100644 --- a/src/main/java/net/minestom/server/entity/EntityCreature.java +++ b/src/main/java/net/minestom/server/entity/EntityCreature.java @@ -295,6 +295,7 @@ public abstract class EntityCreature extends LivingEntity { /** * Used to move the entity toward {@code direction} in the X and Z axis * Gravity is still applied but the entity will not attempt to jump + * Also update the yaw/pitch of the entity to look along 'direction' * * @param direction the targeted position * @param speed define how far the entity will move @@ -318,12 +319,23 @@ public abstract class EntityCreature extends LivingEntity { final float speedX = (float) (Math.cos(radians) * speed); final float speedZ = (float) (Math.sin(radians) * speed); + lookAlong(dx, direction.getY(), dz); + // TODO: is a hard set an issue if there are other external forces at play? final float tps = MinecraftServer.TICK_PER_SECOND; velocity.setX(speedX * tps); velocity.setZ(speedZ * tps); } + private void lookAlong(float dx, float dy, float dz) { + final float horizontalAngle = (float) Math.atan2(dz, dx); + final float yaw = (float) (horizontalAngle * (180.0 / Math.PI)) - 90; + final float pitch = (float) Math.atan2(dy, Math.max(Math.abs(dx), Math.abs(dz))); + + getPosition().setYaw(yaw); + getPosition().setPitch(pitch); + } + private ItemStack getEquipmentItem(ItemStack itemStack, ArmorEquipEvent.ArmorSlot armorSlot) { itemStack = ItemStackUtils.notNull(itemStack);