-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcollider.cpp
221 lines (196 loc) · 8.59 KB
/
collider.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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
//
// Created by Allen on 3/2/2020.
//
#include <cstdio>
#include "entities.h"
#include "utils.h"
#include "ILI9341.h"
using namespace std;
void HitboxManager::checkCollisions() {
for (uint8_t slot = 0; slot < hurtboxSlots; slot++) {
if(!hurtboxes[slot].active) continue;
if(p2 != nullptr && hurtboxes[slot].source == 1 && p2->hitbox.isColliding(hurtboxes[slot])) {
// hurtbox collision with player 2!
hurtboxes[slot].active = false;
activationFlags[slot] = true;
p2->collide(&hurtboxes[slot], p1);
}
else if(p2 != nullptr && hurtboxes[slot].source == 2 && p1->hitbox.isColliding(hurtboxes[slot])) {
// hurtbox collision with player 1!
hurtboxes[slot].active = false;
activationFlags[slot] = true;
p1->collide(&hurtboxes[slot], p2);
}
else if(hurtboxes[slot].source == 0) {
if(p1->hitbox.isColliding(hurtboxes[slot])) {
p1->collide(&hurtboxes[slot], p2);
}
if(p2 != nullptr && p2->hitbox.isColliding(hurtboxes[slot])) {
p2->collide(&hurtboxes[slot], p1);
}
}
else {
// update hurtbox frame
if(!((persistentHurtbox >> slot) & 1)) {
if(++hurtboxes[slot].currentFrame >= hurtboxes[slot].frames) {
hurtboxes[slot].active = false;
}
}
}
}
}
void HitboxManager::displayHitboxesOverlay() {
uint32_t hitboxColorsub = 0x555500;
uint32_t hurtboxColorsub = 0x007777;
// uint32_t hurtboxColorsub = 0xAA00AA;
// first display player hitboxes
if(p1->hitbox.shape == SHAPE_CIRCLE) {
LCD_drawOverlayCircle(p1->hitbox.x + p1->hitbox.xOffset,
p1->hitbox.y + p1->hitbox.yOffset,
p1->hitbox.radius + p1->hitbox.radiusOffset, hitboxColorsub);
}
else if(p1->hitbox.shape == SHAPE_RECTANGLE) {
LCD_drawOverlayRectangle(p1->hitbox.x + p1->hitbox.xOffset,
p1->hitbox.y + p1->hitbox.yOffset,
p1->hitbox.width + p1->hitbox.widthOffset,
p1->hitbox.height + p1->hitbox.heightOffset, hitboxColorsub);
}
if(p2 != nullptr) {
if (p2->hitbox.shape == SHAPE_CIRCLE) {
LCD_drawOverlayCircle(p2->hitbox.x + p2->hitbox.xOffset,
p2->hitbox.y + p2->hitbox.yOffset,
p2->hitbox.radius + p2->hitbox.radiusOffset, hitboxColorsub);
} else if (p2->hitbox.shape == SHAPE_RECTANGLE) {
LCD_drawOverlayRectangle(p2->hitbox.x + + p2->hitbox.xOffset,
p2->hitbox.y + p2->hitbox.yOffset,
p2->hitbox.width + p2->hitbox.widthOffset,
p2->hitbox.height + p2->hitbox.heightOffset, hitboxColorsub);
}
}
// next, draw hurtboxes
for (uint8_t slot = 0; slot < hurtboxSlots; slot++) {
if(hurtboxes[slot].active) {
if(hurtboxes[slot].shape == SHAPE_CIRCLE) {
LCD_drawOverlayCircle(hurtboxes[slot].x + hurtboxes[slot].xOffset,
hurtboxes[slot].y + hurtboxes[slot].yOffset,
hurtboxes[slot].radius, hurtboxColorsub);
}
else if(hurtboxes[slot].shape == SHAPE_RECTANGLE) {
LCD_drawOverlayRectangle(hurtboxes[slot].x + hurtboxes[slot].xOffset,
hurtboxes[slot].y + hurtboxes[slot].yOffset,
hurtboxes[slot].width, hurtboxes[slot].height, hurtboxColorsub);
}
}
}
}
void HitboxManager::clearHitboxOverlay() {
LCD_clearOverlay();
}
bool* HitboxManager::addHurtboxFullConfig(float xOffset, float yOffset, bool mirrored,
class Hurtbox hurtBox, uint8_t playerSource, bool persistent) {
hurtBox.active = true;
hurtBox.currentFrame = 0;
if(mirrored) hurtBox.x = xOffset-hurtBox.x;
else hurtBox.x += xOffset;
hurtBox.y += yOffset;
hurtBox.setSource(playerSource);
// add the hurtbox to the array, find a slot for it
uint8_t slot = 0;
for(slot = 0; slot < hurtboxSlots; slot++) {
if(!hurtboxes[slot].active) {
if(persistent) persistentHurtbox |= 1u << slot;
hurtboxes[slot] = hurtBox;
activationFlags[slot] = false;
return &activationFlags[slot];
}
}
printf("No more hitbox slots!\n");
return nullptr; // no slots remaining
}
bool* HitboxManager::addHurtbox(float xOffset, float yOffset, bool mirrored,
class Hurtbox hurtBox, uint8_t playerSource, float multiplier) {
hurtBox.damage *= multiplier;
hurtBox.yKnockback *= multiplier;
hurtBox.xKnockback *= multiplier;
return this->addHurtboxFullConfig(xOffset, yOffset, mirrored, hurtBox, playerSource, false);
}
bool Hitbox::isColliding(class Hurtbox hurtbox) {
float thisx = x + xOffset;
float thisy = y + yOffset;
float thisRadius = radius + radiusOffset;
float thisWidth = width + widthOffset;
float thisHeight = height + heightOffset;
float hbx = hurtbox.x + hurtbox.xOffset;
float hby = hurtbox.y + hurtbox.yOffset;
float hbh = hurtbox.height;
float hbw = hurtbox.width;
float hbr = hurtbox.radius;
switch (this->shape) {
case SHAPE_CIRCLE:
if(hurtbox.shape == SHAPE_CIRCLE) {
/*
* Find the distance between the two circles.
* If this distance is less than the sum of the radii
* of the circles, they're colliding.
*/
return absVal((thisx - hbx) * (thisx - hbx)
+ (thisy - hby) * (thisy - hby))
< (thisRadius + hbr) * (thisRadius + hbr);
}
else if(hurtbox.shape == SHAPE_RECTANGLE) {
double distanceX = absVal(thisx - hbx);
double distanceY = absVal(thisy - hby);
/*
* If the distance between the two objects are greater
* than the radius and the distance from center to edge combined,
* there is definitely no collision
*/
if (distanceX > (hbw/2 + thisRadius)) { return false; }
if (distanceY > (hbh/2 + thisRadius)) { return false; }
/*
* If the center of the circle is inside the rectangle,
* there's a collision.
*/
if (distanceX <= (hbw/2)) { return true; }
if (distanceY <= (hbh/2)) { return true; }
/*
* Check if the circle intersects rectangle corner
*/
double cornerDistance_sq = (distanceX - hbw/2)*(distanceX - hbw/2) +
(distanceY - hbh/2)*(distanceY - hbh/2);
return (cornerDistance_sq <= (this->radius * thisRadius));
}
case SHAPE_RECTANGLE:
if(hurtbox.shape == SHAPE_CIRCLE) {
double distanceX = absVal(thisx - hbx);
double distanceY = absVal(thisy - hby);
/*
* If the distance between the two objects are greater
* than the radius and the distance from center to edge combined,
* there is definitely no collision
*/
if (distanceX > (thisWidth/2 + hbr)) { return false; }
if (distanceY > (thisHeight/2 + hbr)) { return false; }
/*
* If the center of the circle is inside the rectangle,
* there's a collision.
*/
if (distanceX <= (thisWidth/2)) { return true; }
if (distanceY <= (thisHeight/2)) { return true; }
/*
* Check if the circle intersects rectangle corner
*/
double cornerDistance_sq =
(distanceX - thisWidth/2)*(distanceX - thisWidth/2) +
(distanceY - thisHeight/2)*(distanceY - thisHeight/2);
return (cornerDistance_sq <= (hbr*hbr));
}
else if(hurtbox.shape == SHAPE_RECTANGLE) {
return thisx + thisWidth >= hbx && // r1 right edge past r2 left
thisx <= hbx + hbw && // r1 left edge past r2 right
thisy + thisHeight >= hby && // r1 top edge past r2 bottom
thisy <= hby + hbh; // r1 bottom edge past r2 top
}
}
return false;
}