dynamis-world 0.7.0

Host scene registry, step orchestration, and readback
Documentation
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
use super::common::{DT, new_world, settle, static_config};
use dynamis_model::{BodyDesc, QueryFilter, Shape};
use dynamis_world::World;
use std::panic::{AssertUnwindSafe, catch_unwind};

fn query_static(world: &mut World, radius: f32, position: [f32; 3]) -> dynamis_model::BodyHandle {
    world.spawn(BodyDesc::static_sphere(radius).position(position))
}

#[test]
fn ray_hits_nearest_and_reports_surface() {
    let mut world = new_world(static_config());
    let near = query_static(&mut world, 0.5, [0.0, 0.0, 2.0]);
    let _far = query_static(&mut world, 0.5, [0.0, 0.0, 10.0]);
    let query = world.ray_query(
        [0.0, 0.0, 0.0],
        [0.0, 0.0, 1.0],
        20.0,
        &QueryFilter::default(),
    );
    world.step(DT);
    world.wait();
    let hit = world.query_hit(query).expect("raycast must hit");
    assert_eq!(hit.body, near);
    assert!((hit.distance - 1.5).abs() < 1e-3);
    assert!((hit.point[2] - 1.5).abs() < 1e-3);
    assert!(
        (hit.normal[2] + 1.0).abs() < 1e-3,
        "normal must face the ray"
    );
    assert_eq!(hit.step, 0);
}

#[test]
fn ray_miss_variants() {
    let mut world = new_world(static_config());
    let _target = query_static(&mut world, 0.5, [0.0, 0.0, 10.0]);
    let short = world.ray_query(
        [0.0, 0.0, 0.0],
        [0.0, 0.0, 1.0],
        5.0,
        &QueryFilter::default(),
    );
    world.step(DT);
    world.wait();
    assert_eq!(world.query_hit(short), None, "out-of-range ray must miss");

    let mut behind = new_world(static_config());
    query_static(&mut behind, 0.5, [0.0, 0.0, -5.0]);
    let backward = behind.ray_query(
        [0.0, 0.0, 0.0],
        [0.0, 0.0, 1.0],
        20.0,
        &QueryFilter::default(),
    );
    behind.step(DT);
    behind.wait();
    assert_eq!(
        behind.query_hit(backward),
        None,
        "behind-body ray must miss"
    );
}

#[test]
fn ray_from_inside_body_returns_exit_distance() {
    let mut world = new_world(static_config());
    let target = query_static(&mut world, 0.5, [0.0, 0.0, 0.0]);
    let query = world.ray_query(
        [0.0, 0.0, 0.0],
        [0.0, 0.0, 1.0],
        20.0,
        &QueryFilter::default(),
    );
    world.step(DT);
    world.wait();
    let hit = world.query_hit(query).expect("origin inside body must hit");
    assert_eq!(hit.body, target);
    assert!(
        (hit.distance - 0.5).abs() < 1e-3,
        "inside ray must exit at the radius"
    );
}

#[test]
fn sphere_query_reports_penetration_and_miss() {
    let mut world = new_world(static_config());
    let target = query_static(&mut world, 0.5, [0.0, 0.0, 0.0]);
    let overlap = world.sphere_query([0.0, 0.0, 0.8], 0.5, &QueryFilter::default());
    let miss = world.sphere_query([0.0, 0.0, 5.0], 0.5, &QueryFilter::default());
    world.step(DT);
    world.wait();
    let hit = world.query_hit(overlap).expect("overlap must hit");
    assert_eq!(hit.body, target);
    assert!(
        (hit.distance - (-0.2)).abs() < 1e-3,
        "penetration depth must be -0.2, got {}",
        hit.distance
    );
    assert_eq!(world.query_hit(miss), None);
}

#[test]
fn cuboid_query_reports_overlap_and_outside() {
    let mut world = new_world(static_config());
    query_static(&mut world, 1.0, [0.0, 0.0, 0.0]);
    let inside = world.cuboid_query([0.0, 0.0, 0.0], [2.0, 2.0, 2.0], &QueryFilter::default());
    let outside = world.cuboid_query([0.0, 0.0, 10.0], [1.0, 1.0, 1.0], &QueryFilter::default());
    world.step(DT);
    world.wait();
    assert!(world.query_hit(inside).is_some(), "box overlap must hit");
    assert_eq!(world.query_hit(outside), None, "disjoint box must miss");
}

#[test]
fn sweep_query_stops_at_surface() {
    let mut world = new_world(static_config());
    let wall = query_static(&mut world, 2.0, [0.0, 0.0, 10.0]);
    let query = world.sweep_query(
        &Shape::sphere(0.3),
        [0.0, 0.0, 0.0, 1.0],
        [0.0, 0.0, 0.0],
        [0.0, 0.0, 1.0],
        30.0,
        &QueryFilter::default(),
    );
    world.step(DT);
    world.wait();
    let hit = world.query_hit(query).expect("sweep must hit the wall");
    assert_eq!(hit.body, wall);
    assert!(
        (hit.distance - 7.7).abs() < 0.05,
        "sweep must stop at the surface (7.7), got {}",
        hit.distance
    );
}

#[test]
fn filters_skip_each_body_kind() {
    let mut world = new_world(static_config());
    query_static(&mut world, 0.5, [0.0, 0.0, 2.0]);
    let kinematic = world.spawn(
        BodyDesc::sphere(0.5)
            .kinematic(true)
            .position([0.0, 0.0, 4.0]),
    );
    let sensor = world.spawn(BodyDesc::sphere(0.5).sensor(true).position([0.0, 0.0, 6.0]));
    let dynamic = world.spawn(BodyDesc::sphere(0.5).position([0.0, 0.0, 8.0]));
    world.sleep(dynamic);
    settle(&mut world, 1);

    let plain = world.ray_query(
        [0.0, 0.0, 0.0],
        [0.0, 0.0, 1.0],
        20.0,
        &QueryFilter::default(),
    );
    let no_static = world.ray_query(
        [0.0, 0.0, 0.0],
        [0.0, 0.0, 1.0],
        20.0,
        &QueryFilter {
            ignore_static: true,
            ..QueryFilter::default()
        },
    );
    let no_static_kinematic = world.ray_query(
        [0.0, 0.0, 0.0],
        [0.0, 0.0, 1.0],
        20.0,
        &QueryFilter {
            ignore_static: true,
            ignore_kinematic: true,
            ..QueryFilter::default()
        },
    );
    let also_no_sleeping = world.ray_query(
        [0.0, 0.0, 0.0],
        [0.0, 0.0, 1.0],
        20.0,
        &QueryFilter {
            ignore_static: true,
            ignore_kinematic: true,
            ignore_sleeping: true,
            ..QueryFilter::default()
        },
    );
    let include_sensors = world.ray_query(
        [0.0, 0.0, 0.0],
        [0.0, 0.0, 1.0],
        20.0,
        &QueryFilter {
            ignore_static: true,
            ignore_kinematic: true,
            ignore_sleeping: true,
            ignore_sensors: false,
            ..QueryFilter::default()
        },
    );
    world.step(DT);
    world.wait();

    let plain = world
        .query_hit(plain)
        .expect("static must be hit by default");
    assert!((plain.distance - 1.5).abs() < 1e-3);
    let no_static = world.query_hit(no_static).expect("kinematic must be hit");
    assert_eq!(no_static.body, kinematic);
    assert!((no_static.distance - 3.5).abs() < 1e-3);
    let no_static_kinematic = world
        .query_hit(no_static_kinematic)
        .expect("dynamic must be hit even while sleeping by default");
    assert_eq!(no_static_kinematic.body, dynamic);
    assert!((no_static_kinematic.distance - 7.5).abs() < 1e-3);
    assert_eq!(
        world.query_hit(also_no_sleeping),
        None,
        "sleeping body must be skipped when ignored"
    );
    let sensor_hit = world
        .query_hit(include_sensors)
        .expect("sensor must be hit when not ignored");
    assert_eq!(sensor_hit.body, sensor);
    assert!((sensor_hit.distance - 5.5).abs() < 1e-3);
}

#[test]
fn group_and_mask_filters_select_bodies() {
    let mut world = new_world(static_config());
    let a = world.spawn(
        BodyDesc::static_sphere(0.5)
            .position([0.0, 0.0, 2.0])
            .collision_group(1)
            .collision_mask(1),
    );
    let b = world.spawn(
        BodyDesc::static_sphere(0.5)
            .position([0.0, 0.0, 4.0])
            .collision_group(2)
            .collision_mask(2),
    );
    let group_one = world.ray_query(
        [0.0, 0.0, 0.0],
        [0.0, 0.0, 1.0],
        20.0,
        &QueryFilter {
            group: 1,
            mask: 0xFFFF_FFFF,
            ..QueryFilter::default()
        },
    );
    let group_two = world.ray_query(
        [0.0, 0.0, 0.0],
        [0.0, 0.0, 1.0],
        20.0,
        &QueryFilter {
            group: 2,
            mask: 0xFFFF_FFFF,
            ..QueryFilter::default()
        },
    );
    let mask_excludes_a = world.ray_query(
        [0.0, 0.0, 0.0],
        [0.0, 0.0, 1.0],
        20.0,
        &QueryFilter {
            group: 1,
            mask: 2,
            ..QueryFilter::default()
        },
    );
    world.step(DT);
    world.wait();
    let one = world.query_hit(group_one).expect("group 1 must hit");
    assert_eq!(one.body, a);
    let two = world.query_hit(group_two).expect("group 2 must hit");
    assert_eq!(two.body, b);
    let excluded = world.query_hit(mask_excludes_a);
    assert_eq!(
        excluded, None,
        "mask must exclude bodies whose group does not intersect it"
    );
}

#[test]
fn multi_hit_query_reports_all_in_distance_order() {
    let mut world = new_world(static_config());
    query_static(&mut world, 0.4, [0.0, 0.0, 2.0]);
    query_static(&mut world, 0.4, [0.0, 0.0, 2.7]);
    query_static(&mut world, 0.4, [0.0, 0.0, 3.4]);
    let all = world.sphere_query(
        [0.0, 0.0, 2.7],
        1.2,
        &QueryFilter {
            max_hits: 4,
            ..QueryFilter::default()
        },
    );
    let capped = world.sphere_query(
        [0.0, 0.0, 2.7],
        1.2,
        &QueryFilter {
            max_hits: 1,
            ..QueryFilter::default()
        },
    );
    world.step(DT);
    world.wait();
    let hits = world.query_hits(all);
    assert_eq!(hits.len(), 3, "all three overlaps must be reported");
    assert!(
        hits.windows(2)
            .all(|pair| pair[0].distance <= pair[1].distance)
    );
    assert!(!world.query_overflow(all));
    assert_eq!(world.query_hits(capped).len(), 1);
    assert!(world.query_overflow(capped), "capped query must overflow");
}

#[test]
fn batched_queries_resolve_in_submission_order() {
    let mut world = new_world(static_config());
    let near = query_static(&mut world, 0.5, [0.0, 0.0, 2.0]);
    let far = query_static(&mut world, 0.5, [0.0, 0.0, 8.0]);
    let first = world.ray_query(
        [0.0, 0.0, 0.0],
        [0.0, 0.0, 1.0],
        30.0,
        &QueryFilter::default(),
    );
    let second = world.ray_query(
        [0.0, 0.0, 9.5],
        [0.0, 0.0, -1.0],
        30.0,
        &QueryFilter::default(),
    );
    world.step(DT);
    world.wait();
    assert_eq!(world.query_hit(first).expect("first must hit").body, near);
    assert_eq!(world.query_hit(second).expect("second must hit").body, far);
}

#[test]
fn results_persist_until_slot_reused() {
    let mut world = new_world(static_config());
    let target = query_static(&mut world, 0.5, [0.0, 0.0, 2.0]);
    let query = world.ray_query(
        [0.0, 0.0, 0.0],
        [0.0, 0.0, 1.0],
        10.0,
        &QueryFilter::default(),
    );
    world.step(DT);
    world.wait();
    let hit = world.query_hit(query).expect("first must hit");
    assert_eq!(hit.body, target);
    settle(&mut world, 5);
    assert_eq!(world.query_hit(query), Some(hit));
}

#[test]
fn retired_batch_invalidates_handle() {
    let mut world = new_world(static_config());
    let first = world.ray_query(
        [0.0, 0.0, 0.0],
        [0.0, 0.0, 1.0],
        10.0,
        &QueryFilter::default(),
    );
    world.step(DT);
    world.wait();
    let _ = world.query_hit(first);
    for _ in 0..4 {
        world.ray_query(
            [0.0, 0.0, 0.0],
            [0.0, 0.0, 1.0],
            10.0,
            &QueryFilter::default(),
        );
        world.step(DT);
        world.wait();
    }
    assert!(
        catch_unwind(AssertUnwindSafe(|| world.query_hit(first))).is_err(),
        "a handle whose batch has been retired must stop resolving"
    );
}

#[test]
fn handle_without_arrived_results_panics() {
    let mut world = new_world(static_config());
    let pending = world.ray_query(
        [0.0, 0.0, 0.0],
        [0.0, 0.0, 1.0],
        10.0,
        &QueryFilter::default(),
    );
    assert!(
        catch_unwind(AssertUnwindSafe(|| world.query_hit(pending))).is_err(),
        "reading a query before its step has run must panic"
    );
}

#[test]
fn raycast_resolves_against_latest_state() {
    let mut world = new_world(static_config());
    let target = query_static(&mut world, 0.5, [0.0, 0.0, 2.0]);
    world.step(DT);
    world.wait();
    world.set_position(target, [0.0, 0.0, 30.0]);
    let query = world.ray_query(
        [0.0, 0.0, 0.0],
        [0.0, 0.0, 1.0],
        10.0,
        &QueryFilter::default(),
    );
    world.step(DT);
    world.wait();
    assert_eq!(
        world.query_hit(query),
        None,
        "query must run against the post-teleport state"
    );
}

#[test]
fn query_validation_panics() {
    let mut world = new_world(static_config());
    assert!(
        catch_unwind(AssertUnwindSafe(|| {
            world.sphere_query(
                [0.0, 0.0, 0.0],
                1.0,
                &QueryFilter {
                    max_hits: 32,
                    ..QueryFilter::default()
                },
            );
        }))
        .is_err(),
        "a request above the hit lane count must fail fast"
    );
    assert!(
        catch_unwind(AssertUnwindSafe(|| {
            world.ray_query(
                [0.0, 0.0, 0.0],
                [0.0, 0.0, 1.0],
                0.0,
                &QueryFilter::default(),
            );
        }))
        .is_err()
    );
    assert!(
        catch_unwind(AssertUnwindSafe(|| {
            world.ray_query(
                [0.0, 0.0, 0.0],
                [0.0, 0.0, 0.0],
                10.0,
                &QueryFilter::default(),
            );
        }))
        .is_err()
    );
    assert!(
        catch_unwind(AssertUnwindSafe(|| {
            world.sphere_query([0.0, 0.0, 0.0], 0.0, &QueryFilter::default());
        }))
        .is_err()
    );
    assert!(
        catch_unwind(AssertUnwindSafe(|| {
            world.cuboid_query([0.0; 3], [0.0, 1.0, 1.0], &QueryFilter::default());
        }))
        .is_err()
    );
    assert!(
        catch_unwind(AssertUnwindSafe(|| {
            world.sweep_query(
                &Shape::sphere(0.3),
                [0.0, 0.0, 0.0, 1.0],
                [0.0, 0.0, 0.0],
                [0.0, 0.0, 1.0],
                0.0,
                &QueryFilter::default(),
            );
        }))
        .is_err()
    );
    assert!(
        catch_unwind(AssertUnwindSafe(|| {
            world.sweep_query(
                &Shape::sphere(0.3),
                [0.0, 0.0, 0.0, 1.0],
                [0.0, 0.0, 0.0],
                [0.0, 0.0, 0.0],
                10.0,
                &QueryFilter::default(),
            );
        }))
        .is_err()
    );
    assert!(
        catch_unwind(AssertUnwindSafe(|| {
            world.sweep_query(
                &Shape::sphere(0.3),
                [1.0, 0.5, 0.0, 0.0],
                [0.0, 0.0, 0.0],
                [0.0, 0.0, 1.0],
                10.0,
                &QueryFilter::default(),
            );
        }))
        .is_err()
    );
}

#[test]
fn point_query_detects_inside_and_outside() {
    let mut world = new_world(static_config());
    let body = query_static(&mut world, 0.5, [0.0, 0.0, 0.0]);
    world.step(DT);
    world.wait();
    let inside = world.point_query([0.1, 0.0, 0.0], &QueryFilter::default());
    world.wait();
    let inside_hit = world.query_hit(inside);
    assert_eq!(inside_hit.map(|hit| hit.body), Some(body));
    let outside = world.point_query([5.0, 0.0, 0.0], &QueryFilter::default());
    world.wait();
    assert!(
        world.query_hit(outside).is_none(),
        "point outside must miss"
    );
    let edge = world.point_query([0.5, 0.0, 0.0], &QueryFilter::default());
    world.wait();
    assert!(
        world.query_hit(edge).is_some(),
        "point on the surface must hit"
    );
}

#[test]
fn overlap_query_accepts_any_convex_shape() {
    let mut world = new_world(static_config());
    let body = query_static(&mut world, 0.5, [0.0, 0.0, 0.0]);
    world.add_collider(
        body,
        dynamis_model::ColliderDesc::new(Shape::cylinder(0.4, 0.3)).offset([2.0, 0.0, 0.0]),
    );
    world.step(DT);
    world.wait();
    let probe = Shape::cylinder(0.2, 0.2);
    let handle = world.overlap_query(
        &probe,
        [0.0, 0.0, 0.0, 1.0],
        [2.2, 0.0, 0.0],
        &QueryFilter::default(),
    );
    world.wait();
    let hit = world.query_hit(handle);
    assert_eq!(hit.map(|hit| (hit.body, hit.collider)), Some((body, 1)));
    let miss = world.overlap_query(
        &probe,
        [0.0, 0.0, 0.0, 1.0],
        [8.0, 0.0, 0.0],
        &QueryFilter::default(),
    );
    world.wait();
    assert!(world.query_hit(miss).is_none());
}

#[test]
fn include_filter_limits_results_to_one_body() {
    let mut world = new_world(static_config());
    let first = query_static(&mut world, 0.5, [0.0, 0.0, 0.0]);
    let second = query_static(&mut world, 0.5, [3.0, 0.0, 0.0]);
    world.step(DT);
    world.wait();
    let filter = QueryFilter {
        include: Some(second),
        exclude: None,
        max_hits: 4,
        ..Default::default()
    };
    let handle = world.ray_query([0.0, 0.0, 0.0], [1.0, 0.0, 0.0], 100.0, &filter);
    world.wait();
    let hits = world.query_hits(handle);
    assert!(!hits.is_empty(), "include query must not be empty");
    assert!(
        hits.iter().all(|hit| hit.body == second),
        "include filter must only return the target body"
    );
    assert!(!hits.iter().any(|hit| hit.body == first));
}

#[test]
fn capsule_down_sweep_normal_is_vertical() {
    let mut world = new_world(static_config());
    world.spawn(
        BodyDesc::cuboid([20.0, 0.5, 20.0])
            .mass(0.0)
            .position([0.0, -0.5, 0.0]),
    );
    world.step(DT);
    world.wait();
    let probe = Shape::capsule(0.4, 0.5);
    for x in [0.0f32, 0.2, 0.4, 0.6, 0.8, 1.0, 1.4, 2.0, 3.0] {
        let handle = world.sweep_query(
            &probe,
            [0.0, 0.0, 0.0, 1.0],
            [x, 0.95, 0.0],
            [0.0, -1.0, 0.0],
            0.5,
            &QueryFilter::default(),
        );
        world.wait();
        let hit = world
            .query_hit(handle)
            .expect("down sweep must hit the floor");
        eprintln!("x={x} d={} n={:?}", hit.distance, hit.normal);
    }
}

#[test]
fn capsule_sweep_stops_before_wall_face() {
    let mut world = new_world(static_config());
    world.spawn(
        BodyDesc::cuboid([0.25, 3.0, 4.0])
            .mass(0.0)
            .position([2.0, 1.5, 0.0]),
    );
    world.step(DT);
    world.wait();
    let probe = Shape::sphere(0.4);
    let handle = world.sweep_query(
        &probe,
        [0.0, 0.0, 0.0, 1.0],
        [1.3, 0.95, 0.0],
        [1.0, 0.0, 0.0],
        0.5,
        &QueryFilter::default(),
    );
    world.wait();
    let hit = world
        .query_hit(handle)
        .expect("sweep must stop at the wall");
    assert!(
        hit.distance > 0.0 && hit.distance < 0.25,
        "sweep must stop short of the wall face, got {}",
        hit.distance
    );
    assert!(
        hit.normal[0] > 0.99,
        "wall normal must face the capsule, got {:?}",
        hit.normal
    );
}

#[test]
fn query_after_teleport_sees_the_new_position() {
    let mut world = new_world(static_config());
    let body = query_static(&mut world, 0.5, [0.0, 0.0, 10.0]);

    world.set_position(body, [0.0, 0.0, 2.0]);
    let query = world.ray_query(
        [0.0, 0.0, 0.0],
        [0.0, 0.0, 1.0],
        20.0,
        &QueryFilter::default(),
    );
    world.wait();
    let hit = world
        .query_hit(query)
        .expect("ray must hit the teleported body");
    assert_eq!(hit.body, body);
    assert!((hit.distance - 1.5).abs() < 1e-3, "got {}", hit.distance);
    assert!(!world.query_overflow(query));

    let stale = world.point_query([0.0, 0.0, 10.0], &QueryFilter::default());
    world.wait();
    assert!(
        world.query_hit(stale).is_none(),
        "stale entries must not answer for the old position"
    );
}

#[test]
fn truncated_ray_query_keeps_the_closest_hits() {
    let mut world = new_world(static_config());
    let nearest = query_static(&mut world, 0.5, [0.0, 0.0, 2.0]);
    let middle = query_static(&mut world, 0.5, [0.0, 0.0, 4.0]);
    let _farthest = query_static(&mut world, 0.5, [0.0, 0.0, 6.0]);
    let single_handle = world.ray_query(
        [0.0, 0.0, 0.0],
        [0.0, 0.0, 1.0],
        20.0,
        &QueryFilter {
            max_hits: 1,
            ..QueryFilter::default()
        },
    );
    let pair_handle = world.ray_query(
        [0.0, 0.0, 0.0],
        [0.0, 0.0, 1.0],
        20.0,
        &QueryFilter {
            max_hits: 2,
            ..QueryFilter::default()
        },
    );
    world.step(DT);
    world.wait();
    let single = world.query_hits(single_handle);
    assert_eq!(single.len(), 1);
    assert_eq!(single[0].body, nearest);
    assert!((single[0].distance - 1.5).abs() < 1e-3);
    assert!(world.query_overflow(single_handle));
    let pair = world.query_hits(pair_handle);
    assert_eq!(pair.len(), 2);
    assert_eq!(pair[0].body, nearest);
    assert_eq!(pair[1].body, middle);
    assert!((pair[1].distance - 3.5).abs() < 1e-3);
    assert!(world.query_overflow(pair_handle));
}

#[test]
fn truncated_sweep_query_keeps_the_closest_obstacle() {
    let mut world = new_world(static_config());
    let _near_wall = world.spawn(
        BodyDesc::cuboid([0.25, 3.0, 4.0])
            .mass(0.0)
            .position([2.0, 1.5, 0.0]),
    );
    let _far_wall = world.spawn(
        BodyDesc::cuboid([0.25, 3.0, 4.0])
            .mass(0.0)
            .position([8.0, 1.5, 0.0]),
    );
    world.step(DT);
    world.wait();
    let handle = world.sweep_query(
        &Shape::sphere(0.4),
        [0.0, 0.0, 0.0, 1.0],
        [1.3, 0.95, 0.0],
        [1.0, 0.0, 0.0],
        10.0,
        &QueryFilter {
            max_hits: 1,
            ..QueryFilter::default()
        },
    );
    world.wait();
    let hit = world.query_hit(handle).expect("sweep must hit a wall");
    assert!(
        hit.distance < 1.0,
        "truncated sweep must stop at the nearest wall, got {}",
        hit.distance
    );
}

#[test]
fn exact_hit_count_reports_no_overflow_and_keeps_order() {
    let mut world = new_world(static_config());
    let near = query_static(&mut world, 0.4, [0.0, 0.0, 2.0]);
    let middle = query_static(&mut world, 0.4, [0.0, 0.0, 2.7]);
    let far = query_static(&mut world, 0.4, [0.0, 0.0, 3.4]);
    let exact = world.ray_query(
        [0.0, 0.0, 0.0],
        [0.0, 0.0, 1.0],
        20.0,
        &QueryFilter {
            max_hits: 3,
            ..QueryFilter::default()
        },
    );
    let capped = world.ray_query(
        [0.0, 0.0, 0.0],
        [0.0, 0.0, 1.0],
        20.0,
        &QueryFilter {
            max_hits: 2,
            ..QueryFilter::default()
        },
    );
    world.step(DT);
    world.wait();
    let hits = world.query_hits(exact);
    assert_eq!(
        hits.iter().map(|hit| hit.body).collect::<Vec<_>>(),
        vec![near, middle, far]
    );
    assert!(!world.query_overflow(exact));
    let hits = world.query_hits(capped);
    assert_eq!(
        hits.iter().map(|hit| hit.body).collect::<Vec<_>>(),
        vec![near, middle]
    );
    assert!(world.query_overflow(capped));
}

#[test]
fn queries_do_not_inherit_candidates_from_earlier_queries() {
    let mut world = new_world(static_config());
    let first_target = query_static(&mut world, 0.5, [0.0, 0.0, 2.0]);
    let _second = query_static(&mut world, 0.5, [0.0, 0.0, 4.0]);
    let _third = query_static(&mut world, 0.5, [0.0, 0.0, 6.0]);
    let far_target = query_static(&mut world, 0.5, [10.0, 0.0, 2.0]);
    let first = world.ray_query(
        [0.0, 0.0, 0.0],
        [0.0, 0.0, 1.0],
        20.0,
        &QueryFilter {
            max_hits: 4,
            ..QueryFilter::default()
        },
    );
    world.wait();
    assert_eq!(
        world.query_hit(first).map(|hit| hit.body),
        Some(first_target)
    );
    let second = world.ray_query(
        [10.0, 0.0, 0.0],
        [0.0, 0.0, 1.0],
        20.0,
        &QueryFilter {
            max_hits: 4,
            ..QueryFilter::default()
        },
    );
    world.wait();
    assert_eq!(
        world.query_hit(second).map(|hit| hit.body),
        Some(far_target),
        "a later query must not answer with an earlier query's candidates"
    );
}

#[test]
fn a_query_batch_that_fills_the_stream_keeps_every_result() {
    let mut world = new_world(static_config());
    let target = query_static(&mut world, 0.5, [0.0, 0.0, 2.0]);
    let count = dynamis_domain::STREAM_FLOOR;
    let handles = (0..count)
        .map(|_| {
            world.ray_query(
                [0.0, 0.0, 0.0],
                [0.0, 0.0, 1.0],
                20.0,
                &QueryFilter::default(),
            )
        })
        .collect::<Vec<_>>();
    world.step(DT);
    world.wait();
    assert!(
        handles.iter().all(|handle| world.query_ready(*handle)),
        "a batch as wide as the query stream must resolve completely"
    );
    assert!(
        handles
            .iter()
            .all(|handle| world.query_hit(*handle).map(|hit| hit.body) == Some(target)),
        "every query of a full batch must report the body it hits"
    );
}