use of thpmc.engine.api.util.collision.EngineEntityBoundingBox 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.EngineEntityBoundingBox 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