Added gravity acceleration and terminal velocity (and an utils method to get the gravity tick count)

This commit is contained in:
Felix Cravic 2020-12-06 01:36:37 +01:00
parent d5e5412309
commit 70000e75d5
5 changed files with 68 additions and 8 deletions

View File

@ -87,6 +87,9 @@ public abstract class Entity implements Viewable, EventHandler, DataContainer, P
private long velocityUpdatePeriod; private long velocityUpdatePeriod;
protected float gravityDragPerTick; protected float gravityDragPerTick;
protected float gravityAcceleration;
protected float gravityTerminalVelocity;
protected float gravityTickCount; // Number of tick where gravity tick was applied
protected float eyeHeight; protected float eyeHeight;
private boolean autoViewable; private boolean autoViewable;
@ -475,8 +478,24 @@ public abstract class Entity implements Viewable, EventHandler, DataContainer, P
final float newZ = position.getZ() + velocity.getZ() / tps; final float newZ = position.getZ() + velocity.getZ() / tps;
Position newPosition = new Position(newX, newY, newZ); Position newPosition = new Position(newX, newY, newZ);
if (!noGravity) { // Gravity
velocity.setY(velocity.getY() - gravityDragPerTick * tps); {
// 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(); Vector newVelocityOut = new Vector();
@ -782,13 +801,54 @@ public abstract class Entity implements Viewable, EventHandler, DataContainer, P
velocity.getZ() != 0; 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. * 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 <a href="https://minecraft.gamepedia.com/Entity#Motion_of_entities">Entities motion</a>
*/ */
public void setGravity(float gravityDragPerTick) { public void setGravity(float gravityDragPerTick, float gravityAcceleration, float gravityTerminalVelocity) {
this.gravityDragPerTick = gravityDragPerTick; this.gravityDragPerTick = gravityDragPerTick;
this.gravityAcceleration = gravityAcceleration;
this.gravityTerminalVelocity = gravityTerminalVelocity;
} }
/** /**

View File

@ -13,7 +13,7 @@ public class ExperienceOrb extends Entity {
public ExperienceOrb(short experienceCount, @NotNull Position spawnPosition) { public ExperienceOrb(short experienceCount, @NotNull Position spawnPosition) {
super(EntityType.EXPERIENCE_ORB, spawnPosition); super(EntityType.EXPERIENCE_ORB, spawnPosition);
setGravity(0.02f); setGravity(0.02f, 0.04f, 1.96f);
setBoundingBox(0.5f, 0.5f, 0.5f); setBoundingBox(0.5f, 0.5f, 0.5f);
this.experienceCount = experienceCount; this.experienceCount = experienceCount;
} }

View File

@ -80,7 +80,7 @@ public abstract class LivingEntity extends Entity implements EquipmentHandler {
public LivingEntity(@NotNull EntityType entityType, @NotNull Position spawnPosition) { public LivingEntity(@NotNull EntityType entityType, @NotNull Position spawnPosition) {
super(entityType, spawnPosition); super(entityType, spawnPosition);
setupAttributes(); setupAttributes();
setGravity(0.02f); setGravity(0.02f, 0.08f, 3.92f);
} }
public LivingEntity(@NotNull EntityType entityType) { public LivingEntity(@NotNull EntityType entityType) {

View File

@ -9,7 +9,7 @@ public abstract class ObjectEntity extends Entity {
public ObjectEntity(@NotNull EntityType entityType, @NotNull Position spawnPosition) { public ObjectEntity(@NotNull EntityType entityType, @NotNull Position spawnPosition) {
super(entityType, spawnPosition); super(entityType, spawnPosition);
setGravity(0.02f); setGravity(0.02f, 0.04f, 1.96f);
} }
/** /**

View File

@ -23,7 +23,7 @@ public class EntityItemFrame extends ObjectEntity {
this.orientation = orientation; this.orientation = orientation;
this.rotation = Rotation.NONE; this.rotation = Rotation.NONE;
setNoGravity(true); setNoGravity(true);
setGravity(0f); setGravity(0f, 0f, 0f);
} }
@NotNull @NotNull