use of thpmc.engine.api.util.collision.EngineBoundingBox in project THP-Engine by TheHollowPlanetMC.
the class EngineWorld method rayTraceForCollisionList.
@Nullable
default EngineRayTraceResult rayTraceForCollisionList(@NotNull Vector startPosition, @NotNull Vector direction, double distance, Collection<EngineBoundingBox> boxList, @NotNull CollideOption collideOption) {
RayTraceResult nearestResult = null;
EngineBoundingBox nearestBB = null;
for (EngineBoundingBox boundingBox : boxList) {
if (collideOption.getBoundingBoxGrow() != 0.0) {
boundingBox = (EngineBoundingBox) boundingBox.clone().expand(collideOption.getBoundingBoxGrow());
}
if (collideOption.getCollideBoundingBoxFunction() != null) {
if (!collideOption.getCollideBoundingBoxFunction().apply(boundingBox)) {
continue;
}
}
if (collideOption.getCollideBlockFunction() != null) {
if (boundingBox instanceof EngineBlockBoundingBox) {
if (!collideOption.getCollideBlockFunction().apply(((EngineBlockBoundingBox) boundingBox).getBlock())) {
continue;
}
}
}
if (collideOption.getCollideEntityFunction() != null) {
if (boundingBox instanceof EngineEntityBoundingBox) {
if (!collideOption.getCollideEntityFunction().apply(((EngineEntityBoundingBox) boundingBox).getEntity())) {
continue;
}
}
}
RayTraceResult result = boundingBox.rayTrace(startPosition, direction, distance);
if (result != null) {
if (nearestResult == null) {
nearestResult = result;
nearestBB = boundingBox;
} else if (nearestResult.getHitPosition().distanceSquared(startPosition) > result.getHitPosition().distanceSquared(startPosition)) {
nearestResult = result;
nearestBB = boundingBox;
}
}
}
if (nearestResult == null || nearestResult.getHitBlockFace() == null) {
return null;
} else {
return new EngineRayTraceResult(nearestBB, nearestResult.getHitPosition(), nearestResult.getHitBlockFace());
}
}
use of thpmc.engine.api.util.collision.EngineBoundingBox in project THP-Engine by TheHollowPlanetMC.
the class EngineWorld method rayTraceBlocksForShortRange.
@Nullable
default EngineRayTraceResult rayTraceBlocksForShortRange(@NotNull Vector startPosition, @NotNull Vector direction, double distance, @NotNull CollideOption collideOption) {
Vector movement = direction.clone().normalize().multiply(distance);
Vector endPosition = startPosition.clone().add(movement);
// collect block collisions
EngineBoundingBox region = new EngineBoundingBox(startPosition.getX(), startPosition.getY(), startPosition.getZ(), endPosition.getX(), endPosition.getY(), endPosition.getZ()).expandForMovement(movement);
region.expand(collideOption.getBoundingBoxGrow());
region.expand(16);
Set<EngineBoundingBox> boxList = new HashSet<>();
// get block collisions
int startX = NumberConversions.floor(region.getMinX());
int startY = NumberConversions.floor(region.getMinY());
int startZ = NumberConversions.floor(region.getMinZ());
int endX = NumberConversions.floor(region.getMaxX());
int endY = NumberConversions.floor(region.getMaxY());
int endZ = NumberConversions.floor(region.getMaxZ());
INMSHandler nmsHandler = THPEngineAPI.getInstance().getNMSHandler();
EngineChunk chunk = null;
for (int x = startX; x < endX; x++) {
for (int y = startY; y < endY; y++) {
for (int z = startZ; z < endZ; z++) {
int chunkX = x >> 4;
int chunkZ = z >> 4;
// get chunk cache
if (chunk == null) {
chunk = this.getChunkAt(chunkX, chunkZ);
} else if (chunk.getChunkX() != chunkX || chunk.getChunkZ() != chunkZ) {
chunk = this.getChunkAt(chunkX, chunkZ);
}
if (chunk == null) {
boxList.add(EngineBoundingBox.getBoundingBoxForUnloadChunk(chunkX, chunkZ));
continue;
}
// get nms block from cache
Object iBlockData = chunk.getNMSBlockData(x, y, z);
if (iBlockData == null) {
continue;
}
EngineBlock engineBlock = new EngineBlock(this, chunk, x, y, z, iBlockData);
// collect block collisions
nmsHandler.collectBlockCollisions(engineBlock, boxList, collideOption);
}
}
}
// perform raytrace
return rayTraceForCollisionList(startPosition, direction, distance, boxList, collideOption);
}
use of thpmc.engine.api.util.collision.EngineBoundingBox in project THP-Engine by TheHollowPlanetMC.
the class EngineWorld method rayTraceEntitiesForShortRange.
@Nullable
default EngineRayTraceResult rayTraceEntitiesForShortRange(@NotNull Vector startPosition, @NotNull Vector direction, double distance, @NotNull CollideOption collideOption) {
Vector movement = direction.clone().normalize().multiply(distance);
Vector endPosition = startPosition.clone().add(movement);
// create region for get entities
EngineBoundingBox region = new EngineBoundingBox(startPosition.getX(), startPosition.getY(), startPosition.getZ(), endPosition.getX(), endPosition.getY(), endPosition.getZ()).expandForMovement(movement);
region.expand(collideOption.getBoundingBoxGrow());
region.expand(16);
// get entities
Collection<EngineEntity> entities = getEntitiesInRangeChunkSection(region);
// collect collisions
Set<EngineBoundingBox> boxList = new HashSet<>();
for (EngineEntity entity : entities) {
EngineEntityBoundingBox boundingBox = entity.getBoundingBox();
if (boundingBox != null) {
boxList.add(boundingBox);
}
}
// perform raytrace
return rayTraceForCollisionList(startPosition, direction, distance, boxList, collideOption);
}
Aggregations