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