forked from cubzh/cubzh
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscene.c
1192 lines (1004 loc) · 44.1 KB
/
scene.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
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
// -------------------------------------------------------------
// Cubzh Core
// scene.c
// Created by Gaetan de Villele on May 14, 2019.
// -------------------------------------------------------------
#include "scene.h"
#include <float.h>
#include <stdlib.h>
#include "weakptr.h"
#if DEBUG_SCENE
static int debug_scene_awake_queries = 0;
#endif
struct _Scene {
Transform *root;
Transform *map; // weak ref to Map transform (Shape retained by parent)
Transform *system; // private hierarchy
Rtree *rtree;
Weakptr *wptr;
Weakptr *game; // weak ref used to resolve resources associated to transform IDs
// transforms potentially removed from scene since last end-of-frame,
// relevant for physics & sync, internal transforms do not need to be accounted for here
FifoList *removed;
// rigidbody couples registered & waiting for a call to end-of-collision callback
DoublyLinkedList *collisions;
// awake volumes can be registered for end-of-frame awake phase
DoublyLinkedList *awakeBoxes;
// constant acceleration for the whole Scene (gravity usually)
float3 constantAcceleration;
};
typedef struct {
Weakptr *t1, *t2;
float3 wNormal;
bool flag;
} _CollisionCouple;
void _scene_collision_couple_free_func(void *ptr) {
_CollisionCouple *cc = (_CollisionCouple *)ptr;
weakptr_release(cc->t1);
weakptr_release(cc->t2);
free(cc);
}
void _scene_update_rtree(Scene *sc, RigidBody *rb, Transform *t, Box *collider) {
// register awake volume here for new and removed colliders, and for transformations change
if (rigidbody_is_enabled(rb) && rigidbody_is_collider_valid(rb) &&
box_is_valid(collider, EPSILON_COLLISION)) {
// insert valid collider as a new leaf
if (rigidbody_get_rtree_leaf(rb) == NULL) {
rigidbody_set_rtree_leaf(rb,
rtree_create_and_insert(sc->rtree,
collider,
rigidbody_get_groups(rb),
rigidbody_get_collides_with(rb),
t));
scene_register_awake_rigidbody_contacts(sc, rb);
}
// update leaf due to collider or transformations change
else if (rigidbody_get_collider_dirty(rb) || transform_is_physics_dirty(t)) {
scene_register_awake_rigidbody_contacts(sc, rb);
rtree_update(sc->rtree, rigidbody_get_rtree_leaf(rb), collider);
scene_register_awake_rigidbody_contacts(sc, rb);
}
}
// remove disabled rigidbody or invalid collider from rtree
else if (rigidbody_get_rtree_leaf(rb) != NULL) {
scene_register_awake_rigidbody_contacts(sc, rb);
rtree_remove(sc->rtree, rigidbody_get_rtree_leaf(rb), true);
rigidbody_set_rtree_leaf(rb, NULL);
}
rigidbody_reset_collider_dirty(rb);
transform_reset_physics_dirty(t);
}
void _scene_refresh_rtree_collision_masks(RigidBody *rb) {
RtreeNode *rbLeaf = rigidbody_get_rtree_leaf(rb);
// refresh collision masks if in the rtree
if (rbLeaf != NULL) {
const uint16_t groups = rigidbody_get_groups(rb);
const uint16_t collidesWith = rigidbody_get_collides_with(rb);
if (groups != rtree_node_get_groups(rbLeaf) ||
collidesWith != rtree_node_get_collides_with(rbLeaf)) {
rtree_node_set_collision_masks(rbLeaf, groups, collidesWith);
}
}
}
void _scene_refresh_recurse(Scene *sc,
Transform *t,
bool hierarchyDirty,
const TICK_DELTA_SEC_T dt,
void *callbackData) {
// Refresh transform (top-first) after sandbox changes
transform_refresh(t, hierarchyDirty, false);
// Get rigidbody, compute world collider
Box collider;
RigidBody *rb = transform_get_or_compute_world_aligned_collider(t, &collider);
// Step physics (top-first), collider is kept up-to-date
if (rb != NULL) {
rigidbody_tick(sc, rb, t, &collider, sc->rtree, dt, callbackData);
}
// Refresh transform (top-first) after changes
transform_refresh(t, false, false);
// Update r-tree (top-first) after changes
if (rb != NULL) {
transform_get_or_compute_world_aligned_collider(t, &collider);
_scene_update_rtree(sc, rb, t, &collider);
}
// Recurse down the branch
// ⬆ anything above recursion is executed TOP-FIRST
DoublyLinkedListNode *n = transform_get_children_iterator(t);
while (n != NULL) {
_scene_refresh_recurse(sc,
(Transform *)doubly_linked_list_node_pointer(n),
hierarchyDirty || transform_is_hierarchy_dirty(t),
dt,
callbackData);
n = doubly_linked_list_node_next(n);
}
// ⬇ anything after recursion is executed DEEP-FIRST
// Clear intra-frame refresh flags (deep-first)
transform_refresh_children_done(t);
}
void _scene_end_of_frame_refresh_recurse(Scene *sc, Transform *t, bool hierarchyDirty) {
// Transform ends the frame inside scene hierarchy
transform_set_removed_from_scene(t, false);
// Refresh transform (top-first) after sandbox changes
transform_refresh(t, hierarchyDirty, false);
// Apply shape current transaction (top-first), this may change BB & collider
if (transform_get_type(t) == ShapeTransform) {
shape_apply_current_transaction(transform_utils_get_shape(t), false);
}
// Update r-tree (top-first) after sandbox changes
Box collider;
RigidBody *rb = transform_get_or_compute_world_aligned_collider(t, &collider);
if (rb != NULL) {
_scene_update_rtree(sc, rb, t, &collider);
_scene_refresh_rtree_collision_masks(rb);
}
// Recurse down the branch
// ⬆ anything above recursion is executed TOP-FIRST
DoublyLinkedListNode *n = transform_get_children_iterator(t);
while (n != NULL) {
_scene_end_of_frame_refresh_recurse(sc,
(Transform *)doubly_linked_list_node_pointer(n),
hierarchyDirty || transform_is_hierarchy_dirty(t));
n = doubly_linked_list_node_next(n);
}
// ⬇ anything after recursion is executed DEEP-FIRST
// Clear intra-frame refresh flags (deep-first)
transform_refresh_children_done(t);
#ifndef P3S_CLIENT_HEADLESS
// Refresh shape buffers (deep-first)
if (transform_get_type(t) == ShapeTransform) {
shape_refresh_vertices(transform_utils_get_shape(t));
}
#endif
}
bool _scene_shapes_iterator_func(Transform *t, void *ptr) {
if (transform_get_type(t) == ShapeTransform) {
doubly_linked_list_push_last((DoublyLinkedList *)ptr, (Shape *)transform_get_ptr(t));
}
return false;
}
bool _scene_standalone_refresh_func(Transform *t, void *ptr) {
if (transform_get_type(t) == ShapeTransform) {
shape_apply_current_transaction(transform_utils_get_shape(t), true);
}
transform_refresh(t, true, false);
return false;
}
void _scene_register_removed_transform(Scene *sc, Transform *t) {
if (sc == NULL || t == NULL) {
return;
}
transform_retain(t);
fifo_list_push(sc->removed, t);
}
// MARK: -
Scene *scene_new(Weakptr *g) {
Scene *sc = (Scene *)malloc(sizeof(Scene));
if (sc != NULL) {
sc->root = transform_make(PointTransform);
sc->system = transform_make(HierarchyTransform);
sc->map = NULL;
sc->rtree = rtree_new(RTREE_NODE_MIN_CAPACITY, RTREE_NODE_MAX_CAPACITY);
sc->wptr = NULL;
sc->game = g;
sc->removed = fifo_list_new();
sc->collisions = doubly_linked_list_new();
sc->awakeBoxes = doubly_linked_list_new();
float3_set(&sc->constantAcceleration, 0.0f, 0.0f, 0.0f);
transform_set_parent(sc->system, sc->root, false);
}
return sc;
}
void scene_free(Scene *sc) {
if (sc == NULL) {
return;
}
Transform *t = (Transform *)fifo_list_pop(sc->removed);
while (t != NULL) {
transform_release(t); // from scene_register_removed_transform
t = (Transform *)fifo_list_pop(sc->removed);
}
transform_release(sc->system);
transform_release(sc->root); // triggers release cascade in the hierarchy
rtree_free(sc->rtree);
weakptr_invalidate(sc->wptr);
fifo_list_free(sc->removed, NULL);
doubly_linked_list_flush(sc->collisions, _scene_collision_couple_free_func);
doubly_linked_list_free(sc->collisions);
doubly_linked_list_flush(sc->awakeBoxes, box_free_std);
doubly_linked_list_free(sc->awakeBoxes);
free(sc);
}
Weakptr *scene_get_weakptr(Scene *sc) {
if (sc->wptr == NULL) {
sc->wptr = weakptr_new(sc);
}
return sc->wptr;
}
Weakptr *scene_get_and_retain_weakptr(Scene *sc) {
if (sc->wptr == NULL) {
sc->wptr = weakptr_new(sc);
}
if (weakptr_retain(sc->wptr)) {
return sc->wptr;
} else { // this can only happen if weakptr ref count is at max
return NULL;
}
}
Transform *scene_get_root(Scene *sc) {
return sc->root;
}
Transform *scene_get_system_root(Scene *sc) {
return sc->system;
}
Rtree *scene_get_rtree(Scene *sc) {
return sc->rtree;
}
void scene_refresh(Scene *sc, const TICK_DELTA_SEC_T dt, void *callbackData) {
if (sc == NULL) {
return;
}
#if DEBUG_RIGIDBODY_EXTRA_LOGS
cclog_debug("🏞 physics step");
#endif
_scene_refresh_recurse(sc, sc->root, transform_is_hierarchy_dirty(sc->root), dt, callbackData);
}
void scene_end_of_frame_refresh(Scene *sc, void *callbackData) {
if (sc == NULL) {
return;
}
_scene_end_of_frame_refresh_recurse(sc, sc->root, transform_is_hierarchy_dirty(sc->root));
#if DEBUG_RTREE_CHECK
vx_assert(debug_rtree_integrity_check(sc->rtree));
#endif
// process transforms removal from hierarchy
Transform *t = (Transform *)fifo_list_pop(sc->removed);
DoublyLinkedListNode *n = NULL;
Transform *child = NULL;
RigidBody *rb = NULL;
while (t != NULL) {
// if still outside of hierarchy at end-of-frame, proceed with removal
if (transform_is_removed_from_scene(t)) {
// enqueue children for r-tree leaf removal
n = transform_get_children_iterator(t);
while (n != NULL) {
child = doubly_linked_list_node_pointer(n);
transform_set_removed_from_scene(child, true);
_scene_register_removed_transform(sc, child);
n = doubly_linked_list_node_next(n);
}
// r-tree leaf removal
rb = transform_get_rigidbody(t);
if (rb != NULL && rigidbody_get_rtree_leaf(rb) != NULL) {
rtree_remove(sc->rtree, rigidbody_get_rtree_leaf(rb), true);
rigidbody_set_rtree_leaf(rb, NULL);
}
}
transform_release(t); // from scene_register_removed_transform
t = (Transform *)fifo_list_pop(sc->removed);
}
// process collision couples for end-of-contact callback
n = doubly_linked_list_first(sc->collisions);
_CollisionCouple *cc;
Transform *t2;
while (n != NULL) {
cc = (_CollisionCouple *)doubly_linked_list_node_pointer(n);
t = weakptr_get(cc->t1);
t2 = weakptr_get(cc->t2);
if (t == NULL || t2 == NULL || cc->flag == false) {
if (t != NULL && t2 != NULL) {
rigidbody_fire_reciprocal_collision_end_callback(t, t2, callbackData);
}
_scene_collision_couple_free_func(cc);
DoublyLinkedListNode *next = doubly_linked_list_node_next(n);
doubly_linked_list_delete_node(sc->collisions, n);
n = next;
} else {
cc->flag = false;
n = doubly_linked_list_node_next(n);
}
}
// awake phase
FifoList *awakeQuery = fifo_list_new();
Box *awakeBox;
n = doubly_linked_list_first(sc->awakeBoxes);
while (n != NULL) {
// TODO: save groups in the list
awakeBox = (Box *)doubly_linked_list_node_pointer(n);
vx_assert(fifo_list_pop(awakeQuery) == NULL);
if (rtree_query_overlap_box(sc->rtree,
awakeBox,
PHYSICS_GROUP_ALL_SYSTEM,
PHYSICS_GROUP_ALL_SYSTEM,
NULL,
awakeQuery,
EPSILON_COLLISION) > 0) {
RtreeNode *hit = fifo_list_pop(awakeQuery);
Transform *hitLeaf = NULL;
RigidBody *hitRb = NULL;
while (hit != NULL) {
hitLeaf = (Transform *)rtree_node_get_leaf_ptr(hit);
vx_assert(rtree_node_is_leaf(hit));
hitRb = transform_get_rigidbody(hitLeaf);
vx_assert(hitRb != NULL);
rigidbody_set_awake(hitRb);
hit = fifo_list_pop(awakeQuery);
}
#if DEBUG_SCENE_CALLS
debug_scene_awake_queries++;
#endif
}
DoublyLinkedListNode *next = doubly_linked_list_node_next(n);
doubly_linked_list_delete_node(sc->awakeBoxes, n);
n = next;
box_free(awakeBox);
}
fifo_list_free(awakeQuery, NULL);
// physics layers mask changes take effect in the rtree at the end of each frame
rtree_refresh_collision_masks(sc->rtree);
}
void scene_standalone_refresh(Scene *sc) {
transform_recurse(sc->root, _scene_standalone_refresh_func, NULL, false);
}
DoublyLinkedList *scene_new_shapes_iterator(Scene *sc) {
DoublyLinkedList *list = doubly_linked_list_new();
transform_recurse(sc->root, _scene_shapes_iterator_func, list, true);
return list;
}
void scene_add_map(Scene *sc, Shape *map) {
vx_assert(sc != NULL);
vx_assert(map != NULL);
if (sc->map != NULL) {
transform_remove_parent(sc->map, true);
}
sc->map = shape_get_root_transform(map);
transform_set_parent(sc->map, sc->root, true);
#if DEBUG_SCENE_EXTRALOG
cclog_debug("🏞 map %p (id: %d) added to scene %p", sc->map, transform_get_id(sc->map), sc);
#endif
}
Transform *scene_get_map(Scene *sc) {
return sc->map;
}
bool scene_remove_transform(Scene *sc, Transform *t, const bool keepWorld) {
if (sc == NULL || t == NULL) {
return false;
}
if (transform_remove_parent(t, keepWorld)) {
_scene_register_removed_transform(sc, t);
#if DEBUG_SCENE_EXTRALOG
cclog_debug("🏞 transform %p (id: %d) removed from scene %p", t, transform_get_id(t), sc);
#endif
return true;
}
return false;
}
void scene_register_managed_transform(Scene *sc, Transform *t) {
if (sc == NULL || t == NULL) {
return;
}
transform_set_managed_ptr(t, sc->game);
}
bool _scene_register_collision_couple_func(void *ptr, void *data) {
const _CollisionCouple *cc1 = (_CollisionCouple *)ptr;
const _CollisionCouple *cc2 = (_CollisionCouple *)data;
return (weakptr_get(cc1->t1) == weakptr_get(cc2->t1) &&
weakptr_get(cc1->t2) == weakptr_get(cc2->t2)) ||
(weakptr_get(cc1->t1) == weakptr_get(cc2->t2) &&
weakptr_get(cc1->t2) == weakptr_get(cc2->t1));
}
CollisionCoupleStatus scene_register_collision_couple(Scene *sc,
Transform *t1,
Transform *t2,
float3 *wNormal) {
if (sc == NULL || t1 == NULL || t2 == NULL) {
return CollisionCoupleStatus_Discard;
}
vx_assert(wNormal != NULL);
_CollisionCouple *newCC = (_CollisionCouple *)malloc(sizeof(_CollisionCouple));
if (newCC == NULL) {
return CollisionCoupleStatus_Discard;
}
newCC->t1 = transform_get_and_retain_weakptr(t1);
newCC->t2 = transform_get_and_retain_weakptr(t2);
newCC->wNormal = *wNormal;
newCC->flag = true;
_CollisionCouple *existingCC;
if (doubly_linked_list_contains_func(sc->collisions,
_scene_register_collision_couple_func,
newCC,
(void **)&existingCC)) {
_scene_collision_couple_free_func(newCC);
*wNormal = existingCC->wNormal;
if (existingCC->flag) {
return CollisionCoupleStatus_Discard;
} else {
existingCC->flag = true;
return CollisionCoupleStatus_Tick;
}
}
doubly_linked_list_push_last(sc->collisions, newCC);
return CollisionCoupleStatus_Begin;
}
// MARK: - Physics -
void scene_set_constant_acceleration(Scene *sc, const float *x, const float *y, const float *z) {
vx_assert(sc != NULL);
if (x != NULL) {
sc->constantAcceleration.x = *x;
}
if (y != NULL) {
sc->constantAcceleration.y = *y;
}
if (z != NULL) {
sc->constantAcceleration.x = *z;
}
}
const float3 *scene_get_constant_acceleration(const Scene *sc) {
vx_assert(sc != NULL);
return &sc->constantAcceleration;
}
void scene_register_awake_box(Scene *sc, Box *b) {
float3 size;
box_get_size_float(b, &size);
if (float3_isZero(&size, EPSILON_COLLISION) == false) {
DoublyLinkedListNode *n = doubly_linked_list_first(sc->awakeBoxes);
Box *awakeBox;
while (n != NULL) {
awakeBox = doubly_linked_list_node_pointer(n);
if (box_collide_epsilon(awakeBox, b, EPSILON_ZERO)) {
box_op_merge(awakeBox, b, awakeBox);
box_free(b);
return;
}
n = doubly_linked_list_node_next(n);
}
doubly_linked_list_push_last(sc->awakeBoxes, b);
}
}
void scene_register_awake_rigidbody_contacts(Scene *sc, RigidBody *rb) {
if (rigidbody_get_rtree_leaf(rb) != NULL) {
Box *awakeBox = box_new_copy(rtree_node_get_aabb(rigidbody_get_rtree_leaf(rb)));
float3_op_add_scalar(&awakeBox->max, PHYSICS_AWAKE_DISTANCE);
float3_op_substract_scalar(&awakeBox->min, PHYSICS_AWAKE_DISTANCE);
scene_register_awake_box(sc, awakeBox);
}
}
void scene_register_awake_block_box(Scene *sc,
const Shape *shape,
const SHAPE_COORDS_INT_T x,
const SHAPE_COORDS_INT_T y,
const SHAPE_COORDS_INT_T z) {
Transform *t = shape_get_pivot_transform(shape);
const float3 modelPoint = {x + 0.5f, y + 0.5f, z + 0.5f};
float3 worldPoint;
matrix4x4_op_multiply_vec_point(&worldPoint, &modelPoint, transform_get_ltw(t));
float3 scale2;
transform_get_lossy_scale(t, &scale2);
float3_op_scale(&scale2, 0.5f);
Box *worldBox = box_new_2((float)worldPoint.x - scale2.x - PHYSICS_AWAKE_DISTANCE,
(float)worldPoint.y - scale2.y - PHYSICS_AWAKE_DISTANCE,
(float)worldPoint.z - scale2.z - PHYSICS_AWAKE_DISTANCE,
(float)worldPoint.x + scale2.x + PHYSICS_AWAKE_DISTANCE,
(float)worldPoint.y + scale2.y + PHYSICS_AWAKE_DISTANCE,
(float)worldPoint.z + scale2.z + PHYSICS_AWAKE_DISTANCE);
scene_register_awake_box(sc, worldBox);
}
CastResult scene_cast_result_default(void) {
CastResult hit;
hit.hitTr = NULL;
hit.block = NULL;
hit.blockCoords = coords3_zero;
hit.distance = FLT_MAX;
hit.type = Hit_None;
hit.faceTouched = FACE_NONE;
return hit;
}
HitType scene_cast_ray(Scene *sc,
const Ray *worldRay,
uint16_t groups,
const DoublyLinkedList *filterOutTransforms,
CastResult *result) {
CastResult hit = scene_cast_result_default();
if (result != NULL) {
*result = hit;
}
if (worldRay == NULL || groups == PHYSICS_GROUP_NONE) {
return Hit_None;
}
DoublyLinkedList *sceneQuery = doubly_linked_list_new();
if (rtree_query_cast_all_ray(sc->rtree,
worldRay,
PHYSICS_GROUP_NONE,
groups,
filterOutTransforms,
sceneQuery) > 0) {
// sort query results by distance
doubly_linked_list_sort_ascending(sceneQuery, rtree_utils_result_sort_func);
// process query results in order, to return first hit block or collision box
DoublyLinkedListNode *n = doubly_linked_list_first(sceneQuery);
RtreeCastResult *rtreeHit;
Transform *hitTr;
RigidBody *hitRb;
while (n != NULL) {
rtreeHit = (RtreeCastResult *)doubly_linked_list_node_pointer(n);
hitTr = (Transform *)rtree_node_get_leaf_ptr(rtreeHit->rtreeLeaf);
hitRb = transform_get_rigidbody(hitTr);
// re-examine closer hits after updating hit.distance vs. per-block or rotated collider
if (rtreeHit->distance >= hit.distance) {
break;
}
const RigidbodyMode mode = rigidbody_get_simulation_mode(hitRb);
if (mode == RigidbodyMode_Dynamic) {
hit.hitTr = hitTr;
hit.distance = rtreeHit->distance;
hit.type = Hit_CollisionBox;
} else if (transform_get_type(hitTr) == ShapeTransform &&
rigidbody_uses_per_block_collisions(transform_get_rigidbody(hitTr))) {
CastResult blockHit;
Block *b = scene_cast_ray_shape_only(sc,
transform_utils_get_shape(hitTr),
worldRay,
&blockHit);
if (b != NULL && blockHit.distance < hit.distance) {
hit = blockHit;
}
} else {
// solve non-dynamic rigidbodies in their model space (rotated collider)
const Box *collider = rigidbody_get_collider(hitRb);
Transform *modelTr = transform_get_type(hitTr) == ShapeTransform
? shape_get_pivot_transform(
transform_utils_get_shape(hitTr))
: hitTr;
Ray *modelRay = ray_world_to_local(worldRay, modelTr);
float distance;
if (ray_intersect_with_box(modelRay, &collider->min, &collider->max, &distance)) {
const float3 modelVector = {modelRay->dir->x * distance,
modelRay->dir->y * distance,
modelRay->dir->z * distance};
float3 worldVector;
transform_utils_vector_ltw(modelTr, &modelVector, &worldVector);
distance = float3_length(&worldVector);
if (distance < hit.distance) {
hit.hitTr = hitTr;
hit.distance = distance;
hit.type = Hit_CollisionBox;
}
}
ray_free(modelRay);
}
n = doubly_linked_list_node_next(n);
}
}
doubly_linked_list_flush(sceneQuery, free);
doubly_linked_list_free(sceneQuery);
if (result != NULL) {
*result = hit;
}
return hit.type;
}
size_t scene_cast_all_ray(Scene *sc,
const Ray *worldRay,
uint16_t groups,
const DoublyLinkedList *filterOutTransforms,
DoublyLinkedList *results) {
if (worldRay == NULL || results == NULL || groups == PHYSICS_GROUP_NONE) {
return 0;
}
DoublyLinkedList *sceneQuery = doubly_linked_list_new();
size_t count = 0;
if (rtree_query_cast_all_ray(sc->rtree,
worldRay,
PHYSICS_GROUP_NONE,
groups,
filterOutTransforms,
sceneQuery) > 0) {
// process query results to confirm intersections w/ per-block and rotated colliders
DoublyLinkedListNode *n = doubly_linked_list_first(sceneQuery);
RtreeCastResult *rtreeHit;
Transform *hitTr;
RigidBody *hitRb;
CastResult *hit;
while (n != NULL) {
rtreeHit = (RtreeCastResult *)doubly_linked_list_node_pointer(n);
hitTr = (Transform *)rtree_node_get_leaf_ptr(rtreeHit->rtreeLeaf);
hitRb = transform_get_rigidbody(hitTr);
hit = NULL;
const RigidbodyMode mode = rigidbody_get_simulation_mode(hitRb);
if (mode == RigidbodyMode_Dynamic) {
hit = (CastResult *)malloc(sizeof(CastResult));
hit->hitTr = hitTr;
hit->distance = rtreeHit->distance;
hit->type = Hit_CollisionBox;
} else if (transform_get_type(hitTr) == ShapeTransform &&
rigidbody_uses_per_block_collisions(transform_get_rigidbody(hitTr))) {
CastResult blockHit;
if (scene_cast_ray_shape_only(sc,
transform_utils_get_shape(hitTr),
worldRay,
&blockHit)) {
hit = (CastResult *)malloc(sizeof(CastResult));
*hit = blockHit;
}
} else {
// solve non-dynamic rigidbodies in their model space (rotated collider)
const Box *collider = rigidbody_get_collider(hitRb);
Transform *modelTr = transform_get_type(hitTr) == ShapeTransform
? shape_get_pivot_transform(
transform_utils_get_shape(hitTr))
: hitTr;
Ray *modelRay = ray_world_to_local(worldRay, modelTr);
float distance;
if (ray_intersect_with_box(modelRay, &collider->min, &collider->max, &distance)) {
const float3 modelVector = {modelRay->dir->x * distance,
modelRay->dir->y * distance,
modelRay->dir->z * distance};
float3 worldVector;
transform_utils_vector_ltw(modelTr, &modelVector, &worldVector);
hit = (CastResult *)malloc(sizeof(CastResult));
hit->hitTr = hitTr;
hit->distance = float3_length(&worldVector);
hit->type = Hit_CollisionBox;
}
ray_free(modelRay);
}
if (hit != NULL) {
doubly_linked_list_push_last(results, hit);
++count;
}
n = doubly_linked_list_node_next(n);
}
}
doubly_linked_list_flush(sceneQuery, free);
doubly_linked_list_free(sceneQuery);
// sort query results by distance
doubly_linked_list_sort_ascending(results, rtree_utils_result_sort_func);
return count;
}
Block *scene_cast_ray_shape_only(Scene *sc,
const Shape *sh,
const Ray *worldRay,
CastResult *result) {
CastResult hit = scene_cast_result_default();
if (result != NULL) {
*result = hit;
}
if (sh == NULL)
return NULL;
float3 localImpact;
shape_ray_cast(sh, worldRay, &hit.distance, &localImpact, &hit.block, &hit.blockCoords);
if (hit.block != NULL) {
// find which side local impact is relative to touched block
float3 ldf = {hit.blockCoords.x, hit.blockCoords.y, hit.blockCoords.z};
const FACE_INDEX_INT_T face = ray_impacted_block_face(&localImpact, &ldf);
hit.hitTr = shape_get_root_transform(sh);
hit.type = Hit_Block;
hit.faceTouched = face;
}
if (result != NULL) {
*result = hit;
}
return hit.block;
}
HitType scene_cast_box(Scene *sc,
const Box *aabb,
const float3 *unit,
float maxDist,
uint16_t groups,
const DoublyLinkedList *filterOutTransforms,
CastResult *result) {
CastResult hit = scene_cast_result_default();
if (result != NULL) {
*result = hit;
}
if (aabb == NULL || unit == NULL || groups == PHYSICS_GROUP_NONE) {
return Hit_None;
}
DoublyLinkedList *sceneQuery = doubly_linked_list_new();
if (rtree_query_cast_all_box(sc->rtree,
aabb,
unit,
maxDist,
PHYSICS_GROUP_NONE,
groups,
filterOutTransforms,
sceneQuery)) {
// sort query results by distance
doubly_linked_list_sort_ascending(sceneQuery, rtree_utils_result_sort_func);
// process query results in order, to return first hit block or collision box
DoublyLinkedListNode *n = doubly_linked_list_first(sceneQuery);
RtreeCastResult *rtreeHit;
Transform *hitTr;
RigidBody *hitRb;
while (n != NULL) {
rtreeHit = (RtreeCastResult *)doubly_linked_list_node_pointer(n);
hitTr = (Transform *)rtree_node_get_leaf_ptr(rtreeHit->rtreeLeaf);
hitRb = transform_get_rigidbody(hitTr);
// re-examine closer hits after updating hit.distance vs. per-block or rotated collider
if (rtreeHit->distance >= hit.distance) {
break;
}
const RigidbodyMode mode = rigidbody_get_simulation_mode(hitRb);
if (mode == RigidbodyMode_Dynamic) {
hit.hitTr = hitTr;
hit.distance = rtreeHit->distance;
hit.type = Hit_CollisionBox;
} else {
Box modelBox, modelBroadphase;
float3 modelVector, modelEpsilon;
Shape *hitShape = transform_utils_get_shape(hitTr);
float3 vector = {unit->x * maxDist, unit->y * maxDist, unit->z * maxDist};
// solve non-dynamic rigidbodies in their model space (rotated collider)
const Box *collider = rigidbody_get_collider(hitRb);
const Matrix4x4 *invModel = transform_get_wtl(
hitShape != NULL ? shape_get_pivot_transform(hitShape) : hitTr);
rigidbody_broadphase_world_to_model(invModel,
aabb,
&modelBox,
&vector,
&modelVector,
EPSILON_COLLISION,
&modelEpsilon);
box_set_broadphase_box(&modelBox, &modelVector, &modelBroadphase);
if (box_collide(&modelBroadphase, collider)) {
// shapes may enable per-block collisions
if (hitShape != NULL && rigidbody_uses_per_block_collisions(hitRb)) {
Block *block = NULL;
SHAPE_COORDS_INT3_T blockCoords;
float3 normal;
const float swept = shape_box_cast(hitShape,
&modelBox,
&modelVector,
&modelEpsilon,
false,
&normal,
NULL,
&block,
&blockCoords);
if (swept < 1.0f) {
float3_op_scale(&modelVector, swept);
float3 worldVector, worldNormal;
transform_utils_vector_ltw(shape_get_pivot_transform(hitShape),
&modelVector,
&worldVector);
transform_utils_vector_ltw(shape_get_pivot_transform(hitShape),
&normal,
&worldNormal);
const float distance = float3_length(&worldVector);
if (distance < hit.distance) {
hit.hitTr = hitTr;
hit.block = block;
hit.distance = distance;
hit.type = Hit_Block;
hit.blockCoords = blockCoords;
hit.faceTouched = utils_aligned_normal_to_face(&worldNormal);
}
}
} else {
const float swept = box_swept(&modelBox,
&modelVector,
rigidbody_get_collider(hitRb),
&modelEpsilon,
false,
NULL,
NULL);
if (swept < 1.0f) {
float3_op_scale(&modelVector, swept);
float3 worldVector;
transform_utils_vector_ltw(
hitShape != NULL ? shape_get_pivot_transform(hitShape) : hitTr,
&modelVector,
&worldVector);
const float distance = float3_length(&worldVector);
if (distance < hit.distance) {
hit.hitTr = hitTr;
hit.distance = distance;
hit.type = Hit_CollisionBox;
}
}
}
}
}
n = doubly_linked_list_node_next(n);
}
}
doubly_linked_list_flush(sceneQuery, free);
doubly_linked_list_free(sceneQuery);
if (result != NULL) {
*result = hit;
}
return hit.type;
}
size_t scene_cast_all_box(Scene *sc,
const Box *aabb,
const float3 *unit,
float maxDist,
uint16_t groups,
const DoublyLinkedList *filterOutTransforms,
DoublyLinkedList *results) {
if (aabb == NULL || unit == NULL || results == NULL || groups == PHYSICS_GROUP_NONE) {
return 0;
}
DoublyLinkedList *sceneQuery = doubly_linked_list_new();
size_t count = 0;
if (rtree_query_cast_all_box(sc->rtree,
aabb,
unit,
maxDist,
PHYSICS_GROUP_NONE,
groups,
filterOutTransforms,
sceneQuery)) {
// process query results to confirm intersections w/ per-block and rotated colliders
DoublyLinkedListNode *n = doubly_linked_list_first(sceneQuery);
RtreeCastResult *rtreeHit;
Transform *hitTr;
RigidBody *hitRb;
CastResult *hit;
while (n != NULL) {