Micro collision optimization + style

This commit is contained in:
TheMode 2021-07-11 20:20:01 +02:00
parent 96c7fc9147
commit 9e8d0c9ce0
3 changed files with 36 additions and 32 deletions

View File

@ -50,6 +50,35 @@ public class BoundingBox {
return intersect(entity.getBoundingBox());
}
/**
* Used to know if the bounding box intersects at a block position.
*
* @param blockX the block X
* @param blockY the block Y
* @param blockZ the block Z
* @return true if the bounding box intersects with the position, false otherwise
*/
public boolean intersectWithBlock(int blockX, int blockY, int blockZ) {
final double offsetX = 1;
final double maxX = (double) blockX + offsetX;
final boolean checkX = getMinX() < maxX && getMaxX() > (double) blockX;
if (!checkX)
return false;
final double maxY = (double) blockY + 0.99999;
final boolean checkY = getMinY() < maxY && getMaxY() > (double) blockY;
if (!checkY)
return false;
final double offsetZ = 1;
final double maxZ = (double) blockZ + offsetZ;
// Z check
return getMinZ() < maxZ && getMaxZ() > (double) blockZ;
}
/**
* Used to know if the bounding box intersects at a point.
*
@ -57,27 +86,7 @@ public class BoundingBox {
* @return true if the bounding box intersects with the position, false otherwise
*/
public boolean intersectWithBlock(@NotNull Point blockPosition) {
final double offsetX = 1;
final double x = blockPosition.blockX();
final double maxX = x + offsetX;
final boolean checkX = getMinX() < maxX && getMaxX() > x;
if (!checkX)
return false;
final double y = blockPosition.blockY();
final double maxY = y + 0.99999;
final boolean checkY = getMinY() < maxY && getMaxY() > y;
if (!checkY)
return false;
final double offsetZ = 1;
final double z = blockPosition.blockZ();
final double maxZ = z + offsetZ;
// Z check
return getMinZ() < maxZ && getMaxZ() > z;
return intersectWithBlock(blockPosition.blockX(), blockPosition.blockY(), blockPosition.blockZ());
}
public boolean intersect(double x, double y, double z) {

View File

@ -577,11 +577,10 @@ public class Entity implements Viewable, Tickable, EventHandler<EntityEvent>, Da
final Block block = chunk.getBlock(x, y, z);
final BlockHandler handler = block.handler();
if (handler != null) {
final var blockPosition = new Vec(x, y, z);
// checks that we are actually in the block, and not just here because of a rounding error
if (boundingBox.intersectWithBlock(blockPosition)) {
if (boundingBox.intersectWithBlock(x, y, z)) {
// TODO: replace with check with custom block bounding box
handler.onTouch(new BlockHandler.Touch(block, instance, blockPosition, this));
handler.onTouch(new BlockHandler.Touch(block, instance, new Vec(x, y, z), this));
}
}
}

View File

@ -130,8 +130,7 @@ public abstract class Chunk implements BlockGetter, BlockSetter, Viewable, Ticka
*
* @return a new chunk data packet
*/
@NotNull
public abstract ChunkDataPacket createChunkPacket();
public abstract @NotNull ChunkDataPacket createChunkPacket();
/**
* Creates a copy of this chunk, including blocks state id, custom block id, biomes, update data.
@ -143,8 +142,7 @@ public abstract class Chunk implements BlockGetter, BlockSetter, Viewable, Ticka
* @param chunkZ the chunk Z of the copy
* @return a copy of this chunk with a potentially new instance and position
*/
@NotNull
public abstract Chunk copy(@NotNull Instance instance, int chunkX, int chunkZ);
public abstract @NotNull Chunk copy(@NotNull Instance instance, int chunkX, int chunkZ);
/**
* Resets the chunk, this means clearing all the data making it empty.
@ -158,8 +156,7 @@ public abstract class Chunk implements BlockGetter, BlockSetter, Viewable, Ticka
*
* @return the chunk identifier
*/
@NotNull
public UUID getIdentifier() {
public @NotNull UUID getIdentifier() {
return identifier;
}
@ -168,8 +165,7 @@ public abstract class Chunk implements BlockGetter, BlockSetter, Viewable, Ticka
*
* @return the linked instance
*/
@NotNull
public Instance getInstance() {
public @NotNull Instance getInstance() {
return instance;
}