Skip to content

Commit

Permalink
Apply coordinate offset only to VoxelShape
Browse files Browse the repository at this point in the history
VoxelShape coordiantes generally are an integer + a sum of powers of
two between [-1, -3]. Most offsets are generally an integer. As
a result, applying an offset to the coordinates generally results
in an error of 0. However, coordinate inputs do not follow such
trends. Thus, when applying an offset to the coordinate input,
there may be some floating point error.

By applying the offset to the VoxelShape coordinates, we can
eliminate additional floating point error.

This change also fixes the inconsistency when using
the single AABB, as input coordinates were not offset
when using the single AABB as the single AABB is already
offset.

Fixes #81
This specific issue is caused by floating point error resulting
in the falling anvil's y position becoming around -8E-17 when it
should be 0.
While this is still very comfortably in the collision
epsilon (1.0E-7), this results in the falling anvil's y block
position to become -1 (as the block position is simply
the floor of the coordinate).
  • Loading branch information
Spottedleaf committed Dec 4, 2024
1 parent e917272 commit d87df61
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 82 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -378,19 +378,19 @@ public int findIndex(final Direction.Axis axis, final double value) {
case X: {
final double[] values = this.rootCoordinatesX;
return CollisionUtil.findFloor(
values, value - this.offsetX, 0, values.length - 1
values, this.offsetX, value, 0, values.length - 1
);
}
case Y: {
final double[] values = this.rootCoordinatesY;
return CollisionUtil.findFloor(
values, value - this.offsetY, 0, values.length - 1
values, this.offsetY, value, 0, values.length - 1
);
}
case Z: {
final double[] values = this.rootCoordinatesZ;
return CollisionUtil.findFloor(
values, value - this.offsetZ, 0, values.length - 1
values, this.offsetZ, value, 0, values.length - 1
);
}
default: {
Expand All @@ -411,7 +411,7 @@ private VoxelShape calculateFaceDirect(final Direction direction, final Directio

// see findIndex
final int index = CollisionUtil.findFloor(
coords, (positiveDir ? (1.0 - CollisionUtil.COLLISION_EPSILON) : (0.0 + CollisionUtil.COLLISION_EPSILON)) - offset,
coords, offset, (positiveDir ? (1.0 - CollisionUtil.COLLISION_EPSILON) : (0.0 + CollisionUtil.COLLISION_EPSILON)),
0, coords.length - 1
);

Expand Down
Loading

0 comments on commit d87df61

Please sign in to comment.