-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcollisions.c
360 lines (317 loc) · 11.5 KB
/
collisions.c
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
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
/*
* The "Clown" 3D Game Engine
* Designed, debugged and perfected
* by Bl0ckeduser, 2011.
*
* Edge evasion added May 2012
*
* (i.e. on paper it wasn't this
* complicated and full of workarounds)
*/
/*
* Homegrown AABB collision detector and
* resolver
*/
#include <stdio.h>
#include <stdlib.h>
#include "headers.h"
int which;
/* Compares using absolute values, equivalent
to measuring the magnitude of a 1-dimensional
vector. Ignores zero-length values as
result candidates. */
float smallestOfThree(float a, float b, float c)
{
float aa, ab, ac;
/* Absolute value */
aa = a > 0.0 ? a : -a;
ab = b > 0.0 ? b : -b;
ac = c > 0.0 ? c : -c;
/* Ignore zero length */
aa = aa == 0.0 ? ab : aa;
ab = ab == 0.0 ? aa : ab;
ac = ac == 0.0 ? aa : ac;
if(ab >= aa && ac >= aa) return a;
if(aa >= ab && ac >= ab) return b;
if(aa >= ac && ab >= ac) return c;
return 0.0;
}
/* this macro eliminates redundancies in resolveCollisions. */
#define resolveCollisions_checkVertex(XTYPE, YTYPE, ZTYPE, jump) \
if(node->box.XTYPE.x > node2->box.min.x && \
node->box.YTYPE.y > node2->box.min.y && \
node->box.ZTYPE.z > node2->box.min.z && \
node->box.XTYPE.x < node2->box.max.x && \
node->box.YTYPE.y < node2->box.max.y && \
node->box.ZTYPE.z < node2->box.max.z) \
{ \
/* x_offs, y_offs, z_offs: \
difference between this vertex's coords and \
where it should be to resolve the \
collision. Destination vertices \
are chosen based on movement \
vector */ \
\
if(node->box.move.x < 0) \
x_offs = node2->box.max.x - \
node->box.XTYPE.x; /* vertex x */ \
else \
x_offs = node2->box.min.x - \
node->box.XTYPE.x; /* vertex x */ \
\
if(node->box.move.y < 0) \
y_offs = node2->box.max.y - \
node->box.YTYPE.y; /* vertex y */ \
else \
y_offs = node2->box.min.y - /* vertex y */ \
node->box.YTYPE.y; \
\
if(node->box.move.z < 0) \
z_offs = node2->box.max.z - \
node->box.ZTYPE.z; /* vertex z */ \
else \
z_offs = node2->box.min.z - /* vertex z */ \
node->box.ZTYPE.z; \
\
\
/* The "reaction" vector translates the current object in reaction \
to the collision, resolving the collision. \
\
It is equal to the vector amongst the following that has \
the smallest magnitude ("move" is the current object's movement \
vector): \
\
x_offs / move.x * move \
y_offs / move.y * move \
z_offs / move.z * move \
\
Of course it is only necessary to check which of \
[x|y|z]_offs/move.[x|y|z] is smallest. \
*/ \
\
ratio = smallestOfThree \
(x_offs / node->box.move.x, \
y_offs / node->box.move.y, \
z_offs / node->box.move.z); \
\
/* \
* collision axis register used by edge-evasion code \
* ("wall direction") \
*/ \
which = 0; \
if(ratio == x_offs / node->box.move.x) which = 1; \
else if(ratio == y_offs / node->box.move.y) which = 2; \
else if(ratio == z_offs / node->box.move.z) which = 3; \
\
/* in the case of a corner, wall-direction is ambiguous and must be \
inferred from player direction */ \
if(which) \
if(fabs(x_offs / node->box.move.x - z_offs / node->box.move.z) < 2.0) {\
if(fabs(move.x) > fabs(move.z)) { \
ratio = x_offs / node->box.move.x; which = 1; \
} else { \
ratio = z_offs / node->box.move.z; which = 3; \
} \
} \
\
/* Cheap way to avoid glitches. Normally, \
ratio should be between 0 and 1. */ \
if(ratio < -2.0) ratio = 0.0; \
\
/* Cheap fix for another glitch. Avoid \
infinite collision-solving loops (player \
gets stuck) */ \
ratio *= 1.00001; \
\
react.x = node->box.move.x * ratio; \
react.y = node->box.move.y * ratio; \
react.z = node->box.move.z * ratio; \
\
goto jump; \
} \
#define resolveCollisions_checkVertex_stair(XTYPE, YTYPE, ZTYPE, jump) \
if(node->box.XTYPE.x > node2->box.min.x && \
node->box.YTYPE.y > node2->box.min.y && \
node->box.ZTYPE.z > node2->box.min.z && \
node->box.XTYPE.x < node2->box.max.x && \
node->box.YTYPE.y < node2->box.max.y && \
node->box.ZTYPE.z < node2->box.max.z) \
{ \
y_offs = node2->box.max.y - node->box.YTYPE.y > \
node2->box.min.y - node->box.YTYPE.y ? \
node2->box.max.y - node->box.YTYPE.y : \
node2->box.min.y - node->box.YTYPE.y; \
\
react.y = y_offs; \
\
goto jump; \
} \
void resolveCollisions(game_obj* objs, void (*handler)(void*, void*))
{
/* collision resolver computation variables */
vector move, react;
float x_offs, y_offs, z_offs;
float ratio;
int ee;
/*
* Objects marked as "stairs" have normal
* Y-axis (vertical) collisions,
* but move objects strictly on the Y axis
* when X, Z collisions occur. The desired
* effect is that the player can "walk up
* stairs".
*
* Currently, there is no system for flagging
* specific objects as stairs; setting stair = 1
* below will make the collision code treat
* everything as a stair.
*/
int stair = 0;
game_obj *node, *node2;
for(node = objs; node != NULL; node = node->next) {
if(node->type == SOLID) continue; /* heuristic: SOLIDs don't move ! */
ee = 0;
for(node2 = objs; node2 != NULL; node2 = node2->next) {
if(node != node2 && node->type != NONE && node2->type != NONE) {
/* Check if any of the current boxe's
vertices are within any other box.
The collision engine stores only
two vertices: minimum and maximum.
The other six vertice's coordinates are
combinations of these two vertice's
x,y,z coordinates. */
/* Initialize reaction vector */
react.x = react.y = react.z = 0.0;
move.x = node->box.move.x;
move.y = node->box.move.y;
move.z = node->box.move.z;
/*
* We first resolve collisions
* resulting from movement
* on the Y axis, setting move.x = move.z = 0
* to ensure the reaction vector is strictly Y-axis
*/
node->box.min.x -= move.x; /* cancel out x, z movement */
node->box.max.x -= move.x;
node->box.min.z -= move.z;
node->box.max.z -= move.z;
node->box.move.x = 0.0;
node->box.move.z = 0.0;
resolveCollisions_checkVertex(min, min, min, collision_found);
resolveCollisions_checkVertex(min, min, max, collision_found);
resolveCollisions_checkVertex(min, max, min, collision_found);
resolveCollisions_checkVertex(min, max, max, collision_found);
resolveCollisions_checkVertex(max, min, min, collision_found);
resolveCollisions_checkVertex(max, min, max, collision_found);
resolveCollisions_checkVertex(max, max, min, collision_found);
resolveCollisions_checkVertex(max, max, max, collision_found);
collision_found:
node->box.min.y += react.y;
node->box.max.y += react.y;
if(react.x || react.y || react.z) {
(*handler)(node, node2);
(*handler)(node2, node);
}
/*
* Now we solve collisions resulting from
* movement on the X and Z axes. Notice that
* we keep the collision-resolved Y coordinate
* we just obtained and set move.y = 0, so that the
* reaction vector is strictly in the X and Z axes.
*/
react.x = react.y = react.z = 0.0;
/* restore x,z displacement we cancelled out
earlier on */
node->box.min.x += move.x;
node->box.max.x += move.x;
node->box.min.z += move.z;
node->box.max.z += move.z;
node->box.move.x = move.x;
node->box.move.z = move.z;
node->box.move.y = 0.0;
/* clear the edge-evasion register */
which = 0;
if(stair){
/* stairs mode */
resolveCollisions_checkVertex_stair(min, min, min, collision_found2);
resolveCollisions_checkVertex_stair(min, min, max, collision_found2);
resolveCollisions_checkVertex_stair(min, max, min, collision_found2);
resolveCollisions_checkVertex_stair(min, max, max, collision_found2);
resolveCollisions_checkVertex_stair(max, min, min, collision_found2);
resolveCollisions_checkVertex_stair(max, min, max, collision_found2);
resolveCollisions_checkVertex_stair(max, max, min, collision_found2);
resolveCollisions_checkVertex_stair(max, max, max, collision_found2);
} else {
resolveCollisions_checkVertex(min, min, min, collision_found2);
resolveCollisions_checkVertex(min, min, max, collision_found2);
resolveCollisions_checkVertex(min, max, min, collision_found2);
resolveCollisions_checkVertex(min, max, max, collision_found2);
resolveCollisions_checkVertex(max, min, min, collision_found2);
resolveCollisions_checkVertex(max, min, max, collision_found2);
resolveCollisions_checkVertex(max, max, min, collision_found2);
resolveCollisions_checkVertex(max, max, max, collision_found2);
}
collision_found2:
if(ratio == 0.0 && (x_offs || y_offs || z_offs)) {
/* very rare glitch. setup the edge-evasion
bits as if there was a conflict. */
node->ee_bits = 6;
}
/* Simple edge-evasion, based on PypeBros' suggestion.
When a collision occurs, a request is made to add the
the projection of the player's move vector on the "wall direction"
(obtained here from earlier collision calculations). The
edge-evasion module will cancel all edge-evasion if a conflict
(edge-evasion in both wall-directions requested) is detected.
*/
if(which == 3) {
if(react.x == 0) {
/* rare glitch. setup the EE bits
as if there was a conflict to block
edge-evasion */
node->ee_bits = 6;
}
edge_evade(node, node2, 0);
ee = 1;
}
if(which == 1) {
if(react.z == 0) {
/* rare glitch. setup the EE bits
as if there was a conflict */
node->ee_bits = 6;
}
edge_evade(node, node2, 1);
ee = 1;
}
node->box.min.x += react.x;
node->box.max.x += react.x;
node->box.min.z += react.z;
node->box.max.z += react.z;
if(node->type==PLAYER && stair){
/* player hit a stair-mode object while
moving on X and Z axes; walk the player
up the stair */
node->box.move.y = 1.0f;
node->box.min.y += react.y;
node->box.max.y += react.y;
}
if(react.x || react.y || react.z) {
(*handler)(node, node2);
(*handler)(node2, node);
}
/*
* Restore the move vector as
* it was prior to the Y axis / X, Z axes
* separation for further use
*/
node->box.move.x = move.x;
node->box.move.y = move.y;
node->box.move.z = move.z;
}
}
/* If no edge-evasion requests occured in this collision
code frame, clear the edge-evasion data. (The edge-evasion
module is a state machine) */
if(!ee) edge_evade(node, NULL, 0);
}
}