Search in sources :

Example 1 with EngineBoundingBox

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());
    }
}
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 EngineBoundingBox

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);
}
Also used : EngineBlock(thpmc.engine.api.world.block.EngineBlock) Vector(org.bukkit.util.Vector) EngineBoundingBox(thpmc.engine.api.util.collision.EngineBoundingBox) INMSHandler(thpmc.engine.api.nms.INMSHandler) HashSet(java.util.HashSet) Nullable(org.jetbrains.annotations.Nullable)

Example 3 with EngineBoundingBox

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);
}
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)3 EngineBoundingBox (thpmc.engine.api.util.collision.EngineBoundingBox)3 HashSet (java.util.HashSet)2 Vector (org.bukkit.util.Vector)2 EngineEntityBoundingBox (thpmc.engine.api.util.collision.EngineEntityBoundingBox)2 RayTraceResult (org.bukkit.util.RayTraceResult)1 EngineEntity (thpmc.engine.api.entity.EngineEntity)1 INMSHandler (thpmc.engine.api.nms.INMSHandler)1 EngineBlockBoundingBox (thpmc.engine.api.util.collision.EngineBlockBoundingBox)1 EngineBlock (thpmc.engine.api.world.block.EngineBlock)1