Jump to content
Search In
  • More options...
Find results that contain...
Find results in...

Detecting if Entity is Within Block Hitbox


Syric
 Share

Recommended Posts

I have a block that doesn't take up a full block - it's placed on the side of existing blocks (and is therefore directional). It has a hitbox of about 16x16x2 pixels and does not collide, so it can be entered.

How should I detect when an entity's hitbox intersects with the block's? I simply need to return a boolean for an entity (a check triggered by entityInside, which covers the entire 16x16x16 cube) depending on whether they're in the block's hitbox or not. I tried using level.getEntities, but that seems to be only searching for entities' origins. There are a few other methods: several versions of getEntities, one getEntityCollisions, and one getCollisions, but those don't seem to be returning any hits when I step into the block either. Any advice?

Link to comment
Share on other sites

entityInside() detects the entirety of the 16x16x16-pixel block. I want something that only detects when the entity is inside the small part of it that has a hitbox.

Link to comment
Share on other sites

Yes, I know. That's what I'm asking for help with.

12 hours ago, Syric said:

I simply need to return a boolean for an entity (a check triggered by entityInside, which covers the entire 16x16x16 cube) depending on whether they're in the block's hitbox or not.

I'm not sure which methods to use to check if their hitbox intersects the block's.

Link to comment
Share on other sites

There are a few methods available.

There is AABB which lets you calculate an intersect(). For the entity you would just use entity.getBoundingBox(), the other one would be whatever part of your block you want to check.

There is a pretty complicated entity.collideBoundingBox() that lets you pass an intersection of shapes.

Or you could just do your own (optimised?) calculation based on the entity.getBoundingBox()

  • Thanks 1
Link to comment
Share on other sites

Here's my solution, based on entity.isColliding(). You can just pass your block's VoxelShape in as shape; this block is directional so it needs a reference to the map.

This method returns 'true' if the hitbox of the entity in question intersects the provided shape, in this case the shape of a non-colliding block.

    private boolean isColliding(BlockPos pos, BlockState state, Entity entity) {
        VoxelShape shape = SHAPE_MAP.get(state.getValue(FACING));
        VoxelShape positionedShape = shape.move((double)pos.getX(), (double)pos.getY(), (double)pos.getZ());
        return Shapes.joinIsNotEmpty(positionedShape, Shapes.create(entity.getBoundingBox()), BooleanOp.AND);
    }

 

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

 Share



×
×
  • Create New...

Important Information

By using this site, you agree to our Terms of Use.