Advertisement
DarkRevenant

Untitled

Apr 17th, 2014
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.13 KB | None | 0 0
  1.  
  2. import org.lazywizard.lazylib.FastTrig;
  3. import org.lazywizard.lazylib.MathUtils;
  4. import org.lazywizard.lazylib.VectorUtils;
  5. import org.lwjgl.util.vector.Vector2f;
  6.  
  7. public class Collider {
  8.  
  9.     // Slightly faster than the other methods
  10.     public static boolean isWithinRange(Vector2f a, Vector2f b, float r, float rsquared) {
  11.         float dx = a.x - b.x;
  12.         if (dx > r || dx < -r) {
  13.             return false;
  14.         }
  15.         float dy = a.y - b.y;
  16.         if (dy > r || dy < -r) {
  17.             return false;
  18.         }
  19.         return dx * dx + dy * dy <= rsquared;
  20.     }
  21.  
  22.     public static boolean isWithinRange(Vector2f a, Vector2f b, float r) {
  23.         float dx = a.x - b.x;
  24.         if (dx > r || dx < -r) {
  25.             return false;
  26.         }
  27.         float dy = a.y - b.y;
  28.         if (dy > r || dy < -r) {
  29.             return false;
  30.         }
  31.         return dx * dx + dy * dy <= r * r;
  32.     }
  33.  
  34.     // Slower than the other methods
  35.     public static boolean isWithinRangeSquared(Vector2f a, Vector2f b, float rsquared) {
  36.         float dx = a.x - b.x;
  37.         dx *= dx;
  38.         if (dx > rsquared) {
  39.             return false;
  40.         }
  41.         float dy = a.y - b.y;
  42.         dy *= dy;
  43.         if (dy > rsquared) {
  44.             return false;
  45.         }
  46.         return dx + dy <= rsquared;
  47.     }
  48.  
  49.     // Slightly slower than the other method
  50.     public static boolean pointInEllipse(Vector2f point, Vector2f ellipseCenter, float sideRadius, float forwardRadius, float ellipseAngle) {
  51.         double angle = VectorUtils.getAngle(ellipseCenter, point);
  52.         double squareDistance = MathUtils.getDistanceSquared(point, ellipseCenter);
  53.         double sideRadiusSquared = sideRadius * sideRadius;
  54.         double forwardRadiusSquared = forwardRadius * forwardRadius;
  55.         double cos = FastTrig.cos(angle - ellipseAngle);
  56.         double cosSquared = cos * cos;
  57.         double squareRadius = forwardRadiusSquared + cosSquared * (sideRadiusSquared - forwardRadiusSquared);
  58.         return squareDistance <= squareRadius;
  59.     }
  60.  
  61.     // Slightly faster than the other method
  62.     public static boolean pointInEllipseSquared(Vector2f point, Vector2f ellipseCenter, float sideRadiusSquared, float forwardRadiusSquared, float ellipseAngle) {
  63.         double angle = VectorUtils.getAngle(ellipseCenter, point);
  64.         double squareDistance = MathUtils.getDistanceSquared(point, ellipseCenter);
  65.         double cos = FastTrig.cos(angle - ellipseAngle);
  66.         double cosSquared = cos * cos;
  67.         double squareRadius = forwardRadiusSquared + cosSquared * (sideRadiusSquared - forwardRadiusSquared);
  68.         return squareDistance <= squareRadius;
  69.     }
  70.  
  71.     // Slightly slower than the other method
  72.     public static boolean circleIntersectsEllipse(Vector2f point, float radius, Vector2f ellipseCenter, float sideRadius, float forwardRadius, float ellipseAngle) {
  73.         double angle = VectorUtils.getAngle(ellipseCenter, point);
  74.         double distance = MathUtils.getDistance(point, ellipseCenter) + radius;
  75.         double squareDistance = distance * distance;
  76.         double sideRadiusSquared = sideRadius * sideRadius;
  77.         double forwardRadiusSquared = forwardRadius * forwardRadius;
  78.         double cos = FastTrig.cos(angle - ellipseAngle);
  79.         double cosSquared = cos * cos;
  80.         double squareRadius = forwardRadiusSquared + cosSquared * (sideRadiusSquared - forwardRadiusSquared);
  81.         return squareDistance <= squareRadius;
  82.     }
  83.  
  84.     // Slightly faster than the other method
  85.     public static boolean circleIntersectsEllipseSquared(Vector2f point, float radius, Vector2f ellipseCenter, float sideRadiusSquared, float forwardRadiusSquared, float ellipseAngle) {
  86.         double angle = VectorUtils.getAngle(ellipseCenter, point);
  87.         double distance = MathUtils.getDistance(point, ellipseCenter) + radius;
  88.         double squareDistance = distance * distance;
  89.         double cos = FastTrig.cos(angle - ellipseAngle);
  90.         double cosSquared = cos * cos;
  91.         double squareRadius = forwardRadiusSquared + cosSquared * (sideRadiusSquared - forwardRadiusSquared);
  92.         return squareDistance <= squareRadius;
  93.     }
  94. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement