Search in sources :

Example 1 with EngineEntityBoundingBox

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());
    }
}
Also used : EngineBlockBoundingBox(thpmc.engine.api.util.collision.EngineBlockBoundingBox) EngineEntityBoundingBox(thpmc.engine.api.util.collision.EngineEntityBoundingBox) RayTraceResult(org.bukkit.util.RayTraceResult) EngineBoundingBox(thpmc.engine.api.util.collision.EngineBoundingBox) Nullable(org.jetbrains.annotations.Nullable)

Example 2 with EngineEntityBoundingBox

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);
}
Also used : EngineEntityBoundingBox(thpmc.engine.api.util.collision.EngineEntityBoundingBox) EngineEntity(thpmc.engine.api.entity.EngineEntity) Vector(org.bukkit.util.Vector) EngineBoundingBox(thpmc.engine.api.util.collision.EngineBoundingBox) HashSet(java.util.HashSet) Nullable(org.jetbrains.annotations.Nullable)

Aggregations

Nullable (org.jetbrains.annotations.Nullable)2 EngineBoundingBox (thpmc.engine.api.util.collision.EngineBoundingBox)2 EngineEntityBoundingBox (thpmc.engine.api.util.collision.EngineEntityBoundingBox)2 HashSet (java.util.HashSet)1 RayTraceResult (org.bukkit.util.RayTraceResult)1 Vector (org.bukkit.util.Vector)1 EngineEntity (thpmc.engine.api.entity.EngineEntity)1 EngineBlockBoundingBox (thpmc.engine.api.util.collision.EngineBlockBoundingBox)1