-
Notifications
You must be signed in to change notification settings - Fork 1
/
collision.jai
362 lines (321 loc) · 12.9 KB
/
collision.jai
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
361
HitRecord :: struct {
success : bool;
tri : Tri;
distance : float;
}
// TODO: The PickTri proc in graphics_math.jai should use this proc as it does the same just with the cam as starting point
Trace :: (start : Vector3, end : Vector3, maxT : float, models : [] Model) -> HitRecord {
r := normalize(end - start);
shortestT := maxT;
bestTri : Tri;
success := false;
for model : models {
for tri : model.tris {
n := tri.a.normal; // NOTE: The normal could be an average between two vertex-normals but for now we only have hard surfaces.
c := normalize(start - tri.a.pos);
nDotC := dot(n, c);
if nDotC >= 0 { // Is start pos on the positive (front side) of the plane?
nDotR := dot(n, r);
if nDotR < 0.0 { // Is the ray looking to the front face of the plane? Attention: Must be *really* smaller than 0 becasuse we devide by nDotR later!
// if nDotR >= -0.00001 && nDotR <= 0.00001 continue;
// compute the hitpoint p between ray and plane
d := -dot(n, tri.a.pos);
t := (-dot(n, start) - d) / nDotR;
p := start + t*r;
// print("t: %\n", t);
// print("r: %\n", r);
// print("start + t*r: %\n", p);
v0 := normalize(tri.b.pos - tri.a.pos);
v1 := normalize(tri.c.pos - tri.b.pos);
v2 := normalize(tri.a.pos - tri.c.pos);
ap := normalize(p - tri.a.pos);
bp := normalize(p - tri.b.pos);
cp := normalize(p - tri.c.pos);
n0 := cross(v0, n);
n1 := cross(v1, n);
n2 := cross(v2, n);
if dot(n0, ap) > 0.0 continue;
if dot(n1, bp) > 0.0 continue;
if dot(n2, cp) > 0.0 continue;
if t < shortestT {
bestTri = tri;
shortestT = t;
success = true;
}
}
}
}
}
hitPoint := start + (shortestT*r);
return .{success, bestTri, shortestT};
}
AABB :: struct {
minXYZ := Vector3.{-1.0, -1.0, -1.0};
maxXYZ := Vector3.{1.0, 1.0, 1.0};
width : float = 2;
height : float = 2;
depth : float = 2;
// All 8 points of the box, not sure if this is a waste of memory...
// but makes collision detection easier. TODO: How does Quake do it?
bottomA := Vector3.{-1.0, -1.0, -1.0}; // minXYZ, TODO: Alias in structs?
bottomB := Vector3.{-1.0, -1.0, 1.0};
bottomC := Vector3.{ 1.0, -1.0, 1.0};
bottomD := Vector3. {1.0, -1.0, -1.0};
topA := Vector3.{-1.0, 1.0, -1.0};
topB := Vector3.{-1.0, 1.0, 1.0};
topC := Vector3.{ 1.0, 1.0, 1.0}; // maxXYZ, TODO: Alias in structs?
topD := Vector3.{ 1.0, 1.0, -1.0};
}
ComputeAABB :: (tris : [] Tri) -> AABB {
minX, minY, minZ : float = 999.99;
maxX, maxY, maxZ : float = -999.99;
for tri : tris {
pos := tri.a.pos;
if pos.x < minX minX = pos.x;
else if pos.x > maxX maxX = pos.x;
if pos.y < minY minY = pos.y;
else if pos.y > maxY maxY = pos.y;
if pos.z < minZ minZ = pos.z;
else if pos.z > maxZ maxZ = pos.z;
pos = tri.b.pos;
if pos.x < minX minX = pos.x;
else if pos.x > maxX maxX = pos.x;
if pos.y < minY minY = pos.y;
else if pos.y > maxY maxY = pos.y;
if pos.z < minZ minZ = pos.z;
else if pos.z > maxZ maxZ = pos.z;
pos = tri.c.pos;
if pos.x < minX minX = pos.x;
else if pos.x > maxX maxX = pos.x;
if pos.y < minY minY = pos.y;
else if pos.y > maxY maxY = pos.y;
if pos.z < minZ minZ = pos.z;
else if pos.z > maxZ maxZ = pos.z;
}
width := abs(maxX - minX);
height := abs(maxY - minY);
depth := abs(maxZ - minZ);
X := Vector3.{1.0, 0.0, 0.0};
Y := Vector3.{0.0, 1.0, 0.0};
Z := Vector3.{0.0, 0.0, 1.0};
minXYZ := Vector3.{minX, minY, minZ};
maxXYZ := Vector3.{maxX, maxY, maxZ};
bottomA := minXYZ;
bottomB := minXYZ + Z*depth;
bottomC := bottomB + X*width;
bottomD := bottomA + X*width;
topA := bottomA + Y*height;
topB := bottomB + Y*height;
topC := bottomC + Y*height;
topD := bottomD + Y*height;
return .{
minXYZ,
maxXYZ,
width, height, depth,
bottomA, bottomB, bottomC, bottomD,
topA, topB, topC, topD
};
}
CheckAABB :: (a : AABB, b : AABB) -> bool {
return !((a.maxXYZ.x < b.minXYZ.x) || (b.maxXYZ.x < a.minXYZ.x) ||
(a.maxXYZ.y < b.minXYZ.y) || (b.maxXYZ.y < a.minXYZ.y) ||
(a.maxXYZ.z < b.minXYZ.z) || (b.maxXYZ.z < a.minXYZ.z));
}
// TODO: All this stuff below should be replaced by some solid collision detection stuff.
// TODO: This stuff is probably very expensive!!!
PredictMovement :: (world : *World, entity : *Entity, velocity : Vector3) -> Vector3 {
// Sweep bounding box
sweptAABB_x, sweptAABB_y, sweptAABB_z := SweepAABB(entity.currentAABB, velocity);
// check AABB overlap with world geometry
worldCollisionModels : [..] Model;
worldCollisionModels.allocator = temp;
for world.models {
if CheckAABB(sweptAABB_x, it.aabb) {
array_add(*worldCollisionModels, it);
}
if CheckAABB(sweptAABB_y, it.aabb) {
array_add(*worldCollisionModels, it);
}
if CheckAABB(sweptAABB_z, it.aabb) {
array_add(*worldCollisionModels, it);
}
}
start : Vector3;
start.x = entity.currentAABB.minXYZ.x + entity.aabb.width/2.0;
start.y = entity.currentAABB.minXYZ.y + entity.aabb.height/2.0;
start.z = entity.currentAABB.minXYZ.z + entity.aabb.depth/2.0;
end := start + velocity;
max := length(velocity);
dir := normalize(velocity);
shortestDistance := max;
hitTri : Tri;
doesCollide := false;
for world.models {
minkowskiAABB := MinkowskiAABB(entity.currentAABB, it.aabb);
model := CreateModelFromAABB(minkowskiAABB);
test : [1] Model;
test[0] = model;
hit := Trace(start, end, max, test);
if hit.success { // Collision happened!
// Draw3DImmediateTris(model.tris);
if hit.distance < shortestDistance {
doesCollide = true;
shortestDistance = hit.distance;
hitTri = hit.tri;
}
}
}
movement := shortestDistance*dir;
if doesCollide {
// It could happen that shortestDistance*dir results in a position *inside* the world geometry!
p := start + shortestDistance*dir; // hitpoint
aToP := normalize(p - hitTri.a.pos);
result := dot(aToP, hitTri.a.normal);
if result <= 0 { // <= is important because result could be -0 or 0 which is on the plane. And -0 < 0 evals to false. But we want -0 to be treated as 0!
offset := abs(result) + 0.001;
p = p + 0.0001*(-dir);
sToP := p - start;
movement += offset*(hitTri.a.normal); // move away along the normal so we are outside the level geometry
}
}
if shortestDistance < 0.000001 {
slideFactor := dot(-dir, hitTri.a.normal);
// print("slideFactor: %\n", slideFactor);
slide := hitTri.a.normal + dir; // Vector from inversed dir towards tip of tris normal
// movement += slide;
}
// Visualize movement
rayVis : [1] Line;
rayVis[0] = .{
a = .{pos = entity.currentAABB.minXYZ, color = .{1.0, 1.0, 1.0}},
b = .{pos = entity.currentAABB.minXYZ + movement, color = .{1.0, 1.0, 1.0}}
};
Draw3DImmediateLines(rayVis);
// Draw all world models that the player collided with
for worldCollisionModels {
Draw3DAABB(it.aabb);
}
// Draw the swept AABB of the player to see where applied gravity is putting her
playerAABBcolor := Vector3.{0.0, 1.0, 1.0}; // no hit
if worldCollisionModels.count > 0 {
playerAABBcolor = Vector3.{1.0, 0.95, 0.0}; // hit
}
// Draw3DAABB(sweptAABB_x, color = playerAABBcolor);
// Draw3DAABB(sweptAABB_y, color = playerAABBcolor);
// Draw3DAABB(sweptAABB_z, color = playerAABBcolor);
// Only apply movement when space is pressed. TODO: Remove later or map a key to pause or something.
// if keyState[SDLK_SPACE] {
// entity.pos += movement;
// }
// entity.pos += movement;
return movement;
}
CheckGround :: (world : *World, entity : *Entity, frameTime : float) -> bool, Vector3 #must {
// Sweep bounding box
sweptAABB_x, sweptAABB_y, sweptAABB_z := SweepAABB(entity.currentAABB, frameTime*world.gravity);
// check AABB overlap with world geometry
worldCollisionModels : [..] Model;
worldCollisionModels.allocator = temp;
for world.models {
if CheckAABB(sweptAABB_x, it.aabb) {
array_add(*worldCollisionModels, it);
}
if CheckAABB(sweptAABB_y, it.aabb) {
array_add(*worldCollisionModels, it);
}
if CheckAABB(sweptAABB_z, it.aabb) {
array_add(*worldCollisionModels, it);
}
}
start : Vector3;
start.x = entity.currentAABB.minXYZ.x + entity.aabb.width/2.0;
start.y = entity.currentAABB.minXYZ.y + entity.aabb.height/2.0;
start.z = entity.currentAABB.minXYZ.z + entity.aabb.depth/2.0;
end := start + frameTime*world.gravity;
max := length(frameTime*world.gravity);
dir := normalize(world.gravity);
shortestDistance := max;
hitTri : Tri;
doesCollide := false;
for world.models {
minkowskiAABB := MinkowskiAABB(entity.currentAABB, it.aabb);
model := CreateModelFromAABB(minkowskiAABB);
test : [1] Model;
test[0] = model;
hit := Trace(start, end, max, test);
if hit.success { // Collision happened!
// Draw3DImmediateTris(model.tris);
if hit.distance < shortestDistance {
doesCollide = true;
shortestDistance = hit.distance;
hitTri = hit.tri;
return true, .{0.0, 0.0, 0.0};
}
}
}
movement := shortestDistance*dir;
if doesCollide {
// It could happen that shortestDistance*dir results in a position *inside* the world geometry!
p := start + shortestDistance*dir; // hitpoint
aToP := normalize(p - hitTri.a.pos);
result := dot(aToP, hitTri.a.normal);
if result <= 0 { // <= is important because result could be -0 or 0 which is on the plane. And -0 < 0 evals to false. But we want -0 to be treated as 0!
offset := abs(result) + 0.001;
p = p + 0.0001*(-dir);
sToP := p - start;
movement += offset*(hitTri.a.normal); // move away along the normal so we are outside the level geometry
}
}
return false, movement;
}
// grow b by a
MinkowskiAABB :: (a : AABB, b : AABB) -> AABB {
aHalfExtents := Vector3.{
abs(a.maxXYZ.x - a.minXYZ.x)/2.0,
abs(a.maxXYZ.y - a.minXYZ.y)/2.0,
abs(a.maxXYZ.z - a.minXYZ.z)/2.0
};
return .{
minXYZ = b.minXYZ - aHalfExtents,
maxXYZ = b.maxXYZ + aHalfExtents
};
}
SweepAABB :: (aabb : AABB, sweep : Vector3) -> AABB, AABB, AABB {
sweptAABB_x : AABB;
if sweep.x > 0 {
maxXYZ_x := aabb.maxXYZ;
maxXYZ_x.x += sweep.x;
sweptAABB_x.minXYZ = aabb.minXYZ;
sweptAABB_x.maxXYZ = maxXYZ_x;
} else {
minXYZ_x := aabb.minXYZ;
minXYZ_x.x += sweep.x;
sweptAABB_x.minXYZ = minXYZ_x;
sweptAABB_x.maxXYZ = aabb.maxXYZ;
}
sweptAABB_y : AABB;
if sweep.y > 0 {
maxXYZ_y := aabb.maxXYZ;
maxXYZ_y.y += sweep.y;
sweptAABB_y.minXYZ = aabb.minXYZ;
sweptAABB_y.maxXYZ = maxXYZ_y;
} else {
minXYZ_y := aabb.minXYZ;
minXYZ_y.y += sweep.y;
sweptAABB_y.minXYZ = minXYZ_y;
sweptAABB_y.maxXYZ = aabb.maxXYZ;
}
sweptAABB_z : AABB;
if sweep.z > 0 {
maxXYZ_z := aabb.maxXYZ;
maxXYZ_z.z += sweep.z;
sweptAABB_z.minXYZ = aabb.minXYZ;
sweptAABB_z.maxXYZ = maxXYZ_z;
} else {
minXYZ_z := aabb.minXYZ;
minXYZ_z.z += sweep.z;
sweptAABB_z.minXYZ = minXYZ_z;
sweptAABB_z.maxXYZ = aabb.maxXYZ;
}
return sweptAABB_x, sweptAABB_y, sweptAABB_z;
}