-
Notifications
You must be signed in to change notification settings - Fork 0
/
AABB.cpp
155 lines (119 loc) · 2.89 KB
/
AABB.cpp
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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
#include "AABB.h"
#include <SFML/OpenGL.hpp>
AABB::AABB()
: m_lowerBound(0.0f, 0.0f), m_upperBound(1.0f, 1.0f),
m_center(0.5f, 0.5f), m_halfDims(0.5f, 0.5f)
{
}
AABB::AABB(const Vec2f &lowerBound, const Vec2f &upperBound)
: m_lowerBound(lowerBound), m_upperBound(upperBound)
{
CalculateHalfDims();
CalculateCenter();
}
void AABB::CalculateHalfDims()
{
m_halfDims = (m_upperBound - m_lowerBound) / 2.0f;
}
void AABB::CalculateCenter()
{
m_center = m_lowerBound + m_halfDims;
}
void AABB::CalculateBounds()
{
m_lowerBound = m_center - m_halfDims;
m_upperBound = m_center + m_halfDims;
}
const Vec2f &AABB::GetCenter() const
{
return m_center;
}
Vec2f AABB::GetDims() const
{
return m_upperBound - m_lowerBound;
}
const Vec2f &AABB::GetHalfDims() const
{
return m_halfDims;
}
const Vec2f &AABB::GetLowerBound() const
{
return m_lowerBound;
}
const Vec2f &AABB::GetUpperBound() const
{
return m_upperBound;
}
AABB AABB::GetRotatedAABB(float angleRads) const
{
// Get new dimensions
float cosOfAngle = cosf(angleRads);
float sinOfAngle = sinf(angleRads);
Vec2f newHalfDims(m_halfDims.y * sinOfAngle + m_halfDims.x * cosOfAngle, m_halfDims.x * sinOfAngle + m_halfDims.y * cosOfAngle);
return AABB(m_center - newHalfDims, m_center + newHalfDims);
}
void AABB::SetCenter(const Vec2f &newCenter)
{
m_center = newCenter;
CalculateBounds();
}
void AABB::IncCenter(const Vec2f &increment)
{
m_center += increment;
CalculateBounds();
}
void AABB::SetDims(const Vec2f &newDims)
{
SetHalfDims(newDims / 2.0f);
}
void AABB::SetHalfDims(const Vec2f &newDims)
{
m_halfDims = newDims;
CalculateBounds();
}
void AABB::SetRotatedAABB(float angleRads)
{
float cosOfAngle = cosf(angleRads);
float sinOfAngle = sinf(angleRads);
m_halfDims.x = m_halfDims.y * sinOfAngle + m_halfDims.x * cosOfAngle;
m_halfDims.y = m_halfDims.x * sinOfAngle + m_halfDims.y * cosOfAngle;
}
bool AABB::Intersects(const AABB &other) const
{
if(m_upperBound.x < other.m_lowerBound.x)
return false;
if(m_upperBound.y < other.m_lowerBound.y)
return false;
if(m_lowerBound.x > other.m_upperBound.x)
return false;
if(m_lowerBound.y > other.m_upperBound.y)
return false;
return true;
}
bool AABB::Contains(const AABB &other) const
{
if(other.m_lowerBound.x >= m_lowerBound.x &&
other.m_upperBound.x <= m_upperBound.x &&
other.m_lowerBound.y >= m_lowerBound.y &&
other.m_upperBound.y <= m_upperBound.y)
return true;
return false;
}
void AABB::DebugRender()
{
// Render the AABB with lines
glBegin(GL_LINES);
// Bottom
glVertex2f(m_lowerBound.x, m_lowerBound.y);
glVertex2f(m_upperBound.x, m_lowerBound.y);
// Right
glVertex2f(m_upperBound.x, m_lowerBound.y);
glVertex2f(m_upperBound.x, m_upperBound.y);
// Top
glVertex2f(m_upperBound.x, m_upperBound.y);
glVertex2f(m_lowerBound.x, m_upperBound.y);
// Left
glVertex2f(m_lowerBound.x, m_upperBound.y);
glVertex2f(m_lowerBound.x, m_lowerBound.y);
glEnd();
}