Advertisement
ulfben

getOverlap (Android game programming)

Feb 24th, 2017
392
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.41 KB | None | 0 0
  1. //SAT intersection test. http://www.metanetsoftware.com/technique/tutorialA.html
  2.     //returns true on intersection, and sets the least intersecting axis in overlap
  3.     static final PointF overlap = new PointF(0,0); //Q&D PointF pool for collision detection. Assumes single threading.
  4.     @SuppressWarnings("UnusedReturnValue")
  5.     static boolean getOverlap(final Entity a, final Entity b, final PointF overlap) {
  6.         overlap.x = 0.0f;
  7.         overlap.y = 0.0f;
  8.         final float centerDeltaX = a.centerX() - b.centerX();
  9.         final float halfWidths = (a.width() + b.width()) * 0.5f;
  10.         float dx = Math.abs(centerDeltaX);//cache the abs, we need it twice
  11.  
  12.         if (dx > halfWidths) return false; //no overlap on x == no collision
  13.  
  14.         final float centerDeltaY = a.centerY() - b.centerY();
  15.         final float halfHeights = (a.height() + b.height()) * 0.5f;
  16.         float dy = Math.abs(centerDeltaY);
  17.  
  18.         if (dy > halfHeights) return false; //no overlap on y == no collision
  19.  
  20.         dx = halfWidths - dx; //overlap on x
  21.         dy = halfHeights - dy; //overlap on y
  22.         if (dy < dx) {
  23.             overlap.y = (centerDeltaY < 0) ? -dy : dy;
  24.         } else if (dy > dx) {
  25.             overlap.x = (centerDeltaX < 0) ? -dx : dx;
  26.         } else {
  27.             overlap.x = (centerDeltaX < 0) ? -dx : dx;
  28.             overlap.y = (centerDeltaY < 0) ? -dy : dy;
  29.         }
  30.         return true;
  31.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement