-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathCollisionRegion.cpp
285 lines (262 loc) · 9.46 KB
/
CollisionRegion.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
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
/*
Penjin is Copyright (c)2005, 2006, 2007, 2008, 2009, 2010 Kevin Winfield-Pantoja
This file is part of Penjin.
Penjin is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
Penjin is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License
along with Penjin. If not, see <http://www.gnu.org/licenses/>.
*/
#include "CollisionRegion.h"
#include "NumberUtility.h"
CollisionRegion::CollisionRegion()
{
//ctor
noCollision = WHITE;
map = NULL;
showRect = NULL;
region.x = 0;
region.y = 0;
region.w = 0;
region.h = 0;
pos = Vector2di(0,0);
}
CollisionRegion::~CollisionRegion()
{
delete map;
delete showRect;
}
void CollisionRegion::generateHitRegion()
{
if (map == NULL)
return;
float l,t,r,b;
r = b = 0;
l = map->getWidth();
t = map->getHeight();
// starting on the bottom left, go to the top right of the image
// determines the smallest bounding box aroudn the colliding object
for (float I = map->getWidth()-1; I >= 0; --I)
{
for (float K = map->getHeight()-1; K >= 0; --K)
{
if (map->getPixelInFrame(I,K,0) != noCollision)
{
r = max(r,I);
b = max(b,K);
l = min(l,I);
t = min(t,K);
}
}
}
region.x = l;
region.y = t;
region.w = r-l+1;
region.h = b-t+1;
}
Colour CollisionRegion::getCollisionType(float x, float y, CRbool absolute) const
{
if (absolute)
{
x -= getX();
y -= getY();
}
if (map == NULL)
{
// bounds check
if (x < 0 || y < 0 || x >= getWidth() || y >= getHeight())
return noCollision;
else
{
if (noCollision == Colour(WHITE))
return BLACK;
else
return WHITE;
}
}
else
{
// bounds check
if(x < 0 || y < 0 || x >= getWidth() || y >= getHeight())
return noCollision;
// add the region offset here so we can work with getX / getY as the top / left corner pixel
return map->getPixelInFrame(x + region.x,y + region.y,0);
}
} // check what sort of collision has been made.
bool CollisionRegion::hitTest(const CollisionRegion* const tester, CRbool fullShape) const
{
if (fullShape)
{
// determine overlaping area
float xPos = max(this->getX(), tester->getX());
float yPos = max(this->getY(), tester->getY());
float xPosMax = min(this->getX() + this->getWidth(), tester->getX() + tester->getWidth());
float yPosMax = min(this->getY() + this->getHeight(), tester->getY() + tester->getHeight());
// check for collision
for (float I = xPos; I < xPosMax; ++I)
{
for (float K = yPos; K < yPosMax; ++K)
{
// if both have a collision-pixel at the same spot, we have a collision
if (this->hitTest(I,K,true) && tester->hitTest(I,K,true))
return true;
}
}
return false;
}
else
{
// check rectangular bounds only
if (((tester->getX() - this->getX()) < this->getWidth() && (this->getX() - tester->getX()) < tester->getWidth()) &&
((tester->getY() - this->getY()) < this->getHeight() && (this->getY() - tester->getY()) < tester->getHeight())) return true;
return false;
}
}
bool CollisionRegion::hitTest(const CollisionRegion* const tester, const Vector2df& posObj, const Vector2df& posTester, CRbool fullShape) const
{
float objPosX = posObj.x + this->getRegionOffsetX();
float objPosY = posObj.y + this->getRegionOffsetY();
float testerPosX = posTester.x + tester->getRegionOffsetX();
float testerPosY = posTester.y + tester->getRegionOffsetY();
if (fullShape)
{
/// TODO Optimise by testing rectangular collision first (if possible)
/// We only do more expensive per-pixel test if indeed a rectangle collision has happened.
// determine overlaping area
float xPos = max(objPosX, testerPosX);
float yPos = max(objPosY, testerPosY);
float xPosMax = min(objPosX + this->getWidth(), testerPosX + tester->getWidth());
float yPosMax = min(objPosY + this->getHeight(), testerPosY + tester->getHeight());
// check for collision
for (float I = xPos; I < xPosMax; ++I)
{
for (float K = yPos; K < yPosMax; ++K)
{
// if both have a collision-pixel at the same spot, we have a collision
// also substract the region offset here, because hitTest works with it
if (this->hitTest(I - objPosX,K - objPosY,false) && tester->hitTest(I - testerPosX,K - testerPosY,false))
return true;
}
}
return false;
}
else
{
// check rectangular bounds only
if (((testerPosX - objPosX) < this->getWidth() && (objPosX - testerPosX) < tester->getWidth()) &&
((testerPosY - objPosY) < this->getHeight() && (objPosY - testerPosY) < tester->getHeight())) return true;
return false;
}
}
SimpleDirection CollisionRegion::directionTest(const CollisionRegion* const tester, CRbool fullShape) const
{
// check for collision first
if (not hitTest(tester,fullShape))
{
return SimpleDirection(diNONE);
}
// now check collision direction, by a simple check of the overlaping area
else
{
// calculate overlaping pixels
float diffX = min(this->getX() + this->getWidth(), tester->getX() + tester->getWidth()) - max(this->getX(), tester->getX());
float diffY = min(this->getY() + this->getHeight(), tester->getY() + tester->getHeight()) - max(this->getY(), tester->getY());
// check which side overlaps more (relatively)
if (diffX / min(this->getWidth(),tester->getWidth()) > diffY / min(this->getHeight(),tester->getHeight()))
{
// more X -> top/bottom collision
if ((this->getY() + this->getHeight()/2) > (tester->getY() + tester->getHeight()/2))
return SimpleDirection(diTOP);
else
return SimpleDirection(diBOTTOM);
}
else
{
// more Y -> left/right collision
if ((this->getX() + this->getWidth()/2) > (tester->getX() + tester->getWidth()/2))
return SimpleDirection(diLEFT);
else
return SimpleDirection(diRIGHT);
}
}
}
SimpleDirection CollisionRegion::directionTest(const CollisionRegion* const tester, const Vector2df& posObj, const Vector2df& posTester, CRbool fullShape) const
{
// check for collision first
if (not hitTest(tester,posObj,posTester,fullShape))
{
return SimpleDirection(diNONE);
}
// now check collision direction, by a simple check of the overlaping area
else
{
float objPosX = posObj.x + this->getRegionOffsetX();
float objPosY = posObj.y + this->getRegionOffsetY();
float testerPosX = posTester.x + tester->getRegionOffsetX();
float testerPosY = posTester.y + tester->getRegionOffsetY();
// calculate overlaping pixels
float diffX = min(objPosX + this->getWidth(), testerPosX + tester->getWidth()) - max(objPosX, testerPosX);
float diffY = min(objPosY + this->getHeight(), testerPosY + tester->getHeight()) - max(objPosY, testerPosY);
// check which side overlaps more (relatively)
if (diffX / min(this->getWidth(),tester->getWidth()) > diffY / min(this->getHeight(),tester->getHeight()))
{
// more X -> top/bottom collision
if ((objPosY + this->getHeight()/2) > (testerPosY + tester->getHeight()/2))
return SimpleDirection(diTOP);
else
return SimpleDirection(diBOTTOM);
}
else
{
// more Y -> left/right collision
if ((objPosX + this->getWidth()/2) > (testerPosX + tester->getWidth()/2))
return SimpleDirection(diLEFT);
else
return SimpleDirection(diRIGHT);
}
}
}
/*
void CollisionRegion::render()
{
if (map)
{
map->renderImage(pos);
}
if (not showRect && region.w != 0 && region.h != 0)
{
showRect = new Rectangle(region.w,region.h);
showRect->setColour(Colour(GREEN));
showRect->setThickness(max(getWidth() / 100.0, 1.0));
}
if (showRect)
{
showRect->setDimensions(region.w,region.h);
showRect->setPosition(pos.x + region.x,pos.y + region.y);
showRect->render();
}
}*/
void CollisionRegion::render(SDL_Surface* surf)
{
if (map)
{
map->renderImage(surf, pos);
}
if (not showRect && region.w != 0 && region.h != 0)
{
showRect = new Rectangle(region.w,region.h);
showRect->setColour(Colour(GREEN));
showRect->setThickness(max(getWidth() / 100.0, 1.0));
}
if (showRect)
{
showRect->setDimensions(region.w,region.h);
showRect->setPosition(pos.x + region.x,pos.y + region.y);
showRect->render(surf);
}
}