#include "AABB.h"
+#include "Collision.h"
+#include "../graphics/const.h"
+
+
namespace orbi {
-bool AABB::Intersects(const AABB &other, Vector<float> &p, Vector<float> &n, Vector<float> &d) const {
+bool AABB::Intersects(const AABB &other) const {
if (Bottom() < other.Top()) return false;
if (other.Bottom() < Top()) return false;
if (Right() < other.Left()) return false;
if (other.Right() < Left()) return false;
+ return true;
+}
+
+bool AABB::Intersects(const AABB &other, Collision &coll) const {
+ if (!Intersects(other)) return false;
AABB diff;
- diff.lt.x = std::max(Left(), other.Left());
- diff.lt.y = std::max(Top(), other.Top());
- diff.rb.x = std::min(Right(), other.Right());
- diff.rb.y = std::min(Bottom(), other.Bottom());
+ diff.lt = max(lt, other.lt);
+ diff.rb = min(rb, other.rb);
const Vector<float> sdiff = diff.Size();
if (sdiff.x < sdiff.y) {
+ coll.pos.y = diff.Center().y;
+ coll.norm.y = 0;
+ coll.depth.y = sdiff.y * sigma(Center().y - other.Center().y);
if (Center().x < other.Center().x) {
- p = Vector<float>(Right(), ((Top() + Bottom()) / 2 + (other.Top() + other.Bottom()) / 2) / 2);
- n = Vector<float>(-1, 0);
- d = Vector<float>(other.Left() - Right(), 0);
+ coll.pos.x = Right();
+ coll.norm.x = -1;
+ coll.depth.x = other.Left() - Right();
} else {
- p = Vector<float>(Left(), ((Top() + Bottom()) / 2 + (other.Top() + other.Bottom()) / 2) / 2);
- n = Vector<float>(1, 0);
- d = Vector<float>(other.Right() - Left(), 0);
+ coll.pos.x = Left();
+ coll.norm.x = 1;
+ coll.depth.x = other.Right() - Left();
}
} else {
+ coll.pos.x = diff.Center().x;
+ coll.norm.x = 0;
+ coll.depth.x = 0;
+ coll.depth.x = sdiff.x * sigma(Center().x - other.Center().x);
if (Center().y < other.Center().y) {
- p = Vector<float>(((Left() + Right()) / 2 + (other.Left() + other.Right()) / 2) / 2, Bottom());
- n = Vector<float>(0, -1);
- d = Vector<float>(0, other.Top() - Bottom());
+ coll.pos.y = Bottom();
+ coll.norm.y = -1;
+ coll.depth.y = other.Top() - Bottom();
} else {
- p = Vector<float>(((Left() + Right()) / 2 + (other.Left() + other.Right()) / 2) / 2, Top());
- n = Vector<float>(0, 1);
- d = Vector<float>(0, other.Bottom() - Top());
+ coll.pos.y = Top();
+ coll.norm.y = 1;
+ coll.depth.y = other.Bottom() - Top();
}
}
return true;