]> git.localhorst.tv Git - orbi.git/blob - src/world/AABB.cpp
entity/entity collision stub
[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) 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         return true;
15 }
16
17 bool AABB::Intersects(const AABB &other, Collision &coll) const {
18         if (!Intersects(other)) return false;
19
20         AABB diff;
21         diff.lt = max(lt, other.lt);
22         diff.rb = min(rb, other.rb);
23         const Vector<float> sdiff = diff.Size();
24
25         if (sdiff.x < sdiff.y) {
26                 coll.pos.y = diff.Center().y;
27                 coll.norm.y = 0;
28                 coll.depth.y = sdiff.y * sigma(Center().y - other.Center().y);
29                 if (Center().x < other.Center().x) {
30                         coll.pos.x = Right();
31                         coll.norm.x = -1;
32                         coll.depth.x = other.Left() - Right();
33                 } else {
34                         coll.pos.x = Left();
35                         coll.norm.x = 1;
36                         coll.depth.x = other.Right() - Left();
37                 }
38         } else {
39                 coll.pos.x = diff.Center().x;
40                 coll.norm.x = 0;
41                 coll.depth.x = 0;
42                 coll.depth.x = sdiff.x * sigma(Center().x - other.Center().x);
43                 if (Center().y < other.Center().y) {
44                         coll.pos.y = Bottom();
45                         coll.norm.y = -1;
46                         coll.depth.y = other.Top() - Bottom();
47                 } else {
48                         coll.pos.y = Top();
49                         coll.norm.y = 1;
50                         coll.depth.y = other.Bottom() - Top();
51                 }
52         }
53         return true;
54 }
55
56 }