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; } }
@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.
@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.
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; }
@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.
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; }
@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.
@mouz_ If the difference is greater than 180 then set difference to 360-difference and then check if it's less than 25.
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; }
@mouz_ Also check if the difference is less than negative 180 as well. Besides that, this should work.
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; }
Because it is easier for you to test than for us as we dont have your code and arent making your plugin.
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