From 70000e75d522a0240ad4b50c55986201840e2e07 Mon Sep 17 00:00:00 2001 From: Felix Cravic Date: Sun, 6 Dec 2020 01:36:37 +0100 Subject: [PATCH] Added gravity acceleration and terminal velocity (and an utils method to get the gravity tick count) --- .../net/minestom/server/entity/Entity.java | 68 +++++++++++++++++-- .../minestom/server/entity/ExperienceOrb.java | 2 +- .../minestom/server/entity/LivingEntity.java | 2 +- .../minestom/server/entity/ObjectEntity.java | 2 +- .../type/decoration/EntityItemFrame.java | 2 +- 5 files changed, 68 insertions(+), 8 deletions(-) diff --git a/src/main/java/net/minestom/server/entity/Entity.java b/src/main/java/net/minestom/server/entity/Entity.java index f97f58346..bb5d8d7af 100644 --- a/src/main/java/net/minestom/server/entity/Entity.java +++ b/src/main/java/net/minestom/server/entity/Entity.java @@ -87,6 +87,9 @@ public abstract class Entity implements Viewable, EventHandler, DataContainer, P private long velocityUpdatePeriod; protected float gravityDragPerTick; + protected float gravityAcceleration; + protected float gravityTerminalVelocity; + protected float gravityTickCount; // Number of tick where gravity tick was applied protected float eyeHeight; private boolean autoViewable; @@ -475,8 +478,24 @@ public abstract class Entity implements Viewable, EventHandler, DataContainer, P final float newZ = position.getZ() + velocity.getZ() / tps; Position newPosition = new Position(newX, newY, newZ); - if (!noGravity) { - velocity.setY(velocity.getY() - gravityDragPerTick * tps); + // Gravity + { + // Cache the number of "gravity tick" + if (!isOnGround()) { + gravityTickCount++; + } else { + gravityTickCount = 0; + } + + // Compute the gravity change (drag per tick + acceleration) + final float gravityY = Math.min( + -gravityDragPerTick - (gravityAcceleration * gravityTickCount), + gravityTerminalVelocity); + + // Change velocity to apply gravity + if (!noGravity) { + velocity.setY(velocity.getY() + gravityY); + } } Vector newVelocityOut = new Vector(); @@ -782,13 +801,54 @@ public abstract class Entity implements Viewable, EventHandler, DataContainer, P velocity.getZ() != 0; } + /** + * Gets the gravity drag per tick. + * + * @return the gravity drag per tick in block + */ + public float getGravityDragPerTick() { + return gravityDragPerTick; + } + + /** + * Gets the gravity acceleration. + * + * @return the gravity acceleration in block + */ + public float getGravityAcceleration() { + return gravityAcceleration; + } + + /** + * Gets the maximum gravity velocity. + * + * @return the maximum gravity velocity in block + */ + public float getGravityTerminalVelocity() { + return gravityTerminalVelocity; + } + + /** + * Gets the number of tick this entity has been applied gravity. + * + * @return the number of tick of which gravity has been consequently applied + */ + public float getGravityTickCount() { + return gravityTickCount; + } + /** * Changes the gravity of the entity. * - * @param gravityDragPerTick the gravity drag per tick + * @param gravityDragPerTick the gravity drag per tick in block + * @param gravityAcceleration the gravity acceleration in block + * @param gravityTerminalVelocity the gravity terminal velocity (maximum) in block + * @see Entities motion */ - public void setGravity(float gravityDragPerTick) { + public void setGravity(float gravityDragPerTick, float gravityAcceleration, float gravityTerminalVelocity) { this.gravityDragPerTick = gravityDragPerTick; + this.gravityAcceleration = gravityAcceleration; + this.gravityTerminalVelocity = gravityTerminalVelocity; } /** diff --git a/src/main/java/net/minestom/server/entity/ExperienceOrb.java b/src/main/java/net/minestom/server/entity/ExperienceOrb.java index 43de9534c..63627f1be 100644 --- a/src/main/java/net/minestom/server/entity/ExperienceOrb.java +++ b/src/main/java/net/minestom/server/entity/ExperienceOrb.java @@ -13,7 +13,7 @@ public class ExperienceOrb extends Entity { public ExperienceOrb(short experienceCount, @NotNull Position spawnPosition) { super(EntityType.EXPERIENCE_ORB, spawnPosition); - setGravity(0.02f); + setGravity(0.02f, 0.04f, 1.96f); setBoundingBox(0.5f, 0.5f, 0.5f); this.experienceCount = experienceCount; } diff --git a/src/main/java/net/minestom/server/entity/LivingEntity.java b/src/main/java/net/minestom/server/entity/LivingEntity.java index 30ce0aa0d..f842cfe2f 100644 --- a/src/main/java/net/minestom/server/entity/LivingEntity.java +++ b/src/main/java/net/minestom/server/entity/LivingEntity.java @@ -80,7 +80,7 @@ public abstract class LivingEntity extends Entity implements EquipmentHandler { public LivingEntity(@NotNull EntityType entityType, @NotNull Position spawnPosition) { super(entityType, spawnPosition); setupAttributes(); - setGravity(0.02f); + setGravity(0.02f, 0.08f, 3.92f); } public LivingEntity(@NotNull EntityType entityType) { diff --git a/src/main/java/net/minestom/server/entity/ObjectEntity.java b/src/main/java/net/minestom/server/entity/ObjectEntity.java index 7f0459273..11083c20c 100644 --- a/src/main/java/net/minestom/server/entity/ObjectEntity.java +++ b/src/main/java/net/minestom/server/entity/ObjectEntity.java @@ -9,7 +9,7 @@ public abstract class ObjectEntity extends Entity { public ObjectEntity(@NotNull EntityType entityType, @NotNull Position spawnPosition) { super(entityType, spawnPosition); - setGravity(0.02f); + setGravity(0.02f, 0.04f, 1.96f); } /** diff --git a/src/main/java/net/minestom/server/entity/type/decoration/EntityItemFrame.java b/src/main/java/net/minestom/server/entity/type/decoration/EntityItemFrame.java index 23f81f7df..bffb4690c 100644 --- a/src/main/java/net/minestom/server/entity/type/decoration/EntityItemFrame.java +++ b/src/main/java/net/minestom/server/entity/type/decoration/EntityItemFrame.java @@ -23,7 +23,7 @@ public class EntityItemFrame extends ObjectEntity { this.orientation = orientation; this.rotation = Rotation.NONE; setNoGravity(true); - setGravity(0f); + setGravity(0f, 0f, 0f); } @NotNull