bobmarley12345

fast rotation calculation between 2 points

Jan 14th, 2022 (edited)
1,263
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. public static double angle2d(double cX, double cZ, double pX, double pZ) {
  2.     if (cX == cZ) {
  3.         if (pZ > cZ) {
  4.             return 180;
  5.         }
  6.         else if (pZ < cZ) {
  7.             return 0;
  8.         }
  9.         else {
  10.             throw new RuntimeException("Points were equal");
  11.         }
  12.     }
  13.     else if (pZ == cZ) {
  14.         if (pX > cX) {
  15.             return 90;
  16.         }
  17.         else if (pX < cX) {
  18.             return 270;
  19.         }
  20.         else {
  21.             throw new RuntimeException("Points were equal");
  22.         }
  23.     }
  24.     else {
  25.         double theta = Math.atan2(pX - cX, -(pZ - cZ));
  26.         if (theta < 0.0) {
  27.             theta += 6.2831853071795865d;
  28.         }
  29.         return 57.2957795130823209d * theta;
  30.     }
  31. }
RAW Paste Data