Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <cmath>
- //AABB intersection test
- bool isColliding(const Entity& lhs, const Entity& rhs) {
- SDL_assert(&lhs != &rhs);
- return !(lhs._x + lhs._width < rhs._x ||
- rhs._x + rhs._width < lhs._x ||
- lhs._y + lhs._height < rhs._y ||
- rhs._y + rhs._height < lhs._y);
- }
- //SAT intersection test. http://www.metanetsoftware.com/technique/tutorialA.html
- //Many thanks to Jerry Jonsson and Tommi Lipponen at Uppsala University, GAME,
- //for the original implementation of this function: http://pastebin.com/cbJWTfb9
- bool getOverlap(const Entity& lhs, const Entity& rhs, int& overlapX, int& overlapY) {
- SDL_assert(&lhs != &rhs);
- overlapX = 0;
- overlapY = 0;
- int centerDeltaX = static_cast<int>(lhs.centerX() - rhs.centerX());
- int halfWidths = (lhs._width + rhs._width) / 2;
- if (std::abs(centerDeltaX) > halfWidths) return false; //no overlap on x == no collision
- int centerDeltaY = static_cast<int>(lhs.centerY() - rhs.centerY());
- int halfHeights = (lhs._height + rhs._height) / 2;
- if (std::abs(centerDeltaY) > halfHeights) return false; //no overlap on y == no collision
- int dx = halfWidths - std::abs(centerDeltaX); //overlap on x
- int dy = halfHeights - std::abs(centerDeltaY); //overlap on y
- if (dy < dx) {
- overlapY = (centerDeltaY < 0) ? -dy : dy;
- } else if (dy > dx) {
- overlapX = (centerDeltaX < 0) ? -dx : dx;
- } else {
- overlapX = (centerDeltaX < 0) ? -dx : dx;
- overlapY = (centerDeltaY < 0) ? -dy : dy;
- }
- return true;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement