forked from clickteam-plugin/TileMapViewport
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCollision.cpp
308 lines (250 loc) · 11.3 KB
/
Collision.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
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
#include "Common.h"
#include "Helpers.h"
// Returns true if the given on-screen rectangle collides with the given layer
// region using the
// given tileset (for pixel-testing)
bool checkRectangleOverlap(LPRDATA rdPtr, Layer & layer, Tileset & tileset, Rect rect)
{
if (!rdPtr->p)
return false;
bool fineColl = rdPtr->fineColl;
// Store some frequently used values
const float zoom = rdPtr->zoomColl ? rdPtr->zoom : 1.0f;
float zoomPointX = rdPtr->zoomPointX;
float zoomPointY = rdPtr->zoomPointY;
int viewportWidth = rdPtr->rHo.hoImgWidth;
int viewportHeight = rdPtr->rHo.hoImgHeight;
int tileWidth = layer.settings.tileWidth;
int tileHeight = layer.settings.tileHeight;
float renderTileWidth = tileWidth * zoom;
float renderTileHeight = tileHeight * zoom;
int layerWidth = layer.getWidth();
int layerHeight = layer.getHeight();
int layerPxWidth = layerWidth * renderTileWidth;
int layerPxHeight = layerHeight * renderTileHeight;
// On-screen coordinate
float drawX = layer.getScreenX(rdPtr->cameraX);
float drawY = layer.getScreenY(rdPtr->cameraY);
float viewportXBias = viewportWidth * zoomPointX;
float viewportYBias = viewportHeight * zoomPointY;
drawX = (drawX - viewportXBias) * zoom + viewportXBias;
drawY = (drawY - viewportYBias) * zoom + viewportYBias;
int layerX = drawX + rdPtr->rHo.hoRect.left;
int layerY = drawY + rdPtr->rHo.hoRect.top;
// Not overlapping visible part, exit
if (!rdPtr->outsideColl) {
if (rect.x2 < rdPtr->rHo.hoX - rdPtr->collMargin.left ||
rect.y2 < rdPtr->rHo.hoY - rdPtr->collMargin.top ||
rect.x1 > rdPtr->rHo.hoX + rdPtr->rHo.hoImgWidth + rdPtr->collMargin.right ||
rect.y1 > rdPtr->rHo.hoY + rdPtr->rHo.hoImgHeight + rdPtr->collMargin.bottom)
return false;
}
// Make object coordinates relative to layer's origin
rect.moveBy(-(layerX + rdPtr->rHo.hoAdRunHeader->rh3.rh3DisplayX),
-(layerY + rdPtr->rHo.hoAdRunHeader->rh3.rh3DisplayY));
// Get the tiles that the object overlaps
// Stack of the tile regions that have to be examined
// Whenever an object is on the edge
// The rectangle stack stores the pixel rectangle's rect for the according
// region
const int RECT_MAX = 8;
static Rect rectStack[RECT_MAX];
unsigned rectCount = 1;
// The first region to check is the entire object's overlapped tiles
rectStack[0] = rect;
// Until there are no more rectangles to check...
while (rectCount--) {
// Get the most important rectangle
rect = rectStack[rectCount];
// Wrap the tiles if necessary
unsigned splitOffset = 0;
if (layer.settings.wrapX) {
if (!signmodPair(rect.x1, rect.x2, &splitOffset, layerPxWidth)) {
// Create left half
Rect & split1 = rectStack[rectCount++];
split1 = rect;
split1.x2 = split1.x1 + splitOffset - 1;
if (rectCount < RECT_MAX) {
// Create right half
Rect & split2 = rectStack[rectCount++];
split2 = rect;
split2.x1 = split1.x2 + 1;
}
}
}
if (layer.settings.wrapY) {
if (!signmodPair(rect.y1, rect.y2, &splitOffset, layerPxHeight)) {
// Create top half
Rect & split1 = rectStack[rectCount++];
split1 = rect;
split1.y2 = split1.y1 + splitOffset - 1;
if (rectCount < RECT_MAX) {
// Create bottom half
Rect & split2 = rectStack[rectCount++];
split2 = rect;
split2.y1 = split1.y2 + 1;
}
}
}
// Split occured -> new regions to check added. This one can now be
// ignored
if (splitOffset)
continue;
// Calculate the tiles that this rectangle overlaps
int x1 = floordiv<float>(rect.x1, renderTileWidth);
int y1 = floordiv<float>(rect.y1, renderTileHeight);
int x2 = floordiv<float>(rect.x2 - 1, renderTileWidth);
int y2 = floordiv<float>(rect.y2 - 1, renderTileHeight);
if (x2 < 0 || y2 < 0 || x1 >= layerWidth || y1 >= layerHeight)
continue;
x1 = max(0, min(x1, layerWidth - 1));
x2 = max(0, min(x2, layerWidth - 1));
y1 = max(0, min(y1, layerHeight - 1));
y2 = max(0, min(y2, layerHeight - 1));
// Check for any overlapping tile
for (int x = x1; x <= x2; ++x) {
for (int y = y1; y <= y2; ++y) {
Tile * tile = layer.getTile(x, y);
if (tile->id != Tile::EMPTY) {
// Apply overlap filters
bool filtered = false;
for (unsigned f = 0; f < rdPtr->ovlpFilterCount; ++f) {
filtered = true;
OVERLAPFLT & filter = rdPtr->ovlpFilters[f];
int value = 0;
const SubLayer * subLayer;
switch (filter.type) {
case OFT_SUBLAYER:
if (subLayer = rdPtr->sublayerCache[filter.param]) {
subLayer->getCellSafe(x, y, &value);
if (value == filter.value)
filtered = false;
}
break;
case OFT_TILESETX:
if (tile->x == filter.value)
filtered = false;
break;
case OFT_TILESETY:
if (tile->y == filter.value)
filtered = false;
break;
case OFT_TILESETRANGE:
TileRange tr = *(TileRange *)&filter.value;
if (tr.isWithin(*tile))
filtered = false;
break;
}
}
// This tile is uninteresting for the given filter
if (filtered)
continue;
// Bounding box collisions - we're done
if (!fineColl || zoom < 0.1f)
return true;
// Get bounding box of tile
Rect tileBounds;
tileBounds.x1 = renderTileWidth * x;
tileBounds.y1 = renderTileHeight * y;
tileBounds.x2 = tileBounds.x1 + renderTileWidth;
tileBounds.y2 = tileBounds.y1 + renderTileHeight;
// Get pixel offset of tile in tileset
int tilesetX = tile->x * tileWidth;
int tilesetY = tile->y * tileHeight;
// Get intersection box (relative to tile)
Rect intersect;
intersect.x1 = max(rect.x1, tileBounds.x1) - tileBounds.x1 + tilesetX;
intersect.y1 = max(rect.y1, tileBounds.y1) - tileBounds.y1 + tilesetY;
intersect.x2 = min(rect.x2 - 1, tileBounds.x2 - 1) - tileBounds.x1 + tilesetX;
intersect.y2 = min(rect.y2 - 1, tileBounds.y2 - 1) - tileBounds.y1 + tilesetY;
intersect.x1 /= zoom;
intersect.y1 /= zoom;
intersect.x2 /= zoom;
intersect.y2 /= zoom;
cSurface * surface = tileset.surface;
bool alpha = surface->HasAlpha() != 0;
// Check by alpha channel
if (alpha) {
cSurface * alphaSurf = rdPtr->cndAlphaSurf;
for (int iY = intersect.y1; iY <= intersect.y2; ++iY)
for (int iX = intersect.x1; iX <= intersect.x2; ++iX)
if (alphaSurf->GetPixelFast8(iX, iY) > 0)
return true;
}
// Check by transparent color
else {
COLORREF transpCol = surface->GetTransparentColor();
for (int iY = intersect.y1; iY <= intersect.y2; ++iY)
for (int iX = intersect.x1; iX <= intersect.x2; ++iX)
if (surface->GetPixelFast(iX, iY) != transpCol)
return true;
}
//printf("POST %d, %d\n", intersect.x2 - intersect.x1, intersect.y2 - intersect.y1);
}
}
}
}
return false;
}
// Returns true if the given coordinate is solid
bool checkPixelSolid(LPRDATA rdPtr, Layer & layer, Tileset & tileset, int pixelX, int pixelY)
{
float zoom = rdPtr->zoomColl ? rdPtr->zoom : 1.0f;
// Store tile size
int tileWidth = layer.settings.tileWidth;
int tileHeight = layer.settings.tileHeight;
// Compute layer position on screen
int layerX = layer.getScreenX(rdPtr->cameraX) + rdPtr->rHo.hoRect.left;
int layerY = layer.getScreenY(rdPtr->cameraY) + rdPtr->rHo.hoRect.top;
// Get layer size in px
int layerWidth = layer.getWidth();
int layerHeight = layer.getHeight();
// Not overlapping visible part, exit
if (!rdPtr->outsideColl) {
if (pixelX < rdPtr->rHo.hoX || pixelY < rdPtr->rHo.hoY ||
pixelX > rdPtr->rHo.hoX + rdPtr->rHo.hoImgWidth ||
pixelY > rdPtr->rHo.hoY + rdPtr->rHo.hoImgHeight)
return false;
}
// Convert to on-screen coordinates
pixelX -= rdPtr->rHo.hoAdRunHeader->rh3.rh3DisplayX + layerX;
pixelY -= rdPtr->rHo.hoAdRunHeader->rh3.rh3DisplayY + layerY;
// Get the tile that the object overlaps
int tilePosX = floordiv<float>(pixelX, tileWidth * zoom);
int tilePosY = floordiv<float>(pixelY, tileHeight * zoom);
// Limit X coordinate
if (layer.settings.wrapX) {
tilePosX = signmod(tilePosX, layerWidth);
pixelX = signmod(pixelX, layerWidth * tileWidth * zoom);
}
else if (tilePosX < 0 || tilePosX >= layerWidth)
return false;
// Limit Y coordinate
if (layer.settings.wrapY) {
tilePosY = signmod(tilePosY, layerHeight);
pixelY = signmod(pixelY, layerHeight * tileHeight * zoom);
}
else if (tilePosY < 0 || tilePosY >= layerHeight)
return false;
// Check overlapping tile
Tile * tile = layer.getTile(tilePosX, tilePosY);
if (tile->id == Tile::EMPTY)
return false;
// No fine collision? We're done...
if (!rdPtr->fineColl)
return true;
// Get the pixel in the tile that we have to check...
int tilesetX = pixelX + (tile->x - tilePosX) * tileWidth;
int tilesetY = pixelY + (tile->y - tilePosY) * tileHeight;
cSurface * surface = tileset.surface;
if (!surface)
return false;
bool alpha = surface->HasAlpha() != 0;
if (alpha) {
cSurface * alphaSurf = surface->GetAlphaSurface();
bool result = alphaSurf->GetPixelFast8(tilesetX, tilesetY) != 0;
surface->ReleaseAlphaSurface(alphaSurf);
return result;
}
return surface->GetPixelFast(tilesetX, tilesetY) != surface->GetTransparentColor();
}