[Class] Hitbox class (check if a player is inside a location)

Discussion in 'Spigot Plugin Development' started by retrodaredevil, Apr 3, 2016.

  1. Recently I made a post on how to check if a player is inside a location and the answers I got weren't as acurate as I had hoped. This class is only like 15 lines of actual code but I thought this might help a few people out. Here's the Dropbox link: https://www.dropbox.com/s/d6bqq10u6hle1yh/Hitbox.java?dl=0

    that file should be changed whenever I update it.
     
    #1 retrodaredevil, Apr 3, 2016
    Last edited: Jun 13, 2016
  2. Choco

    Moderator

    Code (Java):
    public class Hitbox {
        private final Location aa, ab;
        public Hitbox(Player player){
            this.aa == player.getLocation().subtract(0.3, 0, 0.3);
            this.ab = player.getLocation().add(0.3, 1.8, 0.3);
        }
        public Hitbox(Location aa, Location ab){
            this.aa = aa;
            this.ab = ab;
        }

        public boolean intersects(Location l){
            return ((l.getX() <= aa.getX() && l.getY() <= aa.getY() && l.getZ() <= aa.getZ())
                && (l.getX() >= ab.getX() && l.getY() >= ab.getY() && l.getZ() >= ab.getZ()));
        }

        public Location getPointAA(){
            return aa;
        }

        public Location getPointAB(){
            return ab;
        }
    }
    I rewrote it so that it makes a little bit more sense. Though you have to know that this is ONLY for players. It's quite specific as to how much it adds/subtracts, and not all entities are 1.8 blocks tall / 0.6 blocks in width/length.

    Also, as a side note, there's an NMS method to retrieve an entity's hitbox, net.minecraft.server.1_9_R1.Entity#getBoundingBox();
     
    #2 Choco, Apr 3, 2016
    Last edited: Apr 3, 2016
    • Like Like x 1
    • Informative Informative x 1
  3. Sorry for necro posting. I have a question.
    Why do you subtract/add 0.3? It wont be accurate. A player hitbox (in length) is 0.5 right?
     
  4. ask the one who made this post he's the one who did it so he knows what the reason is.
     
    • Agree Agree x 1
  5. Yeah, I am asking him.
     
  6. how should I know you've quoted other user's post.
     
    • Agree Agree x 1
  7. @2008Choco, I am asking you. ^_^
     
  8. The base of the hit box is 0.6 by 0.6 blocks and the players location is in the middle of that so by adding 0.3 it will create the box accurately. I believe it says this somewhere on this page: http://minecraft.gamepedia.com/The_Player
     
    • Like Like x 1
  9. What is the shape of a player, is it a cuboid or a cylinder?
     
  10. The hit box is cuboid.
     
    • Like Like x 1
  11. I using hitbox by size 0,8x0,8x1,8 cause default hitbox is not one of the best.