-
Notifications
You must be signed in to change notification settings - Fork 0
/
physica.cpp
1622 lines (1378 loc) · 50.9 KB
/
physica.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
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
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
//
// Created by doug on 4/19/15.
//
#include <assert.h>
#include "physica.h"
#include "float.h"
#include "game.h"
#include "renderer.h"
phy_state_
phy_init(memory_arena_* memory) {
phy_state_ result;
result.time_step = 1.0f / 240.0f;
result.bodies.init(memory, 4000);
result.previous_velocities.init(memory, 4000);
result.previous_angular_velocities.init(memory, 4000);
result.hulls.init(memory, 4000);
result.points.init(memory, 4000);
result.collisions.init(memory, 4000);
result.potential_collisions.init(memory, 4000);
result.manifold_cache.pairs.init(memory, 4000);
result.aabb_tree.nodes.init(memory, 8000);
result.aabb_tree.checked_parents.init(memory, 8000);
result.aabb_tree.dead_nodes.init(memory, 8000);
return result;
}
phy_aabb_
get_union(phy_aabb_ a, phy_aabb_ b) {
phy_aabb_ result;
result.min.x = fmin(a.min.x, b.min.x);
result.min.y = fmin(a.min.y, b.min.y);
result.max.x = fmax(a.max.x, b.max.x);
result.max.y = fmax(a.max.y, b.max.y);
return result;
}
ray_intersect_
ray_hull_intersect(v2 p, v2 d, phy_hull_* hull) {
ray_intersect_ result;
result.intersecting = false;
v2 hp = hull->position;
m2x2 rotation = get_rotation_matrix(hull->orientation);
switch (hull->type) {
case HULL_MESH: {
array<v2> ps = hull->points;
for (int i = 0; i < ps.count; ++i) {
v2 a = hp + rotation * ps[(i == 0 ? ps.count : i) - 1];
v2 b = hp + rotation * ps[i];
ray_intersect_ r = ray_segment_intersect(p,d,a,b);
if (!r.intersecting || (result.intersecting && result.depth < r.depth)) {
continue;
}
result = r;
}
} break;
case HULL_RECT: {
f32 width = hull->width;
f32 height = hull->height;
v2 ps[4] = {
v2 {width/2.0f, height/2.0f},
v2 {width/2.0f, -height/2.0f},
v2{ -width / 2.0f, -height / 2.0f },
v2{ -width / 2.0f, height / 2.0f },
};
for (int i = 0; i < 4; ++i) {
v2 a = hp + rotation * ps[(i == 0 ? 4 : i) - 1];
v2 b = hp + rotation * ps[i];
ray_intersect_ r = ray_segment_intersect(p,d,a,b);
if (!r.intersecting || (result.intersecting && result.depth < r.depth)) {
continue;
}
result = r;
}
} break;
case HULL_FILLET_RECT: {
f32 width = hull->width;
f32 height = hull->height;
v2 ps[4] = {
v2{ width / 2.0f, height / 2.0f },
v2{ width / 2.0f, -height / 2.0f },
v2{ -width / 2.0f, -height / 2.0f },
v2{ -width / 2.0f, height / 2.0f },
};
for (int i = 0; i < 4; ++i) {
v2 a = hp + rotation * ps[(i == 0 ? 4 : i) - 1];
v2 b = hp + rotation * ps[i];
ray_intersect_ r = ray_segment_intersect(p, d, a, b);
if (!r.intersecting || (result.intersecting && result.depth < r.depth)) {
continue;
}
// TODO(doug) right now raycasting is only used for jump logic - make this
// actually take the fillet of the rect into account later if we need it
result = r;
}
} break;
}
return result;
}
inline v2 do_support_mesh(phy_hull_* hull, v2 direction) {
f32 greatest = -FLT_MAX;
f32 last = -FLT_MAX;
m2x2 rotation = get_rotation_matrix(hull->orientation);
v2 result = {};
for (int i = 0; i < hull->points.count; ++i) {
v2 transformed = rotation * hull->points[i] + hull->position;
f32 test = dot(transformed, direction);
if (test > greatest) {
greatest = test;
result = transformed;
}
last = test;
}
return result;
}
inline v2 do_support_rect(phy_hull_* hull, v2 direction) {
v2 local_direction = rotate(direction, -hull->orientation);
v2 local_result;
if (local_direction.x > 0.0f) {
local_result.x = hull->width * 0.5f;
} else {
local_result.x = -hull->width * 0.5f;
}
if (local_direction.y > 0.0f) {
local_result.y = hull->height * 0.5f;
} else {
local_result.y = -hull->height * 0.5f;
}
return hull->position + rotate(local_result, hull->orientation);
}
inline v2 do_support_rect_fillet(phy_hull_* hull, v2 direction) {
v2 local_direction = rotate(direction, -hull->orientation);
v2 local_result;
if (local_direction.x > 0.0f) {
local_result.x = (hull->width * 0.5f) - hull->fillet;
} else {
local_result.x = (-hull->width * 0.5f) + hull->fillet;
}
if (local_direction.y > 0.0f) {
local_result.y = (hull->height * 0.5f) - hull->fillet;
} else {
local_result.y = (-hull->height * 0.5f) + hull->fillet;
}
v2 fillet = normalize(local_direction) * (hull->fillet);
return hull->position + rotate(local_result + fillet, hull->orientation);
}
v2
do_support(phy_hull_* hull, v2 direction) {
switch (hull->type) {
case HULL_MESH: {
return do_support_mesh(hull, direction);
} break;
case HULL_RECT: {
return do_support_rect(hull, direction);
} break;
case HULL_FILLET_RECT: {
return do_support_rect_fillet(hull, direction);
} break;
default: assert_(false);
}
return {0};
}
phy_support_result_
do_support(phy_hull_* a, phy_hull_* b, v2 direction) {
phy_support_result_ result;
result.p_a = do_support(a, direction);
result.p_b = do_support(b, -direction);
result.p = result.p_a - result.p_b;
return result;
}
inline f32
area(phy_aabb_ aabb) {
v2 vec = aabb.max - aabb.min;
return vec.x * vec.y;
}
inline b32
aabb_are_intersecting(phy_aabb_ a, phy_aabb_ b) {
if (a.max.x < b.min.x || b.max.x < a.min.x) return false;
if (a.max.y < b.min.y || b.max.y < a.min.y) return false;
return true;
}
inline b32
aabb_is_contained_in(phy_aabb_ inner, phy_aabb_ outer) {
return inner.max.x <= outer.max.x && inner.max.y <= outer.max.y &&
inner.min.x >= outer.min.x && inner.min.y >= outer.min.y;
}
inline b32
aabb_is_contained_in(v2 p, phy_aabb_ aabb) {
return p.x >= aabb.min.x && p.y >= aabb.min.y &&
p.x <= aabb.max.x && p.y <= aabb.max.y;
}
phy_aabb_tree_node_*
aabb_insert_node(phy_aabb_tree_* tree,
i32 parent_index,
phy_aabb_ fat_aabb,
phy_body_* body,
b32 is_asleep) {
phy_aabb_tree_node_* result;
b32 found_leaf = false;
while (!found_leaf) {
phy_aabb_tree_node_ *parent = tree->nodes.at(parent_index);
if (parent->type == LEAF_NODE) {
// if parent is a leaf, split it - transfer the parent's data into
// the left child and set up our new node as the right child
i32 left_index;
if (tree->dead_nodes.count) {
left_index = tree->dead_nodes[--tree->dead_nodes.count];
}
else {
left_index = tree->nodes.push_unassigned();
}
i32 right_index;
if (tree->dead_nodes.count) {
right_index = tree->dead_nodes[--tree->dead_nodes.count];
}
else {
right_index = tree->nodes.push_unassigned();
}
phy_aabb_tree_node_ *left = tree->nodes.at(left_index);
phy_aabb_tree_node_ *right = tree->nodes.at(right_index);
phy_body_* parent_body = parent->body;
parent->left = left_index;
parent->right = right_index;
left->parent = parent_index;
left->fat_aabb = parent->fat_aabb;
left->is_asleep = parent->is_asleep;
left->body = parent_body;
left->type = LEAF_NODE;
right->parent = parent_index;
right->fat_aabb = fat_aabb;
right->body = body;
right->type = LEAF_NODE;
right->is_asleep = is_asleep;
found_leaf = true;
} else {
phy_aabb_tree_node_ *left = tree->nodes.at(parent->left);
phy_aabb_tree_node_ *right = tree->nodes.at(parent->right);
phy_aabb_ left_union = get_union(left->fat_aabb, fat_aabb);
phy_aabb_ right_union = get_union(right->fat_aabb, fat_aabb);
parent_index = area(left_union) < area(right_union)
? parent->left
: parent->right;
}
}
result = tree->nodes.at(parent_index);
while (parent_index != -1) {
phy_aabb_tree_node_ *parent = tree->nodes.at(parent_index);
phy_aabb_tree_node_ *left = tree->nodes.at(parent->left);
phy_aabb_tree_node_ *right = tree->nodes.at(parent->right);
parent->fat_aabb = get_union(left->fat_aabb, right->fat_aabb);
parent->is_asleep = left->is_asleep && right->is_asleep;
parent_index = parent->parent;
}
return result;
}
void aabb_remove_node(phy_aabb_tree_ *tree, i32 index) {
TIMED_FUNC();
i32 parent_index = tree->nodes[index].parent;
if (parent_index == -1) {
// if parent index is -1, we're removing the root. just kill the tree
assert_(tree->nodes.count == 1);
tree->nodes.count = 0;
tree->dead_nodes.count = 0;
} else {
phy_aabb_tree_node_ *parent = tree->nodes.at(parent_index);
i32 sibling_index = (parent->left == index) ? parent->right
: parent->left;
phy_aabb_tree_node_ *sibling = tree->nodes.at(sibling_index);
if (parent->parent != -1) {
// if we have a grand parent, then replace the parent node with
// the sibling of the node we're removing
phy_aabb_tree_node_ *grandparent = tree->nodes.at(parent->parent);
sibling->parent = parent->parent;
i32 parent_was_right = grandparent->right == parent_index;
if (parent_was_right) {
grandparent->right = sibling_index;
} else {
grandparent->left = sibling_index;
}
} else {
tree->root = sibling_index;
tree->nodes.at(tree->root)->parent = -1;
}
tree->dead_nodes.push(parent_index);
tree->dead_nodes.push(index);
}
while (parent_index != -1) {
phy_aabb_tree_node_ *parent = tree->nodes.at(parent_index);
phy_aabb_tree_node_ *left = tree->nodes.at(parent->left);
phy_aabb_tree_node_ *right = tree->nodes.at(parent->right);
parent->fat_aabb = get_union(left->fat_aabb, right->fat_aabb);
parent->is_asleep = left->is_asleep && right->is_asleep;
parent_index = parent->parent;
}
}
phy_aabb_
get_aabb(phy_body_ *body) {
TIMED_FUNC();
phy_aabb_ result;
result.min = v2 {FLT_MAX, FLT_MAX};
result.max = v2 {-FLT_MAX, -FLT_MAX};
m2x2 rotation = get_rotation_matrix(body->orientation);
for (int i = 0; i < body->hulls.count; ++i) {
phy_hull_ *hull = body->hulls.at(i);
switch (hull->type) {
case HULL_MESH: {
for (int j = 0; j < hull->points.count; ++j) {
v2 p = hull->position + rotation * hull->points[j];
if (p.x < result.min.x) { result.min.x = p.x; }
if (p.y < result.min.y) { result.min.y = p.y; }
if (p.x > result.max.x) { result.max.x = p.x; }
if (p.y > result.max.y) { result.max.y = p.y; }
}
} break;
default: {
result.min.x = do_support(hull, v2 {-1.0f, 0.0f}).x;
result.max.x = do_support(hull, v2 {1.0f, 0.0f}).x;
result.min.y = do_support(hull, v2 {0.0f, -1.0f}).y;
result.max.y = do_support(hull, v2 {0.0f, 1.0f}).y;
} break;
}
}
return result;
}
ray_intersect_
ray_segment_intersect(v2 p, v2 d, v2 a, v2 b) {
v2 v_1 = p - a;
v2 v_2 = b - a;
v2 v_3 = perp(d);
f32 t_1 = flt_cross(v_2, v_1) / dot(v_2, v_3);
f32 t_2 = dot(v_1, v_3) / dot(v_2, v_3);
ray_intersect_ result;
result.intersecting = t_2 >= 0.0f && t_2 <= 1.0f && t_1 >= 0.0f;
result.depth = t_1;
return result;
}
inline ray_intersect_
ray_aabb_intersect(v2 p, v2 d, phy_aabb_ aabb) {
ray_intersect_ result;
result.intersecting = false;
if (aabb_is_contained_in(p, aabb)) {
result.intersecting = true;
result.depth = 0;
} else {
if (p.x <= aabb.min.x) {
f32 t = (aabb.min.x - p.x) / d.x;
f32 y = p.y + t * d.y;
if (t > 0.0f && y >= aabb.min.y && y <= aabb.max.y) {
result.intersecting = true;
result.depth = t;
}
} else if(!result.intersecting && p.x >= aabb.max.x) {
f32 t = (aabb.max.x - p.x) / d.x;
f32 y = p.y + t * d.y;
if (t > 0.0f && y >= aabb.min.y && y <= aabb.max.y) {
result.intersecting = true;
result.depth = t;
}
}
if(!result.intersecting && p.y <= aabb.min.y) {
f32 t = (aabb.min.y - p.y) / d.y;
f32 x = p.x + t * d.x;
if (t > 0.0f && x >= aabb.min.x && x <= aabb.max.x) {
result.intersecting = true;
result.depth = t;
}
} else if (!result.intersecting && p.y >= aabb.max.y) {
f32 t = (aabb.max.y - p.y) / d.y;
f32 x = p.x + t * d.x;
if (t > 0.0f && x >= aabb.min.x && x <= aabb.max.x) {
result.intersecting = true;
result.depth = t;
}
}
}
return result;
}
inline ray_intersect_
ray_body_intersect(v2 p, v2 d, phy_body_* body) {
ray_intersect_ result;
result.intersecting = false;
for (int i = 0; i < body->hulls.count; ++i) {
ray_intersect_ r = ray_hull_intersect(p,d,body->hulls.at(i));
if (!r.intersecting || (result.intersecting && result.depth < r.depth)) { continue; }
result = r;
}
return result;
}
b32 hull_contains_point(phy_hull_* hull, v2 p) {
switch (hull->type) {
case HULL_MESH: {
assert_(false);
} break;
case HULL_RECT: {
v2 p_local = rotate(p - hull->position, -hull->orientation);
f32 width = hull->width;
f32 height = hull->height;
if (p_local.x >= -width / 2.0f && p_local.x <= width / 2.0f &&
p_local.y >= -height / 2.0f && p_local.y <= height / 2.0f) {
return true;
}
} break;
case HULL_FILLET_RECT: {
// TODO(doug): ignoring the fillet for now. will come back to it later
v2 p_local = rotate(p - hull->position, -hull->orientation);
f32 width = hull->width;
f32 height = hull->height;
if (p_local.x >= -width / 2.0f && p_local.x <= width / 2.0f &&
p_local.y >= -height / 2.0f && p_local.y <= height / 2.0f) {
return true;
}
} break;
}
return false;
}
b32 body_contains_point(phy_body_* body, v2 p) {
for (int i = 0; i < body->hulls.count; ++i) {
if (hull_contains_point(body->hulls.at(i), p)) {
return true;
}
}
return false;
}
phy_body_* pick_body(phy_state_* state, v2 p) {
phy_body_* result = 0;
phy_aabb_tree_* tree = &state->aabb_tree;
i32 stack[MEDIUM_STACK_SIZE] = {0};
i32 stack_index = 0;
stack[stack_index++] = tree->root;
while (stack_index > 0) {
assert((size_t)stack_index < ARRAY_SIZE(stack));
phy_aabb_tree_node_* node = tree->nodes.at(stack[--stack_index]);
phy_aabb_ aabb = node->fat_aabb;
if (p.x >= aabb.min.x && p.y >= aabb.min.y &&
p.x <= aabb.max.x && p.y <= aabb.max.y) {
if (node->type == LEAF_NODE) {
phy_body_* body = node->body;
if (body_contains_point(body, p)) {
result = body;
break;
}
} else {
stack[stack_index++] = node->left;
stack[stack_index++] = node->right;
}
}
}
return result;
}
ray_body_intersect_ ray_cast(phy_state_* state,
v2 p,
v2 d,
u32 required_flags,
phy_body_* exclude) {
phy_aabb_tree_* tree = &state->aabb_tree;
ray_body_intersect_ result = {0};
i32 stack[MEDIUM_STACK_SIZE] = {0};
i32 stack_index = 0;
stack[stack_index++] = tree->root;
while (stack_index > 0) {
assert((size_t)stack_index < ARRAY_SIZE(stack));
phy_aabb_tree_node_* node = tree->nodes.at(stack[--stack_index]);
ray_intersect_ r = ray_aabb_intersect(p, d, node->fat_aabb);
if (!r.intersecting || (result.body && result.depth < r.depth)) {
continue;
}
if (node->type == LEAF_NODE) {
phy_body_* body = node->body;
if (!body || body == exclude) { continue; }
if ((body->flags & required_flags) != required_flags) { continue; }
r = ray_body_intersect(p, d, body);
if (!r.intersecting || (result.body && result.depth < r.depth)) {
continue;
}
result.body = body;
result.depth = r.depth;
} else {
stack[stack_index++] = node->left;
stack[stack_index++] = node->right;
}
}
return result;
}
ray_body_intersect_
ray_cast_from_body(phy_state_* state,
phy_body_* self,
f32 width,
v2 d,
u32 required_flags) {
phy_aabb_tree_* tree = &state->aabb_tree;
ray_body_intersect_ result = {0};
i32 stack[MEDIUM_STACK_SIZE] = {0};
for (int i = 0; i < 2; ++i) {
v2 pd = perp(d);
v2 p = i ?
self->position + (0.5f * width * pd) :
self->position + (-0.5f * width * pd);
i32 stack_index = 0;
stack[stack_index++] = tree->root;
while (stack_index > 0) {
assert((size_t)stack_index < ARRAY_SIZE(stack));
phy_aabb_tree_node_* node = tree->nodes.at(stack[--stack_index]);
ray_intersect_ r = ray_aabb_intersect(p, d, node->fat_aabb);
if (!r.intersecting || (result.body && result.depth < r.depth)) {
continue;
}
if (node->type == LEAF_NODE) {
phy_body_* body = node->body;
if (!body || body == self) { continue; }
if ((body->flags & required_flags) != required_flags) { continue; }
r = ray_body_intersect(p, d, body);
if (!r.intersecting || (result.body && result.depth < r.depth)) {
continue;
}
result.body = body;
result.depth = r.depth;
} else {
stack[stack_index++] = node->left;
stack[stack_index++] = node->right;
}
}
}
return result;
}
void
find_broad_phase_collisions(phy_state_* state) {
TIMED_FUNC();
phy_aabb_tree_* tree = &state->aabb_tree;
state->potential_collisions.count = 0;
if (tree->nodes.count == 0) {
return;
}
phy_aabb_tree_node_* root = tree->nodes.at(tree->root);
if (root->type == LEAF_NODE) {
return;
}
for (int i = 0; i < tree->checked_parents.count; ++i) {
tree->checked_parents.values[i] = false;
}
i32 stack[MEDIUM_STACK_SIZE] = {0};
i32 stack_index = -1;
stack[++stack_index] = root->left;
stack[++stack_index] = root->right;
while (stack_index >= 0) {
assert_(stack_index % 2 == 1);
i32 a_index = stack[stack_index--];
i32 b_index = stack[stack_index--];
phy_aabb_tree_node_ *a = tree->nodes.at(a_index);
phy_aabb_tree_node_ *b = tree->nodes.at(b_index);
if (a->is_asleep && b->is_asleep) {
continue;
}
if (a->type == LEAF_NODE) {
if (b->type == LEAF_NODE) {
phy_body_* a_body = a->body;
phy_body_* b_body = b->body;
if (!is_freed(a_body) && !is_freed(b_body) &&
(!(a_body->flags & PHY_FIXED_FLAG) ||
!(b_body->flags & PHY_FIXED_FLAG))) {
phy_aabb_ aabb_a = a_body->aabb;
phy_aabb_ aabb_b = b_body->aabb;
if (aabb_are_intersecting(aabb_a, aabb_b)) {
phy_potential_collision_ collision;
if (a->body < b->body) {
collision.a = a->body;
collision.b = b->body;
} else {
collision.a = b->body;
collision.b = a->body;
}
state->potential_collisions.push(collision);
}
}
} else {
if (!tree->checked_parents[b_index]) {
tree->checked_parents.values[b_index] = true;
stack[++stack_index] = b->left;
stack[++stack_index] = b->right;
}
if (aabb_are_intersecting(a->fat_aabb, b->fat_aabb)) {
stack[++stack_index] = a_index;
stack[++stack_index] = b->left;
stack[++stack_index] = a_index;
stack[++stack_index] = b->right;
}
}
} else {
if (b->type == LEAF_NODE) {
if (!tree->checked_parents[a_index]) {
tree->checked_parents.values[a_index] = true;
stack[++stack_index] = a->left;
stack[++stack_index] = a->right;
}
if (aabb_are_intersecting(a->fat_aabb, b->fat_aabb)) {
stack[++stack_index] = b_index;
stack[++stack_index] = a->left;
stack[++stack_index] = b_index;
stack[++stack_index] = a->right;
}
} else {
if (!tree->checked_parents[a_index]) {
tree->checked_parents.values[a_index] = true;
stack[++stack_index] = a->left;
stack[++stack_index] = a->right;
}
if (!tree->checked_parents[b_index]) {
tree->checked_parents.values[b_index] = true;
stack[++stack_index] = b->left;
stack[++stack_index] = b->right;
}
if (aabb_are_intersecting(a->fat_aabb, b->fat_aabb)) {
stack[++stack_index] = b->left;
stack[++stack_index] = a->left;
stack[++stack_index] = b->right;
stack[++stack_index] = a->right;
stack[++stack_index] = b->right;
stack[++stack_index] = a->left;
stack[++stack_index] = b->left;
stack[++stack_index] = a->right;
}
}
}
}
}
phy_body_*
phy_add_block(phy_state_* state,
v2 center,
v2 diagonal,
f32 mass,
f32 orientation) {
phy_body_* body = phy_add_body(state);
f32 width = diagonal.x;
f32 height = diagonal.y;
body->mass = mass;
body->gravity_normal = v2{0};
body->inv_mass = 1.0f / body->mass;
body->moment = 1.0f/12.0f * body->mass * (width * width + height * height);
body->inv_moment = 1.0f / body->moment;
body->hulls = phy_add_hulls(state, 1);
body->aabb_node_index = -1;
body->position = center;
body->orientation = orientation;
phy_hull_ * hull = body->hulls.values;
hull->mass = mass;
hull->inv_mass = 1.0f / hull->mass;
hull->moment = 1.0f;
hull->inv_moment = 1.0f/12.0f *
hull->mass *
(width * width + height * height);
hull->type = HULL_RECT;
hull->width = width;
hull->height = height;
return body;
}
phy_body_*
phy_add_fillet_block(phy_state_* state,
v2 center,
v2 diagonal,
f32 fillet,
f32 mass,
f32 orientation) {
phy_body_* body = phy_add_body(state);
f32 width = diagonal.x;
f32 height = diagonal.y;
body->mass = mass;
body->gravity_normal = v2{0};
body->inv_mass = 1.0f / body->mass;
body->moment = 1.0f/12.0f * body->mass * (width * width + height * height);
body->inv_moment = 1.0f / body->moment;
body->hulls = phy_add_hulls(state, 1);
body->aabb_node_index = -1;
body->position = center;
body->orientation = orientation;
phy_hull_ * hull = body->hulls.values;
hull->mass = mass;
hull->inv_mass = 1.0f / hull->mass;
hull->moment = 1.0f;
hull->inv_moment = 1.0f/12.0f *
hull->mass *
(width * width + height * height);
hull->type = HULL_FILLET_RECT;
hull->width = width;
hull->height = height;
hull->fillet = fillet;
return body;
}
phy_body_*
phy_add_body(phy_state_* state) {
return state->bodies.acquire();
}
void
phy_remove_body(phy_state_* state, phy_body_* body) {
aabb_remove_node(&state->aabb_tree, body->aabb_node_index);
state->hulls.free_many(body->hulls.values, body->hulls.count);
state->bodies.free(body);
}
array<phy_hull_>
phy_add_hulls(phy_state_* state, i32 count) {
return state->hulls.acquire_many(count);
}
array<v2>
phy_add_points(phy_state_* state, i32 count) {
return state->points.acquire_many(count);
}
phy_collision_*
phy_add_collision(phy_state_* state) {
assert_(state->collisions.count < state->collisions.capacity);
return state->collisions.values + state->collisions.count++;
}
phy_collision_*
phy_add_collision(phy_state_* state, phy_collision_ collision) {
assert_(state->collisions.count < state->collisions.capacity);
phy_collision_* result =
state->collisions.values + state->collisions.count++;
*result = collision;
return result;
}
void
update_hulls(phy_body_* body) {
TIMED_FUNC();
m2x2 rotation = get_rotation_matrix(body->orientation);
for (int i = 0; i < body->hulls.count; ++i) {
phy_hull_* hull = body->hulls.at(i);
hull->position = body->position + rotation * hull->relative_position;
hull->orientation = body->orientation;
}
}
v2
do_simplex(phy_support_result_ support,
phy_support_result_* simplex,
i32* simplex_count) {
v2 result;
switch (*simplex_count) {
case 1: {
v2 ab = support.p - simplex[0].p;
simplex[1] = support;
*simplex_count += 1;
result = triple(ab, ab, simplex[0].p);
} break;
case 2: {
// (support) c
// / \
// o?/ \ o?
// / o? \
// (simplex[0]) a-------b (simplex[1])
v2 a = simplex[0].p;
v2 b = simplex[1].p;
v2 c = support.p;
v2 ac = c - a;
// perpendicular away from b
v2 ac_p = triple(ac, ac, b);
f32 dot_acp = dot(ac_p, c);
if (dot_acp == 0.0f) {
// origin is on ac - return a collision and let EPA handle
// whether or not we care
*simplex_count += 1;
simplex[2] = support;
result = v2 {0};
} else if (dot_acp > 0.0f) {
// origin may be inside simplex - keep checking
v2 bc = c - b;
// perpendicular away from a
v2 bc_p = triple(bc, bc, a);
f32 dot_bcp = dot(bc_p, c);
if (dot_bcp >= 0.0f) {
// origin is on bc or inside simplex
*simplex_count += 1;
simplex[2] = support;
result = v2 {0};
} else {
// origin is on outside of bc - replace a with c and check
// perpendicular to bc away from c
simplex[0] = support;
result = bc_p;
}
} else {
// origin is on outside of ac - replace b with c and check
// perpendicular to ac away from b
simplex[1] = support;
result = ac_p;
}
} break;
default: assert_(false);
}
return result;
}
// must guarantee to populate simplex with 3 points if it returns true
b32
do_gjk(phy_hull_* a, phy_hull_* b, phy_support_result_* simplex) {
TIMED_BLOCK(do_gjk);
v2 d = v2 {1,1};
phy_support_result_ support = do_support(a, b, d);
if (dot(support.p, d) <= 0.0f) {
return false;
}
simplex[0] = support;
d = -d;
i32 simplex_count = 1;
for (int i = 0; i < 20; ++i) {
support = do_support(a, b, d);
if (dot(support.p, d) <= 0.0f) {
return false;
}
d = do_simplex(support, simplex, &simplex_count);
if (simplex_count == 3) {
return true;
}
}
assert_(false);
return false;
}
phy_edge_
find_closest_edge_to_origin(phy_support_result_* polytope,
i32 vertex_count) {
phy_edge_ result = {0};
result.depth = FLT_MAX;
result.normal = {0};
for (int i = 0; i < vertex_count; ++i) {
i32 next = (i + 1) % vertex_count;
v2 a = polytope[i].p;
v2 b = polytope[next].p;
assert_(!(fequals(a.x, b.x) && fequals(a.y, b.y)));
v2 e = b - a;
v2 n = triple(e, a, e);
n = normalize(n);
f32 test_distance = dot(n, a);
if (test_distance < result.depth) {
result.depth = test_distance;
result.normal = n;
result.polytope_index = next;
result.a = i;
result.b = next;
}
}
return result;
}
inline void
insert_into_polytope(phy_support_result_* polytope,
i32* vertex_count,
i32 index,
phy_support_result_ vertex) {
for (i32 i = *vertex_count; i > index; --i) {
polytope[i] = polytope[i - 1];
}
polytope[index] = vertex;
*vertex_count += 1;
}
inline void
reorder_simplex(phy_support_result_* simplex) {
if (dot(perp(simplex[1].p - simplex[0].p), simplex[2].p) < 0.0f) {
phy_support_result_ temp = simplex[0];
simplex[0] = simplex[1];
simplex[1] = temp;
}
}
b32
do_epa(phy_hull_* a,
phy_hull_* b,
phy_support_result_* polytope,
phy_edge_* result) {
reorder_simplex(polytope);
i32 vertex_count = 3;
const f32 threshold = 0.001f;
for (int i = 0; i < 20; i++) {
phy_edge_ edge = find_closest_edge_to_origin(polytope,
vertex_count);
phy_support_result_ support = do_support(a, b, edge.normal);
f32 d = dot(support.p, edge.normal);