Direction check

Discussion in 'Plugin Development' started by mouz_, May 8, 2016.

Thread Status:
Not open for further replies.
  1. Hi, how to make this code be more rounded? I want to check if player A is looking in the same direction as player B, but more rounded.
    Code:
        public static String getDirection(Player player) {
            double rotation = (player.getLocation().getYaw() - 90) % 360;
            if (rotation < 0) {
                rotation += 360.0;
            }
             if (0 <= rotation && rotation < 45.5) {
                return "N";
            } else if (45 <= rotation && rotation < 135) {
                return "N";
            } else if (135 <= rotation && rotation < 225) {
                return "E";
            } else if (225 <= rotation && rotation < 315) {
                return "S";
            } else if (315 <= rotation && rotation < 45) {
                return "W";
            } else {
                return null;
            }
        }
     
  2. Offline

    teej107

    @mouz_ What do you mean by
     
  3. Offline

    Zombie_Striker

    @mouz_
    I'm assuming you mean to add Strings such as "NW", "SW","SE", ect. Do this by dividing the rotation by 45 and casting the value to an int.The following should be the case:
    • 0 = east
    • 1 = southeast
    • 2 = south
    • 3 = southwest
    • ect.
     
  4. Why would I need extra strings? I just want to check the directions of 2 players to match it.
     
    Last edited by a moderator: May 10, 2016
  5. Offline

    567legodude

    @mouz_ You could get both player's directions and subtract them, then take the absolute value, then see if the number is less than say 10 you know both players are looking in the same direction +/- 10 degrees.
     
  6. Like that?
    Code:
        public static boolean getDirections(Player a, Player v) {
            double rotationA = (a.getLocation().getYaw() - 90) % 360;
            double rotationV = (v.getLocation().getYaw() - 90) % 360;
            int difference = (int) (rotationA - rotationV);
            if (difference >= 25) {
                return true;
            }
            return false;
        }
     
  7. Offline

    Zombie_Striker

    @mouz_
    In this case, you do not need to subtract 90 from the rotation, nor do you need to cast the difference to an int. Also, you are not getting the absolute value of the difference. This is important, as all negative numbers are less than 25, so this could return true even if they are not looking in the same direction.

    If you fix these things, then this method should work.
     
  8. How about this?
    Code:
        public static boolean getDirections(Player a, Player v) {
            double rotationA = a.getLocation().getYaw() % 360;
            double rotationV = v.getLocation().getYaw() % 360;
            double difference = (rotationA - rotationV);
            if (difference > 0 && difference >= 25) {
                return true;
            }
            else if (difference < 0 && difference <= 25) {
                return true;
            }
            return false;
        }
     
  9. Offline

    567legodude

    @mouz_ Let me show you a magical absolute value method.
    https://docs.oracle.com/javase/8/docs/api/java/lang/Math.html#abs-double-

    You also need to take into account the wraparound. Because people looking in the direction 1 and 359 are basically looking in the same direction, but the difference returns a number bigger than 25. I know how I would do it but see if you can figure out a way to overcome that issue.
     
  10. Sorry, I don't know how to use Math from your link. :(
     
  11. Offline

    567legodude

    @mouz_ Have you never seen this before:
    Code:
    double a = Math.abs(d);
     
  12. Yes, once in my thread.
     
  13. Offline

    567legodude

    @mouz_ Just remember to account for the wraparound like I said.
     
  14. I don't understand.
     
  15. Offline

    567legodude

    @mouz_
     
  16. How do I do that?
     
  17. Offline

    567legodude

    @mouz_ If the difference is greater than 180 then set difference to 360-difference and then check if it's less than 25.
     
  18. Is it good?
    Code:
        public static boolean getDirections(Player a, Player v) {
            double rotationA = a.getLocation().getYaw() % 360;
            double rotationV = v.getLocation().getYaw() % 360;
            double difference = (rotationA - rotationV);
            if (difference > 180 && 360 - difference >= 25) {
                return true;
            }
            if (difference > 0 && difference >= 25) {
                return true;
            }
            else if (difference < 0 && difference <= 25) {
                return true;
            }
            return false;
        }
     
  19. Offline

    Zombie_Striker

    @mouz_
    Also check if the difference is less than negative 180 as well. Besides that, this should work.
     
  20. Is it ready?
    Code:
        public static boolean getDirections(Player a, Player v) {
            double rotationA = a.getLocation().getYaw() % 360;
            double rotationV = v.getLocation().getYaw() % 360;
            double difference = (rotationA - rotationV);
            if (difference > 180 && 360 - difference >= 25) {
                return true;
            }
            if (difference > -180 && 360 - difference >= 25) {
                return true;
            }
            if (difference > 0 && difference >= 25) {
                return true;
            }
            else if (difference < 0 && difference <= 25) {
                return true;
            }
            return false;
        }
    
     
  21. Offline

    bennie3211

    Why don't you try it before asking?
     
  22. Why should I?
     
  23. Online

    timtower Administrator Administrator Moderator

    Because it is easier for you to test than for us as we dont have your code and arent making your plugin.
     
  24. Offline

    mcdorli

    Just a hint, it isn't. You check if the difference is bigger than 0 AND bigger or equals to 25.
     
  25. Offline

    bennie3211

    Why? Hmhm
    • It's your own code.
    • You have to learn from it by using debug messages.
    You can also use the dot product to calculate if the directions from both players are within 25 degree range. The dot product check not only yaw but also pitch. Can be modified to yaw only when you set certain vector components to the same value
     
Thread Status:
Not open for further replies.

Share This Page