mirror of
https://github.com/Minestom/Minestom.git
synced 2025-01-06 00:17:58 +01:00
Fixed non-smooth pathfinding by setting the velocity of the creature, therefore allowing the client to predict the position and smooth the path
This commit is contained in:
parent
7b947ba09b
commit
49b4f0c79d
@ -53,7 +53,7 @@ public class SimpleCommand implements CommandProcessor {
|
|||||||
|
|
||||||
Instance instance = player.getInstance();
|
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.setInstance(instance);
|
||||||
|
|
||||||
chickenCreature.setPathTo(player.getPosition());
|
chickenCreature.setPathTo(player.getPosition());
|
||||||
|
@ -428,7 +428,7 @@ public abstract class Entity implements Viewable, EventHandler, DataContainer {
|
|||||||
sendSynchronization();
|
sendSynchronization();
|
||||||
|
|
||||||
if (shouldSendVelocityUpdate(time)) {
|
if (shouldSendVelocityUpdate(time)) {
|
||||||
sendPacketToViewersAndSelf(getVelocityPacket());
|
sendVelocityPacket();
|
||||||
lastVelocityUpdateTime = time;
|
lastVelocityUpdateTime = time;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -482,6 +482,13 @@ public abstract class Entity implements Viewable, EventHandler, DataContainer {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Equivalent to <code>sendPacketsToViewers(getVelocityPacket());</code>
|
||||||
|
*/
|
||||||
|
public void sendVelocityPacket() {
|
||||||
|
sendPacketsToViewers(getVelocityPacket());
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Get the number of ticks this entity has been active for
|
* Get the number of ticks this entity has been active for
|
||||||
*
|
*
|
||||||
|
@ -2,6 +2,7 @@ package net.minestom.server.entity;
|
|||||||
|
|
||||||
import com.extollit.gaming.ai.path.HydrazinePathFinder;
|
import com.extollit.gaming.ai.path.HydrazinePathFinder;
|
||||||
import com.extollit.gaming.ai.path.model.PathObject;
|
import com.extollit.gaming.ai.path.model.PathObject;
|
||||||
|
import net.minestom.server.MinecraftServer;
|
||||||
import net.minestom.server.attribute.Attribute;
|
import net.minestom.server.attribute.Attribute;
|
||||||
import net.minestom.server.collision.CollisionUtils;
|
import net.minestom.server.collision.CollisionUtils;
|
||||||
import net.minestom.server.entity.pathfinding.PFPathingEntity;
|
import net.minestom.server.entity.pathfinding.PFPathingEntity;
|
||||||
@ -50,8 +51,6 @@ public abstract class EntityCreature extends LivingEntity {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void update(long time) {
|
public void update(long time) {
|
||||||
super.update(time);
|
|
||||||
|
|
||||||
// Path finding
|
// Path finding
|
||||||
path = pathFinder.update();
|
path = pathFinder.update();
|
||||||
if (path != null) {
|
if (path != null) {
|
||||||
@ -61,12 +60,11 @@ public abstract class EntityCreature extends LivingEntity {
|
|||||||
} else {
|
} else {
|
||||||
final float speed = getAttributeValue(Attribute.MOVEMENT_SPEED);
|
final float speed = getAttributeValue(Attribute.MOVEMENT_SPEED);
|
||||||
Position targetPosition = pathingEntity.getTargetPosition();
|
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);
|
moveTowards(targetPosition, speed);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
super.update(time);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@ -306,12 +304,23 @@ public abstract class EntityCreature extends LivingEntity {
|
|||||||
final float currentZ = position.getZ();
|
final float currentZ = position.getZ();
|
||||||
final float targetX = direction.getX();
|
final float targetX = direction.getX();
|
||||||
final float targetZ = direction.getZ();
|
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 speedX = (float) (Math.cos(radians) * speed);
|
||||||
final float speedZ = (float) (Math.sin(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) {
|
private ItemStack getEquipmentItem(ItemStack itemStack, ArmorEquipEvent.ArmorSlot armorSlot) {
|
||||||
|
Loading…
Reference in New Issue
Block a user