Hitbox issues

Discussion in 'Plugin Development' started by Crash, Jan 26, 2011.

Thread Status:
Not open for further replies.
  1. Offline

    Crash

    I'm making a plugin that deals with what is in front of you and I need to know what block is in front of me. I am able to find this but it becomes an issue when you're partially in a block that has something in front of it but the block you're mostly in has nothing in front. I just need some type of hitbox thing to check the corners of the hitbox or maybe an onCollide method ?

    [​IMG]

    Here is an example of what I'm talking about,
    (red is hitbox, blue is your position, black are solid blocks)

    It finds nothing in front of the block but I'm still colliding with the one to the right.
     
  2. Offline

    Raphfrk

    What code are you using for hitbox?

    It is possible that it is using one of the corners of the cube you are in, instead of the middle?
     
  3. Offline

    Crash

    I don't use anything for a hitbox I just get my position and use the view angle to get the block in front of you.
     
  4. Offline

    Raphfrk

    Do you round your position to the nearest integer? What is the code :)?
     
  5. Offline

    Crash

    I use this to get the block in front of me :
    Code:
    Block facingBlock = self.getWorld().getBlockAt(loc.getBlockX(), loc.getBlockY(), loc.getBlockZ()).getFace(getClosestFace(loc.getYaw()));
    loc is the player's location, getClosestFace is a method I made that converts degrees to BlockFace, and self is the onPlayerMove person.
     
  6. Offline

    Raphfrk

    Is getClosestFace() you own method?

    If so, then you need to show that too :).

    In fairness, trying to just show the smallest amount of code just makes things slower.
     
  7. Offline

    Crash

    I didn't think that was needed because that works fine as far as I've seen.

    Code:
        public BlockFace getClosestFace(float direction){
    
            direction = direction % 360;
    
            if(direction < 0)
                direction += 360;
    
            direction = Math.round(direction / 45);
    
            switch((int)direction){
    
                case 0:
                    return BlockFace.WEST;
                case 1:
                    return BlockFace.NORTH_WEST;
                case 2:
                    return BlockFace.NORTH;
                case 3:
                    return BlockFace.NORTH_EAST;
                case 4:
                    return BlockFace.EAST;
                case 5:
                    return BlockFace.SOUTH_EAST;
                case 6:
                    return BlockFace.SOUTH;
                case 7:
                    return BlockFace.SOUTH_WEST;
                default:
                    return BlockFace.WEST;
    
            }
    }
    It's more of trying to figure out how to do this rather than fixing an error.
     
  8. Offline

    Raphfrk

    That looks fine. Are you absolutely sure you are in the right box?

    If you look vertically downwards, you can tell which block you are standing on.

    It might be worth adding a function which will send a message giving the various return values. It is possible that there is an issue with the getLocation function or something.

    Get it to send you

    - your location (doubles)
    - the result of getBlockAt -> int x,y,z
    - the result of getFacing -> int x,y,z
    - the result of get closest face

    You can then see how it changes as you move.
     
  9. Offline

    Crash

    I debugged this for awhile so I'm positive it works out fine and gets the correct block. I can get the blocks to the left and right by adding 90 or subtracting 90 to the view angle and then pass it into the getClosestFace.

    What I'm trying to figure out is to know which block I'm colliding with which is why I would need some sort of bounding box for the player.
     
  10. Offline

    Raphfrk

    Sorry I thought the problem was that your method was returning the wrong block. In the case you give in your image, you want it to check both boxes in front of you? Unless both are empty, you want it to say blocked?

    This would mean that you also need to check +1 y as well, as your player is 2 blocks tall.

    You could run your function on 4 points:
    loc(x+0.5,y,z+0.5)
    loc(x+0.5,y,z-0.5)
    loc(x-0.5,y,z+0.5)
    loc(x-0.5,y,z-0.5)

    If none of the 4 corners impact another block, then the whole square won't.

    However, you might need a more complex method of performing the calculations.

    There is a pull request that implements the actual hitblox algorithm.

    It is just 1 file, so you could copy and paste it into your plugin. You could then use it to see if any of the 4 lines hit anything within 1 block.
     
  11. Offline

    Crash

    Thanks I'll try that out and see if it works.
     
Thread Status:
Not open for further replies.

Share This Page