daily pastebin goal
16%
SHARE
TWEET

getOverlap (Android game programming)

ulfben Feb 24th, 2017 (edited) 116 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  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.     }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top