]> git.localhorst.tv Git - orbi.git/blob - src/world/AABB.cpp
initial collision tests
[orbi.git] / src / world / AABB.cpp
1 #include "AABB.h"
2
3 namespace orbi {
4
5 bool AABB::Intersects(const AABB &other, Vector<float> &p, Vector<float> &n, Vector<float> &d) const {
6         if (Bottom() < other.Top()) return false;
7         if (other.Bottom() < Top()) return false;
8         if (Right() < other.Left()) return false;
9         if (other.Right() < Left()) return false;
10
11         AABB diff;
12         diff.lt.x = std::max(Left(), other.Left());
13         diff.lt.y = std::max(Top(), other.Top());
14         diff.rb.x = std::min(Right(), other.Right());
15         diff.rb.y = std::min(Bottom(), other.Bottom());
16         const Vector<float> sdiff = diff.Size();
17
18         if (sdiff.x < sdiff.y) {
19                 if (Center().x < other.Center().x) {
20                         p = Vector<float>(Right(), ((Top() + Bottom()) / 2 + (other.Top() + other.Bottom()) / 2) / 2);
21                         n = Vector<float>(-1, 0);
22                         d = Vector<float>(other.Left() - Right(), 0);
23                 } else {
24                         p = Vector<float>(Left(), ((Top() + Bottom()) / 2 + (other.Top() + other.Bottom()) / 2) / 2);
25                         n = Vector<float>(1, 0);
26                         d = Vector<float>(other.Right() - Left(), 0);
27                 }
28         } else {
29                 if (Center().y < other.Center().y) {
30                         p = Vector<float>(((Left() + Right()) / 2 + (other.Left() + other.Right()) / 2) / 2, Bottom());
31                         n = Vector<float>(0, -1);
32                         d = Vector<float>(0, other.Top() - Bottom());
33                 } else {
34                         p = Vector<float>(((Left() + Right()) / 2 + (other.Left() + other.Right()) / 2) / 2, Top());
35                         n = Vector<float>(0, 1);
36                         d = Vector<float>(0, other.Bottom() - Top());
37                 }
38         }
39         return true;
40 }
41
42 }