[Solved] isInsideBox() method

Discussion in 'Plugin Development' started by CrankkDatJFel, Jul 16, 2012.

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

    CrankkDatJFel

    I am trying to figure out a way to tell if a Player is within a cube made up of 2 points in opposite corners. I will provide what I tried but I am thinking this has to have been made before. So why reinvent the wheel if I don't need too. Any help appreciated.

    Code:
        public boolean isInsideCube(Player player, Location locOne, Location locTwo) {
            Location playerLoc = player.getLocation();
       
            Double playerXDouble = playerLoc.getX();
            Double playerYDouble = playerLoc.getY();
            Double playerZDouble = playerLoc.getZ();
       
            int playerX = playerXDouble.intValue();
            int playerY = playerYDouble.intValue();
            int playerZ = playerZDouble.intValue();
       
            Double oneXDouble = locOne.getX();
            Double oneYDouble = locOne.getY();
            Double oneZDouble = locOne.getZ();
       
            int oneX = oneXDouble.intValue();
            int oneY = oneYDouble.intValue();
            int oneZ = oneZDouble.intValue();
       
            Double twoXDouble = locTwo.getX();
            Double twoYDouble = locTwo.getY();
            Double twoZDouble = locTwo.getZ();
       
            int twoX = twoXDouble.intValue();
            int twoY = twoYDouble.intValue();
            int twoZ = twoZDouble.intValue();
       
            boolean isInsideCube = false;
       
            //playerLoc.equals(new Location(world, oneX, oneY, oneZ))
       
            if (oneX <= twoX) {
                if (oneZ <= twoZ) {
                    if (oneY <= twoY) {
                        for (; oneX <= twoX; oneX++) {
                            for (; oneZ <= twoZ; oneZ++) {
                                for (; oneY <= twoY; oneY++) {
                                    if ((oneX == playerX) && (oneZ == playerZ) && (oneY == playerY)) {
                                        isInsideCube = true;
                                    }
                                }
                            }
                        }
                    } else {
                        for (; oneX <= twoX; oneX++) {
                            for (; oneZ <= twoZ; oneZ++) {
                                for (; oneY >= twoY; oneY--) {
                                    if ((oneX == playerX) && (oneZ == playerZ) && (oneY == playerY)) {
                                        isInsideCube = true;
                                    }
                                }
                            }
                        }
                    }
                } else {
                    if (oneY <= twoY) {
                        for (; oneX <= twoX; oneX++) {
                            for (; oneZ >= twoZ; oneZ--) {
                                for (; oneY <= twoY; oneY++) {
                                    if ((oneX == playerX) && (oneZ == playerZ) && (oneY == playerY)) {
                                        isInsideCube = true;
                                    }
                                }
                            }
                        }
                    } else {
                        for (; oneX <= twoX; oneX++) {
                            for (; oneZ >= twoZ; oneZ--) {
                                for (; oneY >= twoY; oneY--) {
                                    if ((oneX == playerX) && (oneZ == playerZ) && (oneY == playerY)) {
                                        isInsideCube = true;
                                    }
                                }
                            }
                        }
                    }
                }
            } else {
                if (oneZ <= twoZ) {
                    if (oneY <= twoY) {
                        for (; oneX >= twoX; oneX--) {
                            for (; oneZ <= twoZ; oneZ++) {
                                for (; oneY <= twoY; oneY++) {
                                    if ((oneX == playerX) && (oneZ == playerZ) && (oneY == playerY)) {
                                        isInsideCube = true;
                                    }
                                }
                            }
                        }
                    } else {
                        for (; oneX >= twoX; oneX--) {
                            for (; oneZ <= twoZ; oneZ++) {
                                for (; oneY >= twoY; oneY--) {
                                    if ((oneX == playerX) && (oneZ == playerZ) && (oneY == playerY)) {
                                        isInsideCube = true;
                                    }
                                }
                            }
                        }
                    }
                } else {
                    if (oneY <= twoY) {
                        for (; oneX >= twoX; oneX--) {
                            for (; oneZ >= twoZ; oneZ--) {
                                for (; oneY <= twoY; oneY++) {
                                    if ((oneX == playerX) && (oneZ == playerZ) && (oneY == playerY)) {
                                        isInsideCube = true;
                                    }
                                }
                            }
                        }
                    } else {
                        for (; oneX >= twoX; oneX--) {
                            for (; oneZ >= twoZ; oneZ--) {
                                for (; oneY >= twoY; oneY--) {
                                    if ((oneX == playerX) && (oneZ == playerZ) && (oneY == playerY)) {
                                        isInsideCube = true;
                                    }
                                }
                            }
                        }
                    }
                }
            }
       
       
            return isInsideCube;
       
        }
    Edit: ^-- is very dumb, and I know this now. I was thinking to check every block to see if the Player is there.
     
  2. Offline

    Vynlar

    This can be done much more simply. Check if the player X is more than the bottom of the cube, and less than the top, then same for the X axis and Z axis'. You also don't need both the int and double values. One set(probably the doubles for more accuracy, will do.) If you have any more questions, post here with your new code and questions.
     
  3. Offline

    CrankkDatJFel

    Hey thanks for the help. I think I did it correctly this time.

    Code:
        public boolean isInsideCube(Player player, Location locOne, Location locTwo) {
         
            Location playerLoc = player.getLocation();
         
            double playerX = playerLoc.getX();
            double playerY = playerLoc.getY();
            double playerZ = playerLoc.getZ();
            double oneX = locOne.getX();
            double oneY = locOne.getY();
            double oneZ = locOne.getZ();
            double twoX = locTwo.getX();
            double twoY = locTwo.getY();
            double twoZ = locTwo.getZ();
         
     
         
            boolean isInsideCube = true;
         
            // X Axis
            if (oneX >= twoX) {
                if (((playerX-1)  > oneX) || (playerX < twoX)) {
                    isInsideCube = false;
                }
            } else {
                if (((playerX-1)  > twoX) || (playerX < oneX)) {
                    isInsideCube = false;
                }
            }
         
            // Z Axis
            if (oneZ >= twoZ) {
                if (((playerZ-1)  >= oneZ) || (playerZ < twoZ)) {
                    isInsideCube = false;
                }
            } else {
                if (((playerZ-1)  >= twoZ) || (playerZ < oneZ)) {
                    isInsideCube = false;
                }
            }
         
            // Y Axis
            if (oneY >= twoY) {
                if ((playerY  >= oneY) || (playerY < twoY)) {
                    isInsideCube = false;
                }
            } else {
                if ((playerY  >= twoY) || (playerY < oneY)) {
                    isInsideCube = false;
                }
            }
         
            return isInsideCube; 
        }
     
  4. Offline

    Dark_Balor

    you can optimize a little, instead of using a boolean you can return directly.

    After all, if the player is within the limit of one of the Axis, the statement is reached, no need to check the other axis.
     
    ferrybig likes this.
  5. Offline

    CrankkDatJFel

    Thanks, I see what you're talking about.

    Code:
        public boolean isInsideCube(Player player, Location locOne, Location locTwo) {
           
            Location playerLoc = player.getLocation();
           
            double playerX = playerLoc.getX();
            double playerY = playerLoc.getY();
            double playerZ = playerLoc.getZ();
            double oneX = locOne.getX();
            double oneY = locOne.getY();
            double oneZ = locOne.getZ();
            double twoX = locTwo.getX();
            double twoY = locTwo.getY();
            double twoZ = locTwo.getZ();
           
            // X Axis
            if (oneX >= twoX) {
                if (((playerX-1)  > oneX) || (playerX < twoX)) {
                    return false;
                }
            } else {
                if (((playerX-1)  > twoX) || (playerX < oneX)) {
                    return false;
                }
            }
           
            // Z Axis
            if (oneZ >= twoZ) {
                if (((playerZ-1)  >= oneZ) || (playerZ < twoZ)) {
                    return false;
                }
            } else {
                if (((playerZ-1)  >= twoZ) || (playerZ < oneZ)) {
                    return false;
                }
            }
           
            // Y Axis
            if (oneY >= twoY) {
                if ((playerY  >= oneY) || (playerY < twoY)) {
                    return false;
                }
            } else {
                if ((playerY  >= twoY) || (playerY < oneY)) {
                    return false;
                }
            }
            return true;   
        }
    }
     
  6. Offline

    Courier

    This is a little more optimised:
    Code:
    public boolean isInsideCube(Player player, Location locOne, Location locTwo)
    {
            Location playerLoc = player.getLocation();
            //x axis
            double[] locBounds = new double[] {locOne.getX(), locTwo.getX()};
            Arrays.sort(locBounds);
            double playerPosition = playerLoc.getX();
            if(playerPosition < locBounds[0] || playerPosition > locBounds[1])
                    return false;
            //z axis
            locBounds[0] = locOne.getZ();
            locBounds[1] = locTwo.getZ();
            playerPosition = playerLoc.getZ();
            Arrays.sort(locBounds);
            if(playerPosition < locBounds[0] || playerPosition > locBounds[1])
                    return false;
            //y axis
            locBounds[0] = locOne.getY();
            locBounds[1] = locTwo.getY();
            playerPosition = playerLoc.getY();
            Arrays.sort(locBounds);
            return (playerPosition >= locBounds[0] && playerPosition <= locBounds[1]);
        }
    }
    I'm not completely sure using arrays and Arrays.sort() will be faster in this case; that is somewhat system-dependent. Experiment. Even if you stick with your way, you shouldn't call .getZ() until you are sure that the player is inside X, etc.; don't call anything unless you need it. I don't have the playerX-1 stuff, but you could easily put that back.
     
  7. Offline

    CrankkDatJFel

    I think using the array and doing it my way has to be around the same. But I do see your point about not grabbing variables unless I am positive I need them so here is what I have now and I am not sure it can get any better than this.

    Also I used (playerX-1) and (playerZ-1) to fit my definition of being "inside the box" which is both blocks the Player is taking up must be within the cube. Without the -1's if you stand on the edge block you were considered outside the cube.

    Code:
        public boolean isInsideCube(Player player, Location locOne, Location locTwo) {
     
            Location playerLoc = player.getLocation();
     
            double playerX = playerLoc.getX();
            double oneX = locOne.getX();
            double twoX = locTwo.getX();
       
            // X Axis
            if (oneX >= twoX) {
                if (((playerX-1)  > oneX) || (playerX < twoX)) {
                    return false;
                }
            } else {
                if (((playerX-1)  > twoX) || (playerX < oneX)) {
                    return false;
                }
            }
     
            double playerZ = playerLoc.getZ();
            double oneZ = locOne.getZ();
            double twoZ = locTwo.getZ();
     
            // Z Axis
            if (oneZ >= twoZ) {
                if (((playerZ-1)  >= oneZ) || (playerZ < twoZ)) {
                    return false;
                }
            } else {
                if (((playerZ-1)  >= twoZ) || (playerZ < oneZ)) {
                    return false;
                }
            }
     
            double playerY = playerLoc.getY();
            double oneY = locOne.getY();
            double twoY = locTwo.getY();
     
            // Y Axis
            if (oneY >= twoY) {
                if ((playerY  >= oneY) || (playerY < twoY)) {
                    return false;
                }
            } else {
                if ((playerY  >= twoY) || (playerY < oneY)) {
                    return false;
                }
            }
            return true;
        }
     
    Prgr likes this.
  8. Offline

    pzxc

    This is much simpler:

    Code:
    double x1=locOne.getX(), y1=locOne.getY(), z1=locOne.getZ();
    double x2=locTwo.getX(), y2=locTwo.getY(), z2=locTwo.getZ();
    double minx=Math.min(x1,x2), miny=Math.min(y1,y2), minz=Math.min(z1,z2);
    double maxx=Math.max(x1,x2), maxy=Math.max(y1,y2), maxz=Math.max(z1,z2);
    double px=player.getLocation().getX(), py=player.getLocation().getY(), pz=player.getLocation().getZ();
    return px >= minx && px <= maxx && py >= miny && py <= maxy && pz >= minz && pz <= maxz;
     
    CrankkDatJFel likes this.
  9. Offline

    CrankkDatJFel

    Very nice solution pzxc, thanks!
     
Thread Status:
Not open for further replies.

Share This Page