Jump to content

Detecting if Entity is Within Block Hitbox


Syric

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

You can override BlockBehaviour.entityInside() on your block to detect this. Look at BasePressurePlateBlock (activates the plate) or BaseFireBlock (sets entity on fire). 

Boilerplate:

If you don't post your logs/debug.log we can't help you. For curseforge you need to enable the forge debug.log in its minecraft settings. You should also post your crash report if you have one.

If there is no error in the log file and you don't have a crash report then post the launcher_log.txt from the minecraft folder. Again for curseforge this will be in your curseforge/minecraft/Install

Large files should be posted to a file sharing site like https://gist.github.com  You should also read the support forum sticky post.

Link to comment
Share on other sites

You can do additional checks yourself in that method.

Boilerplate:

If you don't post your logs/debug.log we can't help you. For curseforge you need to enable the forge debug.log in its minecraft settings. You should also post your crash report if you have one.

If there is no error in the log file and you don't have a crash report then post the launcher_log.txt from the minecraft folder. Again for curseforge this will be in your curseforge/minecraft/Install

Large files should be posted to a file sharing site like https://gist.github.com  You should also read the support forum sticky post.

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

Boilerplate:

If you don't post your logs/debug.log we can't help you. For curseforge you need to enable the forge debug.log in its minecraft settings. You should also post your crash report if you have one.

If there is no error in the log file and you don't have a crash report then post the launcher_log.txt from the minecraft folder. Again for curseforge this will be in your curseforge/minecraft/Install

Large files should be posted to a file sharing site like https://gist.github.com  You should also read the support forum sticky post.

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
Unfortunately, your content contains terms that we do not allow. Please edit your content to remove the highlighted words below.
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.



×
×
  • Create New...

Important Information

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