From b0bc389efda9f5d66c52abcfb7b8e6e8b1886f39 Mon Sep 17 00:00:00 2001 From: daoge_cmd <3523206925@qq.com> Date: Sun, 2 Feb 2025 04:45:26 +0800 Subject: [PATCH] feat: entity will be push out from block by default now --- .../entity/component/EntityBaseComponent.java | 2 +- .../EntityPickableBaseComponentImpl.java | 5 --- .../service/AllayEntityPhysicsService.java | 38 ++++++++++++------- 3 files changed, 25 insertions(+), 20 deletions(-) diff --git a/api/src/main/java/org/allaymc/api/entity/component/EntityBaseComponent.java b/api/src/main/java/org/allaymc/api/entity/component/EntityBaseComponent.java index e7f3aa5f3e..e883238fff 100644 --- a/api/src/main/java/org/allaymc/api/entity/component/EntityBaseComponent.java +++ b/api/src/main/java/org/allaymc/api/entity/component/EntityBaseComponent.java @@ -337,7 +337,7 @@ default boolean computeEntityCollisionMotion() { * @return {@code true} if the entity has block collision motion. */ default boolean computeBlockCollisionMotion() { - return false; + return true; } /** diff --git a/server/src/main/java/org/allaymc/server/entity/component/EntityPickableBaseComponentImpl.java b/server/src/main/java/org/allaymc/server/entity/component/EntityPickableBaseComponentImpl.java index 773bccc0b0..982b61e06e 100644 --- a/server/src/main/java/org/allaymc/server/entity/component/EntityPickableBaseComponentImpl.java +++ b/server/src/main/java/org/allaymc/server/entity/component/EntityPickableBaseComponentImpl.java @@ -67,11 +67,6 @@ public NbtMap saveNBT() { return builder.build(); } - @Override - public boolean computeBlockCollisionMotion() { - return true; - } - @Override public float getGravity() { return 0.04f; diff --git a/server/src/main/java/org/allaymc/server/world/service/AllayEntityPhysicsService.java b/server/src/main/java/org/allaymc/server/world/service/AllayEntityPhysicsService.java index ca68b64f66..274b2bb3b8 100644 --- a/server/src/main/java/org/allaymc/server/world/service/AllayEntityPhysicsService.java +++ b/server/src/main/java/org/allaymc/server/world/service/AllayEntityPhysicsService.java @@ -59,7 +59,8 @@ public class AllayEntityPhysicsService implements EntityPhysicsService { public static final float BLOCK_COLLISION_MOTION; /** * When the min distance of entity to the collision shape of block is smaller than FAT_AABB_MARGIN, - * the entity will be considered as collided with the block. This is what the vanilla actually does. + * the entity will be considered as collided with the block. This is used to prevent floating point + * number overflow problem and is what the vanilla actually does. *

* This value is actually the value of the y coordinate decimal point when the player stands on the * full block (such as the grass block) @@ -418,7 +419,9 @@ protected boolean applyMotion(Entity entity) { */ private float applyMotion0(float stepHeight, Location3f pos, float motion, AABBf aabb, boolean enableStepping, int axis) { if (motion == 0) return motion; - if (axis < X || axis > Z) throw new IllegalArgumentException("Invalid axis: " + axis); + if (axis < X || axis > Z) { + throw new IllegalArgumentException("Invalid axis: " + axis); + } var resultAABB = new AABBf(aabb); var resultPos = new Vector3f(pos); @@ -456,33 +459,36 @@ private float applyMotion0(float stepHeight, Location3f pos, float motion, AABBf * @throws IllegalArgumentException if an invalid axis is provided. */ private Pair moveAlongAxisAndStopWhenCollision(AABBf aabb, float motion, Vector3f recorder, int axis) { - if (motion == 0) return EMPTY_FLOAT_BOOLEAN_PAIR; + if (motion == 0) { + return EMPTY_FLOAT_BOOLEAN_PAIR; + } var extendAxis = new AABBf(aabb); // Move towards the negative(motion < 0) or positive(motion > 0) axis direction var shouldTowardsNegative = motion < 0; switch (axis) { - case X: + case X -> { var lengthX = extendAxis.lengthX(); extendAxis.minX += shouldTowardsNegative ? motion : lengthX; extendAxis.maxX += shouldTowardsNegative ? -lengthX : motion; - break; - case Y: + } + case Y -> { var lengthY = extendAxis.lengthY(); extendAxis.minY += shouldTowardsNegative ? motion : lengthY; extendAxis.maxY += shouldTowardsNegative ? -lengthY : motion; - break; - case Z: + } + case Z -> { var lengthZ = extendAxis.lengthZ(); extendAxis.minZ += shouldTowardsNegative ? motion : lengthZ; extendAxis.maxZ += shouldTowardsNegative ? -lengthZ : motion; - break; - default: - throw new IllegalArgumentException("Invalid axis provided"); + } + default -> throw new IllegalArgumentException("Invalid axis provided"); } - if (notValidEntityArea(extendAxis)) return EMPTY_FLOAT_BOOLEAN_PAIR; + if (notValidEntityArea(extendAxis)) { + return EMPTY_FLOAT_BOOLEAN_PAIR; + } var deltaAxis = motion; var collision = false; @@ -501,8 +507,12 @@ private Pair moveAlongAxisAndStopWhenCollision(AABBf aabb, float deltaAxis = 0; } else { deltaAxis = min(abs(coordinate - minAxis), abs(coordinate - maxAxis)); - if (shouldTowardsNegative) deltaAxis = -deltaAxis; - if (abs(deltaAxis) <= FAT_AABB_MARGIN) deltaAxis = 0; + if (shouldTowardsNegative) { + deltaAxis = -deltaAxis; + } + if (abs(deltaAxis) <= FAT_AABB_MARGIN) { + deltaAxis = 0; + } } motion = 0;