-
Notifications
You must be signed in to change notification settings - Fork 1
/
Util.cpp
144 lines (125 loc) · 2.97 KB
/
Util.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
#include "Util.h"
//Box collision detector
bool checkCollision(SDL_Rect a, SDL_Rect b)
{
//The sides of the rectangles
int leftA, leftB;
int rightA, rightB;
int topA, topB;
int bottomA, bottomB;
//Claculate the sides of rect A
leftA = a.x;
rightA = a.x + a.w;
topA = a.y;
bottomA = a.y + a.h;
//Calculate the sides of rect B
leftB = b.x;
rightB = b.x + b.w;
topB = b.y;
bottomB = b.y + b.h;
//If any of the dsides from A are outside of B
if (bottomA <= topB) { return false; }
if (topA >= bottomB) { return false; }
if (rightA <= leftB) { return false; }
if (leftA >= rightB) { return false; }
//If none of the sides from A are outside of B
return true;
}//end static check collision method
//Per Pixel collision detector
bool checkCollision(std::vector<SDL_Rect>& a, std::vector<SDL_Rect>& b)
{
//The sides of the rectangles
int leftA, leftB;
int rightA, rightB;
int topA, topB;
int bottomA, bottomB;
//Go though the boxes
for (int Abox = 0; Abox < a.size(); Abox++)
{
//Calculate the sides of rect A
leftA = a[Abox].x;
rightA = a[Abox].x + a[Abox].w;
topA = a[Abox].y;
bottomA = a[Abox].y + a[Abox].h;
//Go though the B boxes
for (int Bbox = 0; Bbox < b.size(); Bbox++)
{
//Calculate the sides of rect A
leftB = b[Bbox].x;
rightB = b[Bbox].x + b[Bbox].w;
topB = b[Bbox].y;
bottomB = b[Bbox].y + b[Bbox].h;
//If no sides from A are outside of B
if (((bottomA <= topB) || (topA >= bottomB) ||
(leftA >= rightB) || (rightA <= leftB)) == false)
{
//A collision is detected
return true;
}//end if collision is deteched
}//end Bbox Loop
}//end Abox Loop
//If neither set of collision boxes touched
return false;
}//end per pixel collision detection method
//Circle/Circle collision detector
bool checkCollision(Circle& a, Circle& b)
{
//Calculate total radius squared
int totalRadiusSquared = a.r + b.r;
totalRadiusSquared *= totalRadiusSquared;
//If the distance between the centers of the circles is less than the sum of their radii
if (distanceSquared(a.x, a.y, b.x, b.y) < (totalRadiusSquared))
{
//The circles have collided
return true;
}
//if not
return false;
}//end check circle circle collision method
//Circle/Box collision detector
bool checkCollision(Circle& a, SDL_Rect& b)
{
//Closest point on vollision box
int cX, cY;
//Find closest x offset
if (a.x < b.x)
{
cX = b.x;
}
else if (a.x > b.x + b.w)
{
cX = b.x + b.w;
}
else
{
cX = a.x;
}
//Find closest y offset
if (a.y < b.y)
{
cY = b.y;
}
else if (a.y > b.y + b.h)
{
cY = b.y + b.h;
}
else
{
cY = a.y;
}
//If the closest point is inside the circle
if (distanceSquared(a.x, a.y, cX, cY) < a.r * a.r)
{
//This box and cirlce have collided
return true;
}
//If not
return false;
}//end check circle to square collision method
//Calculates distance squared between two points
double distanceSquared(int x1, int y1, int x2, int y2)
{
int deltaX = x2 - x1;
int deltaY = y2 - y1;
return deltaX*deltaX + deltaY*deltaY;
}