X-Git-Url: http://git.localhorst.tv/?a=blobdiff_plain;f=src%2Fworld%2FAABB.cpp;h=92c8ba2014f017632c54e7c50074010f8f973a86;hb=a73a6dd63407b5f5ef5b0c635551ad27b27c95d6;hp=c54537e16ee9f6e40b99b75e552a7f720fe2ab6e;hpb=4a51a83bdff30d1e25a5867cfb19936adc0034b1;p=orbi.git diff --git a/src/world/AABB.cpp b/src/world/AABB.cpp index c54537e..92c8ba2 100644 --- a/src/world/AABB.cpp +++ b/src/world/AABB.cpp @@ -1,39 +1,53 @@ #include "AABB.h" +#include "Collision.h" +#include "../graphics/const.h" + + namespace orbi { -bool AABB::Intersects(const AABB &other, Vector &p, Vector &n, Vector &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 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(Right(), ((Top() + Bottom()) / 2 + (other.Top() + other.Bottom()) / 2) / 2); - n = Vector(-1, 0); - d = Vector(other.Left() - Right(), 0); + coll.pos.x = Right(); + coll.norm.x = -1; + coll.depth.x = other.Left() - Right(); } else { - p = Vector(Left(), ((Top() + Bottom()) / 2 + (other.Top() + other.Bottom()) / 2) / 2); - n = Vector(1, 0); - d = Vector(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(((Left() + Right()) / 2 + (other.Left() + other.Right()) / 2) / 2, Bottom()); - n = Vector(0, -1); - d = Vector(0, other.Top() - Bottom()); + coll.pos.y = Bottom(); + coll.norm.y = -1; + coll.depth.y = other.Top() - Bottom(); } else { - p = Vector(((Left() + Right()) / 2 + (other.Left() + other.Right()) / 2) / 2, Top()); - n = Vector(0, 1); - d = Vector(0, other.Bottom() - Top()); + coll.pos.y = Top(); + coll.norm.y = 1; + coll.depth.y = other.Bottom() - Top(); } } return true;