From a29a51ee0aacb994f62bb9fcdd61ff9e462c0197 Mon Sep 17 00:00:00 2001 From: themode Date: Wed, 9 Mar 2022 20:14:42 +0100 Subject: [PATCH] Greatly reduce the number of checks --- .../server/collision/BlockCollision.java | 36 +++++-------------- 1 file changed, 9 insertions(+), 27 deletions(-) diff --git a/src/main/java/net/minestom/server/collision/BlockCollision.java b/src/main/java/net/minestom/server/collision/BlockCollision.java index 02aaad186..537917761 100644 --- a/src/main/java/net/minestom/server/collision/BlockCollision.java +++ b/src/main/java/net/minestom/server/collision/BlockCollision.java @@ -148,38 +148,20 @@ final class BlockCollision { // Go through all points to check. See if the point after the move will be in a new block // If the point after is in a new block that new block needs to be checked, otherwise only check the current block for (Vec point : allFaces) { - Vec pointBefore = point.add(entityPosition); - Vec pointAfter = point.add(entityPosition).add(deltaPosition); - - if (pointBefore.blockX() != pointAfter.blockX()) { - checkBoundingBox(pointAfter.blockX(), pointBefore.blockY(), pointBefore.blockZ(), deltaPosition, entityPosition, boundingBox, instance, originChunk, finalResult); - + final Vec pointBefore = point.add(entityPosition); + final Vec pointAfter = pointBefore.add(deltaPosition); + if (!pointAfter.sameBlock(pointBefore)) { + if (pointBefore.blockX() != pointAfter.blockX()) { + checkBoundingBox(pointAfter.blockX(), pointBefore.blockY(), pointBefore.blockZ(), deltaPosition, entityPosition, boundingBox, instance, originChunk, finalResult); + } if (pointBefore.blockY() != pointAfter.blockY()) { - checkBoundingBox(pointAfter.blockX(), pointAfter.blockY(), pointBefore.blockZ(), deltaPosition, entityPosition, boundingBox, instance, originChunk, finalResult); + checkBoundingBox(pointBefore.blockX(), pointAfter.blockY(), pointBefore.blockZ(), deltaPosition, entityPosition, boundingBox, instance, originChunk, finalResult); } if (pointBefore.blockZ() != pointAfter.blockZ()) { - checkBoundingBox(pointAfter.blockX(), pointBefore.blockY(), pointAfter.blockZ(), deltaPosition, entityPosition, boundingBox, instance, originChunk, finalResult); + checkBoundingBox(pointBefore.blockX(), pointBefore.blockY(), pointAfter.blockZ(), deltaPosition, entityPosition, boundingBox, instance, originChunk, finalResult); } } - - if (pointBefore.blockY() != pointAfter.blockY()) { - checkBoundingBox(pointBefore.blockX(), pointAfter.blockY(), pointBefore.blockZ(), deltaPosition, entityPosition, boundingBox, instance, originChunk, finalResult); - - if (pointBefore.blockZ() != pointAfter.blockZ()) { - checkBoundingBox(pointBefore.blockX(), pointAfter.blockY(), pointAfter.blockZ(), deltaPosition, entityPosition, boundingBox, instance, originChunk, finalResult); - } - } - - if (pointBefore.blockZ() != pointAfter.blockZ()) { - checkBoundingBox(pointBefore.blockX(), pointBefore.blockY(), pointAfter.blockZ(), deltaPosition, entityPosition, boundingBox, instance, originChunk, finalResult); - } - checkBoundingBox(pointBefore.blockX(), pointBefore.blockY(), pointBefore.blockZ(), deltaPosition, entityPosition, boundingBox, instance, originChunk, finalResult); - - if (pointBefore.blockX() != pointAfter.blockX() - && pointBefore.blockY() != pointAfter.blockY() - && pointBefore.blockZ() != pointAfter.blockZ()) - checkBoundingBox(pointAfter.blockX(), pointAfter.blockY(), pointAfter.blockZ(), deltaPosition, entityPosition, boundingBox, instance, originChunk, finalResult); } } else { // When large moves are done we need to ray-cast to find all blocks that could intersect with the movement @@ -252,8 +234,8 @@ final class BlockCollision { checkBlock = Block.STONE; // Generic full block } boolean hitBlock = false; - final Pos blockPos = new Pos(blockX, blockY, blockZ); if (checkBlock.isSolid()) { + final Vec blockPos = new Vec(blockX, blockY, blockZ); hitBlock = checkBlock.registry().collisionShape().intersectBoxSwept(entityPosition, entityVelocity, blockPos, boundingBox, finalResult); } return hitBlock;