fix: entity onGround applies correctly (#2059)

This commit is contained in:
DeidaraMC 2024-03-28 04:22:21 -04:00 committed by GitHub
parent f95d73eca8
commit f09d3db999
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 33 additions and 1 deletions

View File

@ -47,7 +47,7 @@ public final class PhysicsUtils {
private static @NotNull Vec updateVelocity(@NotNull Pos entityPosition, @NotNull Vec currentVelocity, @NotNull Block.Getter blockGetter, @NotNull Aerodynamics aerodynamics,
boolean positionChanged, boolean entityFlying, boolean entityOnGround, boolean entityNoGravity) {
if (!positionChanged) {
if (entityOnGround || entityFlying) return Vec.ZERO;
if (entityFlying) return Vec.ZERO;
return new Vec(0, entityNoGravity ? 0 : -aerodynamics.gravity() * aerodynamics.verticalAirResistance(), 0);
}

View File

@ -0,0 +1,32 @@
package net.minestom.server.entity;
import net.minestom.server.coordinate.Pos;
import net.minestom.server.instance.block.Block;
import net.minestom.testing.Env;
import net.minestom.testing.EnvTest;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.*;
@EnvTest
public class EntityPhysicsIntegrationTest
{
@Test
public void onGround(Env env) {
var instance = env.createFlatInstance();
instance.setBlock(1, 40, 1, Block.STONE);
var entity = new Entity(EntityTypes.ZOMBIE);
entity.setInstance(instance, new Pos(1, 41, 1)).join();
env.tick();
// Entity shouldn't be on ground because it intitially spawns in with onGround = false
// and a velocity of 0, it'll take 1 entity tick for gravity to be applied to their velocity
// and a downward block collision to occur
assertFalse(entity.onGround);
for (int i = 0; i < 10; i++) {
env.tick();
assertTrue(entity.onGround, "entity needs to be grounded on tick: " + entity.getAliveTicks());
}
}
}