Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //SAT intersection test. http://www.metanetsoftware.com/technique/tutorialA.html
- //returns true on intersection, and sets the least intersecting axis in overlap
- static final PointF overlap = new PointF(0,0); //Q&D PointF pool for collision detection. Assumes single threading.
- @SuppressWarnings("UnusedReturnValue")
- static boolean getOverlap(final Entity a, final Entity b, final PointF overlap) {
- overlap.x = 0.0f;
- overlap.y = 0.0f;
- final float centerDeltaX = a.centerX() - b.centerX();
- final float halfWidths = (a.width() + b.width()) * 0.5f;
- float dx = Math.abs(centerDeltaX);//cache the abs, we need it twice
- if (dx > halfWidths) return false; //no overlap on x == no collision
- final float centerDeltaY = a.centerY() - b.centerY();
- final float halfHeights = (a.height() + b.height()) * 0.5f;
- float dy = Math.abs(centerDeltaY);
- if (dy > halfHeights) return false; //no overlap on y == no collision
- dx = halfWidths - dx; //overlap on x
- dy = halfHeights - dy; //overlap on y
- if (dy < dx) {
- overlap.y = (centerDeltaY < 0) ? -dy : dy;
- } else if (dy > dx) {
- overlap.x = (centerDeltaX < 0) ? -dx : dx;
- } else {
- overlap.x = (centerDeltaX < 0) ? -dx : dx;
- overlap.y = (centerDeltaY < 0) ? -dy : dy;
- }
- return true;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement