]> git.localhorst.tv Git - orbi.git/blob - src/world/AABB.cpp
edce97e313dfe3f832dab4204f6ca0d100dc7643
[orbi.git] / src / world / AABB.cpp
1 #include "AABB.h"
2
3 #include "Collision.h"
4 #include "../graphics/const.h"
5
6
7 namespace orbi {
8
9 bool AABB::Intersects(const AABB &other, Collision &coll) const {
10         if (Bottom() < other.Top()) return false;
11         if (other.Bottom() < Top()) return false;
12         if (Right() < other.Left()) return false;
13         if (other.Right() < Left()) return false;
14
15         AABB diff;
16         diff.lt = max(lt, other.lt);
17         diff.rb = min(rb, other.rb);
18         const Vector<float> sdiff = diff.Size();
19
20         if (sdiff.x < sdiff.y) {
21                 coll.pos.y = diff.Center().y;
22                 coll.norm.y = 0;
23                 coll.depth.y = sdiff.y * sigma(Center().y - other.Center().y);
24                 if (Center().x < other.Center().x) {
25                         coll.pos.x = Right();
26                         coll.norm.x = -1;
27                         coll.depth.x = other.Left() - Right();
28                 } else {
29                         coll.pos.x = Left();
30                         coll.norm.x = 1;
31                         coll.depth.x = other.Right() - Left();
32                 }
33         } else {
34                 coll.pos.x = diff.Center().x;
35                 coll.norm.x = 0;
36                 coll.depth.x = 0;
37                 coll.depth.x = sdiff.x * sigma(Center().x - other.Center().x);
38                 if (Center().y < other.Center().y) {
39                         coll.pos.y = Bottom();
40                         coll.norm.y = -1;
41                         coll.depth.y = other.Top() - Bottom();
42                 } else {
43                         coll.pos.y = Top();
44                         coll.norm.y = 1;
45                         coll.depth.y = other.Bottom() - Top();
46                 }
47         }
48         return true;
49 }
50
51 }