Fix onGround field and gravityTickCount

This commit is contained in:
themode 2021-01-04 03:04:45 +01:00
parent f2f9cbc5fa
commit 7019b7da85
6 changed files with 50 additions and 41 deletions

View File

@ -26,7 +26,10 @@ public class CollisionUtils {
* @param velocityOut the Vector object in which the new velocity will be saved * @param velocityOut the Vector object in which the new velocity will be saved
* @return whether this entity is on the ground * @return whether this entity is on the ground
*/ */
public static boolean handlePhysics(Entity entity, Vector deltaPosition, Position positionOut, Vector velocityOut) { public static boolean handlePhysics(@NotNull Entity entity,
@NotNull Vector deltaPosition,
@NotNull Position positionOut,
@NotNull Vector velocityOut) {
// TODO handle collisions with nearby entities (should it be done here?) // TODO handle collisions with nearby entities (should it be done here?)
final Instance instance = entity.getInstance(); final Instance instance = entity.getInstance();
final Position currentPosition = entity.getPosition(); final Position currentPosition = entity.getPosition();
@ -75,7 +78,7 @@ public class CollisionUtils {
* @param stepAmount how much to step in the direction (in blocks) * @param stepAmount how much to step in the direction (in blocks)
* @param positionOut the vector in which to store the new position * @param positionOut the vector in which to store the new position
* @param corners the corners to check against * @param corners the corners to check against
* @return true iif a collision has been found * @return true if a collision has been found
*/ */
private static boolean stepAxis(Instance instance, Vector startPosition, Vector axis, float stepAmount, Vector positionOut, Vector... corners) { private static boolean stepAxis(Instance instance, Vector startPosition, Vector axis, float stepAmount, Vector positionOut, Vector... corners) {
positionOut.copy(startPosition); positionOut.copy(startPosition);

View File

@ -453,6 +453,13 @@ public abstract class Entity implements Viewable, EventHandler, DataContainer, P
// Entity tick // Entity tick
{ {
// Cache the number of "gravity tick"
if (!onGround) {
gravityTickCount++;
} else {
gravityTickCount = 0;
}
// Velocity // Velocity
boolean applyVelocity; boolean applyVelocity;
// Non-player entities with either velocity or gravity enabled // Non-player entities with either velocity or gravity enabled
@ -467,32 +474,19 @@ 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);
// Gravity
{
// Cache the number of "gravity tick"
if (!onGround) {
gravityTickCount++;
} else {
gravityTickCount = 0;
}
// Compute the gravity change (drag per tick + acceleration)
final float gravityY = Math.min(
gravityDragPerTick + (gravityAcceleration * (float) gravityTickCount),
gravityTerminalVelocity);
// Change velocity to apply gravity
if (!noGravity) {
velocity.setY(velocity.getY() - gravityY);
}
}
Vector newVelocityOut = new Vector(); Vector newVelocityOut = new Vector();
// Gravity force
final float gravityY = !noGravity ? Math.min(
gravityDragPerTick + (gravityAcceleration * (float) gravityTickCount),
gravityTerminalVelocity) : 0f;
final Vector deltaPos = new Vector( final Vector deltaPos = new Vector(
getVelocity().getX() / tps, getVelocity().getX() / tps,
getVelocity().getY() / tps, (getVelocity().getY() - gravityY) / tps,
getVelocity().getZ() / tps getVelocity().getZ() / tps
); );
this.onGround = CollisionUtils.handlePhysics(this, deltaPos, newPosition, newVelocityOut); this.onGround = CollisionUtils.handlePhysics(this, deltaPos, newPosition, newVelocityOut);
// Stop here if the position is the same // Stop here if the position is the same
@ -535,7 +529,7 @@ public abstract class Entity implements Viewable, EventHandler, DataContainer, P
// Stop player velocity // Stop player velocity
if (PlayerUtils.isNettyClient(this)) { if (PlayerUtils.isNettyClient(this)) {
velocity.zero(); this.velocity.zero();
} }
} else { } else {
drag = 0.98f; // air drag drag = 0.98f; // air drag

View File

@ -130,7 +130,6 @@ public class Player extends LivingEntity implements CommandSender {
protected final Set<Chunk> viewableChunks = new CopyOnWriteArraySet<>(); protected final Set<Chunk> viewableChunks = new CopyOnWriteArraySet<>();
private final AtomicInteger teleportId = new AtomicInteger(); private final AtomicInteger teleportId = new AtomicInteger();
protected boolean onGround;
private final Queue<ClientPlayPacket> packets = Queues.newConcurrentLinkedQueue(); private final Queue<ClientPlayPacket> packets = Queues.newConcurrentLinkedQueue();
private final boolean levelFlat; private final boolean levelFlat;
private final PlayerSettings settings; private final PlayerSettings settings;

View File

@ -52,7 +52,6 @@ public class PlayerPositionListener {
private static void processMovement(@NotNull Player player, float x, float y, float z, private static void processMovement(@NotNull Player player, float x, float y, float z,
float yaw, float pitch, boolean onGround) { float yaw, float pitch, boolean onGround) {
final Instance instance = player.getInstance(); final Instance instance = player.getInstance();
// Prevent moving before the player spawned, probably a modified client (or high latency?) // Prevent moving before the player spawned, probably a modified client (or high latency?)

View File

@ -21,13 +21,15 @@ public class Vector implements PublicCloneable<Vector> {
this.z = z; this.z = z;
} }
public Vector add(Vector vec) { @NotNull
public Vector add(@NotNull Vector vec) {
x += vec.x; x += vec.x;
y += vec.y; y += vec.y;
z += vec.z; z += vec.z;
return this; return this;
} }
@NotNull
public Vector add(float x, float y, float z) { public Vector add(float x, float y, float z) {
this.x += x; this.x += x;
this.y += y; this.y += y;
@ -41,13 +43,15 @@ public class Vector implements PublicCloneable<Vector> {
* @param vec The other vector * @param vec The other vector
* @return the same vector * @return the same vector
*/ */
public Vector subtract(Vector vec) { @NotNull
public Vector subtract(@NotNull Vector vec) {
x -= vec.x; x -= vec.x;
y -= vec.y; y -= vec.y;
z -= vec.z; z -= vec.z;
return this; return this;
} }
@NotNull
public Vector subtract(float x, float y, float z) { public Vector subtract(float x, float y, float z) {
this.x -= x; this.x -= x;
this.y -= y; this.y -= y;
@ -61,7 +65,8 @@ public class Vector implements PublicCloneable<Vector> {
* @param vec The other vector * @param vec The other vector
* @return the same vector * @return the same vector
*/ */
public Vector multiply(Vector vec) { @NotNull
public Vector multiply(@NotNull Vector vec) {
x *= vec.x; x *= vec.x;
y *= vec.y; y *= vec.y;
z *= vec.z; z *= vec.z;
@ -74,7 +79,8 @@ public class Vector implements PublicCloneable<Vector> {
* @param vec The other vector * @param vec The other vector
* @return the same vector * @return the same vector
*/ */
public Vector divide(Vector vec) { @NotNull
public Vector divide(@NotNull Vector vec) {
x /= vec.x; x /= vec.x;
y /= vec.y; y /= vec.y;
z /= vec.z; z /= vec.z;
@ -87,7 +93,8 @@ public class Vector implements PublicCloneable<Vector> {
* @param vec The other vector * @param vec The other vector
* @return the same vector * @return the same vector
*/ */
public Vector copy(Vector vec) { @NotNull
public Vector copy(@NotNull Vector vec) {
x = vec.x; x = vec.x;
y = vec.y; y = vec.y;
z = vec.z; z = vec.z;
@ -128,7 +135,7 @@ public class Vector implements PublicCloneable<Vector> {
* @param o The other vector * @param o The other vector
* @return the distance * @return the distance
*/ */
public double distance(Vector o) { public double distance(@NotNull Vector o) {
return Math.sqrt(MathUtils.square(x - o.x) + MathUtils.square(y - o.y) + MathUtils.square(z - o.z)); return Math.sqrt(MathUtils.square(x - o.x) + MathUtils.square(y - o.y) + MathUtils.square(z - o.z));
} }
@ -138,7 +145,7 @@ public class Vector implements PublicCloneable<Vector> {
* @param o The other vector * @param o The other vector
* @return the squared distance * @return the squared distance
*/ */
public double distanceSquared(Vector o) { public double distanceSquared(@NotNull Vector o) {
return MathUtils.square(x - o.x) + MathUtils.square(y - o.y) + MathUtils.square(z - o.z); return MathUtils.square(x - o.x) + MathUtils.square(y - o.y) + MathUtils.square(z - o.z);
} }
@ -149,6 +156,7 @@ public class Vector implements PublicCloneable<Vector> {
* @param m The factor * @param m The factor
* @return the same vector * @return the same vector
*/ */
@NotNull
public Vector multiply(int m) { public Vector multiply(int m) {
x *= m; x *= m;
y *= m; y *= m;
@ -163,6 +171,7 @@ public class Vector implements PublicCloneable<Vector> {
* @param m The factor * @param m The factor
* @return the same vector * @return the same vector
*/ */
@NotNull
public Vector multiply(double m) { public Vector multiply(double m) {
x *= m; x *= m;
y *= m; y *= m;
@ -177,6 +186,7 @@ public class Vector implements PublicCloneable<Vector> {
* @param m The factor * @param m The factor
* @return the same vector * @return the same vector
*/ */
@NotNull
public Vector multiply(float m) { public Vector multiply(float m) {
x *= m; x *= m;
y *= m; y *= m;
@ -204,6 +214,7 @@ public class Vector implements PublicCloneable<Vector> {
* *
* @return the same vector * @return the same vector
*/ */
@NotNull
public Vector zero() { public Vector zero() {
x = 0; x = 0;
y = 0; y = 0;
@ -295,7 +306,17 @@ public class Vector implements PublicCloneable<Vector> {
* *
* @return this vector as a position * @return this vector as a position
*/ */
@NotNull
public Position toPosition() { public Position toPosition() {
return new Position(x, y, z); return new Position(x, y, z);
} }
/**
* Get the threshold used for equals().
*
* @return The epsilon.
*/
public static double getEpsilon() {
return epsilon;
}
} }

View File

@ -8,7 +8,6 @@ import net.minestom.server.chat.ColoredText;
import net.minestom.server.entity.*; import net.minestom.server.entity.*;
import net.minestom.server.entity.damage.DamageType; import net.minestom.server.entity.damage.DamageType;
import net.minestom.server.entity.type.monster.EntityZombie; import net.minestom.server.entity.type.monster.EntityZombie;
import net.minestom.server.entity.type.other.EntityEndCrystal;
import net.minestom.server.event.GlobalEventHandler; import net.minestom.server.event.GlobalEventHandler;
import net.minestom.server.event.entity.EntityAttackEvent; import net.minestom.server.event.entity.EntityAttackEvent;
import net.minestom.server.event.entity.EntityPotionAddEvent; import net.minestom.server.event.entity.EntityPotionAddEvent;
@ -192,12 +191,6 @@ public class PlayerInit {
EntityZombie entityZombie = new EntityZombie(new Position(0, 41, 0)); EntityZombie entityZombie = new EntityZombie(new Position(0, 41, 0));
entityZombie.setInstance(player.getInstance()); entityZombie.setInstance(player.getInstance());
entityZombie.setPathTo(player.getPosition()); entityZombie.setPathTo(player.getPosition());
{
EntityEndCrystal entityEndCrystal = new EntityEndCrystal(player.getPosition());
entityEndCrystal.setInstance(instanceContainer);
entityEndCrystal.setBeamTarget(player.getPosition().toBlockPosition().add(5, 5, 0));
}
}); });
globalEventHandler.addEventCallback(PlayerDisconnectEvent.class, event -> { globalEventHandler.addEventCallback(PlayerDisconnectEvent.class, event -> {
@ -223,7 +216,7 @@ public class PlayerInit {
globalEventHandler.addEventCallback(PlayerSpawnEvent.class, event -> { globalEventHandler.addEventCallback(PlayerSpawnEvent.class, event -> {
final Player player = event.getPlayer(); final Player player = event.getPlayer();
player.setGameMode(GameMode.SURVIVAL); player.setGameMode(GameMode.CREATIVE);
PlayerInventory inventory = player.getInventory(); PlayerInventory inventory = player.getInventory();
ItemStack itemStack = new ItemStack(Material.STONE, (byte) 64); ItemStack itemStack = new ItemStack(Material.STONE, (byte) 64);