• API
• FAQ
• Tools
• Trends
• Archive
SHARE
TWEET

# getOverlap (Android game programming)

ulfben Feb 24th, 2017 70 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. public static boolean getOverlap(GameObject a, GameObject b, PointF overlap) {
4.     overlap.set(0f, 0f);
5.     float centerDeltaX = a.mBounds.centerX() - b.mBounds.centerX();
6.     float halfWidths = (a.mWidth + b.mWidth) * 0.5f;
7.
8.     if (Math.abs(centerDeltaX) > halfWidths) return false; //no overlap on x == no collision
9.
10.     float centerDeltaY = a.mBounds.centerY() - b.mBounds.centerY();
11.     float halfHeights = (a.mHeight + b.mHeight) * 0.5f;
12.
13.     if (Math.abs(centerDeltaY) > halfHeights) return false; //no overlap on y == no collision
14.
15.     float dx = halfWidths - Math.abs(centerDeltaX); //overlap on x
16.     float dy = halfHeights - Math.abs(centerDeltaY); //overlap on y
17.     if (dy < dx) {
18.         overlap.y = (centerDeltaY < 0) ? -dy : dy;
19.     } else if (dy > dx) {
20.         overlap.x = (centerDeltaX < 0) ? -dx : dx;
21.     } else {
22.         overlap.x = (centerDeltaX < 0) ? -dx : dx;
23.         overlap.y = (centerDeltaY < 0) ? -dy : dy;
24.     }
25.     return true;
26. }
RAW Paste Data
Top