-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAABB.h
59 lines (46 loc) · 1.16 KB
/
AABB.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
#ifndef AABB_H
#define AABB_H
#include "vector.h"
class AABB {
public:
AABB(Vector min, Vector max) {
m_min = min;
m_max = max;
}
Vector min() {
return m_min;
}
Vector max() {
return m_max;
}
bool collides(AABB with, float padding = 0) {
float d1x = (with.min().x() - padding) - (m_max.x() + padding);
float d1y = (with.min().y() - padding) - (m_max.y() + padding);
float d2x = (m_min.x() - padding) - (with.max().x() + padding);
float d2y = (m_min.y() - padding) - (with.max().y() + padding);
if (d1x > 0.0f || d1y > 0.0f) {
return false;
}
if (d2x > 0.0f || d2y > 0.0f) {
return false;
}
return true;
}
AABB translate(Vector vector) {
Vector newMin = min();
newMin.add(vector);
Vector newMax = max();
newMax.add(vector);
return {newMin, newMax};
}
float deltaX() {
return m_max.x() - m_min.x();
}
float deltaY() {
return m_max.y() - m_min.y();
}
private:
Vector m_min = Vector(0, 0);
Vector m_max = Vector(0, 0);
};
#endif