crcbl-phys 0.12.0

Crucible physics: queries, forces, CCD
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
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
//! Collider shapes and bounding volumes for physics queries.
//!
//! Every collider shape can produce an axis-aligned bounding box ([`Aabb`]) for
//! broadphase culling. Shapes are parametric (no mesh data) — L0 covers spheres,
//! boxes, and capsules, which together handle ~90% of the engines collision
//! needs (projectiles, characters, vehicles, pickups).
//!
//! All spatial types use `f64` so queries are consistent with the server's
//! deterministic tick loop. Downcasting to `f32` happens only at the render
//! boundary (camera-relative transforms).

use crcbl_core::bounds::{max_lanes_d, min_lanes_d};
use glam::{DMat3, DQuat, DVec3};

// ---------------------------------------------------------------------------
// Aabb
// ---------------------------------------------------------------------------

/// An axis-aligned bounding box.
///
/// Defined by a minimum and maximum corner. An empty / degenerate box has
/// `min > max` on at least one axis; [`Aabb::EMPTY`] is the canonical empty
/// value and [`Aabb::is_empty`] checks for it.
#[derive(Debug, Clone, Copy, PartialEq)]
pub struct Aabb {
    /// Minimum corner (inclusive).
    pub min: DVec3,
    /// Maximum corner (inclusive).
    pub max: DVec3,
}

impl Aabb {
    /// The empty AABB: `min = +inf, max = -inf`. Union with any real AABB
    /// produces that real AABB.
    pub const EMPTY: Self = Self {
        min: DVec3::splat(f64::INFINITY),
        max: DVec3::splat(f64::NEG_INFINITY),
    };

    /// Create an AABB from explicit min / max bounds.
    ///
    /// The caller is responsible for ensuring `min ≤ max` per axis; a box that
    /// violates it reads as empty (see [`Aabb::is_empty`]).
    #[inline]
    #[must_use]
    pub fn new(min: DVec3, max: DVec3) -> Self {
        Self { min, max }
    }

    /// Build an AABB from a centre and half-extents.
    #[inline]
    #[must_use]
    pub fn from_centre_half(centre: DVec3, half: DVec3) -> Self {
        Self {
            min: centre - half,
            max: centre + half,
        }
    }

    /// The centre of this AABB.
    #[inline]
    #[must_use]
    pub fn centre(&self) -> DVec3 {
        (self.min + self.max) * 0.5
    }

    /// Extents on each axis (full width / height / depth).
    #[inline]
    #[must_use]
    pub fn extents(&self) -> DVec3 {
        self.max - self.min
    }

    /// Whether this AABB is degenerate: `min` is past `max` on at least one
    /// axis. A zero-extent (flat or point) box is *not* empty — it still
    /// intersects and bounds correctly.
    #[inline]
    #[must_use]
    pub fn is_empty(&self) -> bool {
        self.min.x > self.max.x || self.min.y > self.max.y || self.min.z > self.max.z
    }

    /// Half the true surface area of this box — `wh + hd + dw`.
    ///
    /// This is the cost term of the surface area heuristic the dynamic BVH
    /// picks insertion sites with. The factor of two the real surface area
    /// carries is dropped because every comparison the heuristic makes is
    /// between two of these, and a constant factor common to both sides cannot
    /// change which one is smaller.
    ///
    /// An empty box has no surface, so it costs `0` rather than the `NaN` that
    /// `(-inf) - (+inf)` arithmetic on [`Aabb::EMPTY`] would otherwise produce
    /// and propagate into every subsequent comparison.
    #[inline]
    #[must_use]
    pub(crate) fn half_surface_area(&self) -> f64 {
        if self.is_empty() {
            return 0.0;
        }
        let e = self.extents();
        e.x * e.y + e.y * e.z + e.z * e.x
    }

    /// Widen this AABB by a uniform margin on every side.
    #[inline]
    #[must_use]
    pub fn inflated(self, margin: f64) -> Self {
        Self {
            min: self.min - DVec3::splat(margin),
            max: self.max + DVec3::splat(margin),
        }
    }

    // -- set operations -------------------------------------------------------

    /// Union with another AABB, expanding to contain both.
    #[inline]
    #[must_use]
    pub fn union(self, other: Self) -> Self {
        if other.is_empty() {
            return self;
        }
        if self.is_empty() {
            return other;
        }
        // Not `DVec3::min`/`DVec3::max`: glam writes those as a bare `<` per
        // lane, which is false against a `NaN` and so yields the *incoming*
        // box, discarding the accumulator. `Bvh::build_rec` folds this over a
        // range, so one `NaN` body position followed by one finite body left a
        // parent node that came back finite and no longer contained its
        // children — and overlap and ray queries silently miss those pairs.
        // The `is_empty` guards above do not catch it either: `is_empty` is
        // `min.x > max.x`, and `>` is false on a `NaN`, so a poisoned box reads
        // as non-empty. See `crcbl_core::bounds`.
        Self {
            min: min_lanes_d(self.min, other.min),
            max: max_lanes_d(self.max, other.max),
        }
    }

    /// Whether `other` overlaps this AABB (touching counts as overlapping).
    #[inline]
    #[must_use]
    pub fn intersects(&self, other: &Self) -> bool {
        self.min.x <= other.max.x
            && self.max.x >= other.min.x
            && self.min.y <= other.max.y
            && self.max.y >= other.min.y
            && self.min.z <= other.max.z
            && self.max.z >= other.min.z
    }

    /// Whether `other` lies wholly inside this AABB (a shared face counts as
    /// inside).
    ///
    /// The contact broadphase's test for a proxy that has left its fattened
    /// bounds: a `NaN` anywhere in `other` answers `false`, so a poisoned body
    /// is re-inserted rather than kept on bounds that no longer describe it.
    #[inline]
    #[must_use]
    pub fn contains(&self, other: &Self) -> bool {
        self.min.x <= other.min.x
            && self.min.y <= other.min.y
            && self.min.z <= other.min.z
            && self.max.x >= other.max.x
            && self.max.y >= other.max.y
            && self.max.z >= other.max.z
    }

    /// The point of this box (a solid, so its interior counts) nearest to
    /// `point`: `point` itself when it is inside or on the surface, otherwise
    /// the point on the surface it is closest to.
    ///
    /// `None` when there is no answer to give: the box is empty (`min` past
    /// `max` on an axis), or `point` or either corner holds a `NaN`. Clamping
    /// against such a box would either panic in `f64::clamp` or hand back a
    /// `NaN` that reads as a position everywhere downstream.
    #[inline]
    #[must_use]
    pub fn closest_point(&self, point: DVec3) -> Option<DVec3> {
        // `cmple` is false on a `NaN` lane, so one test refuses both an
        // inverted box and a poisoned one — `is_empty` alone passes a `NaN`.
        if !self.min.cmple(self.max).all() || point.is_nan() {
            return None;
        }
        Some(point.clamp(self.min, self.max))
    }

    // -- ray intersection (slab method)---------------------------------------

    /// The parametric interval `(t_near, t_far)` over which a ray is inside
    /// this AABB, or `None` if the ray misses it entirely.
    ///
    /// This is the one slab test in the crate — the boolean overlap check, the
    /// BVH's entry-distance probe and the shape-level box queries all go
    /// through it. `inv_dir` is the component-wise reciprocal of the ray
    /// direction and `dir_is_neg` its per-axis sign; both are precomputed by
    /// the caller because a traversal reuses them across many boxes. Axes with
    /// a zero direction component (infinite reciprocal) are handled without
    /// producing `NaN` from `0.0 * INF`.
    ///
    /// The interval is unclamped: `t_near` is negative when the ray origin is
    /// inside the box. Choosing which end to report is the caller's job — see
    /// `crate::query`'s shared root-selection rule.
    #[must_use]
    pub(crate) fn ray_slab(
        &self,
        origin: DVec3,
        inv_dir: DVec3,
        dir_is_neg: [bool; 3],
    ) -> Option<(f64, f64)> {
        let origin = origin.to_array();
        let inv_dir = inv_dir.to_array();
        let min = self.min.to_array();
        let max = self.max.to_array();

        let mut t_near = f64::NEG_INFINITY;
        let mut t_far = f64::INFINITY;
        for axis in 0..3 {
            let (lo, hi) = if dir_is_neg[axis] {
                (max[axis], min[axis])
            } else {
                (min[axis], max[axis])
            };
            if inv_dir[axis].is_finite() {
                t_near = t_near.max((lo - origin[axis]) * inv_dir[axis]);
                t_far = t_far.min((hi - origin[axis]) * inv_dir[axis]);
            } else if origin[axis] < min[axis] || origin[axis] > max[axis] {
                // Parallel to this slab and outside it: no intersection.
                return None;
            }
        }

        if t_near > t_far {
            return None;
        }
        Some((t_near, t_far))
    }

    /// Test whether a ray intersects this AABB, using the pre-computed
    /// inverse direction and sign flags for efficiency.
    ///
    /// `t_min` and `t_max` bound the ray segment (typically `(0, +inf)` for
    /// unbounded rays, or `(0, length)` for segments).
    #[inline]
    #[must_use]
    pub fn intersect_ray(
        &self,
        origin: DVec3,
        inv_dir: DVec3,
        dir_is_neg: [bool; 3],
        t_min: f64,
        t_max: f64,
    ) -> bool {
        self.ray_slab(origin, inv_dir, dir_is_neg)
            .is_some_and(|(t_near, t_far)| t_near <= t_max && t_far >= t_min)
    }
}

// ---------------------------------------------------------------------------
// Sphere
// ---------------------------------------------------------------------------

/// A sphere collider.
#[derive(Debug, Clone, Copy, PartialEq)]
pub struct Sphere {
    /// Centre in local simulation space.
    pub centre: DVec3,
    /// Radius, in metres.
    pub radius: f64,
}

impl Sphere {
    /// Create a sphere.
    #[inline]
    #[must_use]
    pub fn new(centre: DVec3, radius: f64) -> Self {
        debug_assert!(radius >= 0.0, "sphere radius must be non-negative");
        Self { centre, radius }
    }

    /// AABB of this sphere.
    #[inline]
    #[must_use]
    pub fn aabb(&self) -> Aabb {
        let r = DVec3::splat(self.radius);
        Aabb {
            min: self.centre - r,
            max: self.centre + r,
        }
    }
}

// ---------------------------------------------------------------------------
// BoxCollider
// ---------------------------------------------------------------------------

/// A box collider: a centre, half extents along the box's own axes, and how
/// those axes are turned from the world's.
///
/// [`BoxCollider::new`] makes an unturned box, whose axes are the world's, and
/// [`BoxCollider::with_rotation`] turns one. The query world answers rays,
/// sweeps and overlaps against the turned box itself, not the world-axis box
/// around it — see [`crate::query::ray_vs_box`] and its siblings — and its
/// broadphase holds that world-axis box, [`aabb`](Self::aabb).
#[derive(Debug, Clone, Copy, PartialEq)]
pub struct BoxCollider {
    /// Centre in local simulation space.
    pub centre: DVec3,
    /// Half-width along each of the box's own axes, in metres.
    pub half_extents: DVec3,
    /// How the box's axes are turned from the world's: a unit quaternion,
    /// [`DQuat::IDENTITY`] for a box whose axes are the world's.
    pub rotation: DQuat,
}

impl BoxCollider {
    /// Create an unturned box collider: its axes are the world's.
    ///
    /// # Panics
    ///
    /// Panics in debug builds if any half-extent is negative.
    #[inline]
    #[must_use]
    pub fn new(centre: DVec3, half_extents: DVec3) -> Self {
        debug_assert!(
            half_extents.x >= 0.0 && half_extents.y >= 0.0 && half_extents.z >= 0.0,
            "box half-extents must be non-negative"
        );
        Self {
            centre,
            half_extents,
            rotation: DQuat::IDENTITY,
        }
    }

    /// This box turned by `rotation` about its centre, which must be a unit
    /// quaternion.
    #[inline]
    #[must_use]
    pub const fn with_rotation(self, rotation: DQuat) -> Self {
        Self { rotation, ..self }
    }

    /// The smallest world-axis box holding this one: the box itself for an
    /// unturned collider, and for a turned one its half extents projected onto
    /// each world axis (the absolute rotation matrix times the extents,
    /// Ericson, _Real-Time Collision Detection_ §4.2.6).
    ///
    /// Exactly `centre ± half_extents` when unturned, an infinite extent
    /// included, which the projection would turn into `0 · ∞ = NaN`.
    #[inline]
    #[must_use]
    pub fn aabb(&self) -> Aabb {
        if !self.is_turned() {
            return Aabb {
                min: self.centre - self.half_extents,
                max: self.centre + self.half_extents,
            };
        }
        let turned = DMat3::from_quat(self.rotation);
        let reach = DVec3::new(
            turned.row(0).abs().dot(self.half_extents),
            turned.row(1).abs().dot(self.half_extents),
            turned.row(2).abs().dot(self.half_extents),
        );
        Aabb {
            min: self.centre - reach,
            max: self.centre + reach,
        }
    }

    /// Whether the box's axes are not the world's. An unturned box is answered
    /// by the world-axis queries directly, so its answers are the ones it had
    /// before boxes could turn, to the bit.
    #[inline]
    #[must_use]
    pub fn is_turned(&self) -> bool {
        self.rotation != DQuat::IDENTITY
    }

    /// The box at the origin with its own axes, as a world-axis box: what a
    /// query moved into the box's frame ([`local_point`](Self::local_point)) is
    /// tested against.
    #[inline]
    #[must_use]
    pub(crate) fn local_aabb(&self) -> Aabb {
        Aabb {
            min: -self.half_extents,
            max: self.half_extents,
        }
    }

    /// `point` in the box's own frame: relative to its centre, turned back by
    /// its rotation.
    #[inline]
    #[must_use]
    pub(crate) fn local_point(&self, point: DVec3) -> DVec3 {
        self.rotation.inverse() * (point - self.centre)
    }

    /// `local`, a point in the box's own frame, back in the world.
    #[inline]
    #[must_use]
    pub(crate) fn world_point(&self, local: DVec3) -> DVec3 {
        self.centre + self.rotation * local
    }
}

// ---------------------------------------------------------------------------
// Capsule
// ---------------------------------------------------------------------------

/// A capsule collider: a cylinder with hemispherical caps, aligned to the Y
/// axis in local space.
///
/// A capsule is defined by a centre point, a radius, and a half-height
/// (half the length of the cylindrical section). The total length is
/// `2 * half_height + 2 * radius`.
#[derive(Debug, Clone, Copy, PartialEq)]
pub struct Capsule {
    /// Centre of the cylindrical section (midpoint between the two hemisphere
    /// centres).
    pub centre: DVec3,
    /// Radius of the capsule (both the cylinder and the hemispherical caps).
    pub radius: f64,
    /// Half the length of the cylindrical section, not including caps.
    pub half_height: f64,
}

impl Capsule {
    /// Create a capsule.
    ///
    /// # Panics
    ///
    /// Panics in debug builds if radius or half-height is negative.
    #[inline]
    #[must_use]
    pub fn new(centre: DVec3, radius: f64, half_height: f64) -> Self {
        debug_assert!(radius >= 0.0, "capsule radius must be non-negative");
        debug_assert!(
            half_height >= 0.0,
            "capsule half-height must be non-negative"
        );
        Self {
            centre,
            radius,
            half_height,
        }
    }

    /// AABB of this capsule (the capsule is always Y-aligned).
    #[inline]
    #[must_use]
    pub fn aabb(&self) -> Aabb {
        let total_half = DVec3::new(self.radius, self.half_height + self.radius, self.radius);
        Aabb {
            min: self.centre - total_half,
            max: self.centre + total_half,
        }
    }

    /// The top hemisphere centre.
    #[inline]
    #[must_use]
    pub fn top(&self) -> DVec3 {
        DVec3::new(
            self.centre.x,
            self.centre.y + self.half_height,
            self.centre.z,
        )
    }

    /// The bottom hemisphere centre.
    #[inline]
    #[must_use]
    pub fn bottom(&self) -> DVec3 {
        DVec3::new(
            self.centre.x,
            self.centre.y - self.half_height,
            self.centre.z,
        )
    }
}

// ---------------------------------------------------------------------------
// LyingCapsule
// ---------------------------------------------------------------------------

/// A capsule lying on its side: a prone body, its core running from the head
/// back to the feet, level or pitched to follow the ground.
///
/// A query shape, not a collider, for asking whether a body lying at a pose
/// would be inside anything, see [`crate::PhysicsWorld::lying_capsule_blocker`],
/// and for moving one, see [`crate::CharacterController::move_lying`].
///
/// # The pose
///
/// - [`head`](Self::head) is the centre of the hemisphere at the head end.
/// - [`yaw`](Self::yaw) is the way the head faces, in radians: a right-handed
///   turn about `+Y`, which is up, from `-Z`. Zero faces `-Z`, a quarter turn
///   faces `-X`, a half turn `+Z` and three quarters `+X` — `-Z` turned by the
///   rotation [`crate::rotation_from_scaled_axis`] builds from `+Y · yaw`, and
///   the `ahead` of `OrbitCamera::walk_direction` in `crcbl-render`.
/// - [`pitch_sine`](Self::pitch_sine) tilts the core out of the horizontal,
///   about the head: it is the **sine** of the angle the core makes with the
///   horizontal, **positive when the head end is higher** than the feet end —
///   a body crawling head first up a slope has a positive pitch. Zero, which
///   [`new`](Self::new) gives, lies level.
/// - [`length`](Self::length) is how far behind the head the feet end's
///   hemisphere centre lies, along the core, so the body reaches a further
///   [`radius`](Self::radius) past each of the two ends.
///
/// The pitch is a sine and not an angle for the reason
/// [`CharacterConfig::min_ground_normal_y`](crate::CharacterConfig::min_ground_normal_y)
/// is a cosine: this crate constructs no inverse trigonometry, and a pitch
/// found from the ground comes out of a square root. [`axis`](Self::axis) is
/// the direction it gives the core, which is what a renderer orients the body
/// model by.
///
/// A length of zero is a sphere at `head`, which is what a character
/// controller with a `half_height` of zero already is.
#[derive(Debug, Clone, Copy, PartialEq)]
pub struct LyingCapsule {
    /// Centre of the head end's hemisphere.
    pub head: DVec3,
    /// The way the head faces, in radians about `+Y` from `-Z`.
    pub yaw: f64,
    /// The sine of the core's angle above the horizontal, positive with the
    /// head end higher: in `[-1, 1]`.
    pub pitch_sine: f64,
    /// Radius of the capsule.
    pub radius: f64,
    /// Distance from the head end's hemisphere centre back to the feet end's,
    /// not including the caps.
    pub length: f64,
}

impl LyingCapsule {
    /// A capsule lying level at a pose; see the type for what each part means.
    ///
    /// # Panics
    ///
    /// Panics in debug builds if radius or length is negative.
    #[inline]
    #[must_use]
    pub fn new(head: DVec3, yaw: f64, radius: f64, length: f64) -> Self {
        debug_assert!(radius >= 0.0, "capsule radius must be non-negative");
        debug_assert!(length >= 0.0, "capsule length must be non-negative");
        Self {
            head,
            yaw,
            pitch_sine: 0.0,
            radius,
            length,
        }
    }

    /// The same capsule pitched about its head: see
    /// [`pitch_sine`](Self::pitch_sine).
    ///
    /// # Panics
    ///
    /// Panics in debug builds if `pitch_sine` is outside `[-1, 1]`.
    #[inline]
    #[must_use]
    pub fn with_pitch_sine(self, pitch_sine: f64) -> Self {
        debug_assert!(
            (-1.0..=1.0).contains(&pitch_sine),
            "a pitch sine lies in [-1, 1], not {pitch_sine}"
        );
        Self { pitch_sine, ..self }
    }

    /// The unit horizontal direction the head faces, which the
    /// [`yaw`](Self::yaw) alone decides: from the feet toward the head, seen
    /// from above.
    #[inline]
    #[must_use]
    pub fn facing(&self) -> DVec3 {
        DVec3::new(
            -crcbl_core::trig::sin(self.yaw),
            0.0,
            -crcbl_core::trig::cos(self.yaw),
        )
    }

    /// The unit direction of the core from the feet end toward the head end:
    /// the [`facing`](Self::facing), tilted up by the
    /// [`pitch_sine`](Self::pitch_sine). Level, it is the facing itself.
    #[inline]
    #[must_use]
    pub fn axis(&self) -> DVec3 {
        let facing = self.facing();
        let level = (1.0 - self.pitch_sine * self.pitch_sine).max(0.0).sqrt();
        DVec3::new(facing.x * level, self.pitch_sine, facing.z * level)
    }

    /// Centre of the feet end's hemisphere: [`length`](Self::length) behind
    /// the head along the [`axis`](Self::axis).
    #[inline]
    #[must_use]
    pub fn feet(&self) -> DVec3 {
        self.head - self.axis() * self.length
    }

    /// AABB of this capsule.
    #[inline]
    #[must_use]
    pub fn aabb(&self) -> Aabb {
        let feet = self.feet();
        let r = DVec3::splat(self.radius);
        Aabb {
            min: self.head.min(feet) - r,
            max: self.head.max(feet) + r,
        }
    }
}

// ---------------------------------------------------------------------------
// Tests
// ---------------------------------------------------------------------------

#[cfg(test)]
mod tests {
    use super::*;

    // -- Aabb ----------------------------------------------------------------

    /// **A `NaN` box does not eat the union it is folded into.**
    ///
    /// `Bvh::build_rec` folds `union` over a range of items, so this is the
    /// shape that matters rather than the pairwise call. glam's `DVec3::min` is
    /// a bare `<` per lane, false against a `NaN`, so it used to yield the
    /// incoming box and throw the accumulator away — and then the *next* finite
    /// box threw the `NaN` away, leaving a node that was finite and no longer
    /// contained its children. A parent that does not contain its children is a
    /// BVH that silently misses overlaps.
    ///
    /// The `is_empty` early-outs do not save it: `is_empty` is `min.x > max.x`,
    /// and every comparison against a `NaN` is false, so a poisoned box reads as
    /// non-empty and goes down the folding path.
    #[test]
    fn a_nan_box_does_not_shrink_the_union_it_is_folded_into() {
        let low = Aabb::from_centre_half(DVec3::splat(-2.0), DVec3::splat(1.0));
        let high = Aabb::from_centre_half(DVec3::splat(2.0), DVec3::splat(1.0));
        let nan = Aabb {
            min: DVec3::new(f64::NAN, 0.0, 0.0),
            max: DVec3::new(f64::NAN, 0.0, 0.0),
        };

        assert!(
            !nan.is_empty(),
            "a NaN box reads as non-empty, which is why the early-outs above do not catch it"
        );

        // The damaging order: a finite box after the NaN restores finiteness
        // while the extent gathered before it would be gone.
        let folded = [low, high, nan, low]
            .into_iter()
            .fold(Aabb::EMPTY, Aabb::union);

        assert!(
            folded.min.x <= low.min.x && folded.max.x >= high.max.x,
            "the union came back as {:?}..{:?}, which does not contain the boxes it was folded \
             from — a BVH node like this misses every pair in the part it dropped",
            folded.min,
            folded.max
        );
    }

    #[test]
    fn empty_aabb_is_empty() {
        assert!(Aabb::EMPTY.is_empty());
    }

    #[test]
    fn point_aabb_is_not_empty() {
        let aabb = Aabb::new(DVec3::splat(1.0), DVec3::splat(1.0));
        assert!(!aabb.is_empty());
        assert_eq!(aabb.min, DVec3::splat(1.0));
        assert_eq!(aabb.max, DVec3::splat(1.0));
    }

    #[test]
    fn union_with_empty_is_identity() {
        let a = Aabb::from_centre_half(DVec3::ZERO, DVec3::splat(1.0));
        assert_eq!(a.union(Aabb::EMPTY), a);
        assert_eq!(Aabb::EMPTY.union(a), a);
    }

    #[test]
    fn union_expands_to_contain_both() {
        let a = Aabb::from_centre_half(DVec3::new(-2.0, 0.0, 0.0), DVec3::splat(1.0));
        let b = Aabb::from_centre_half(DVec3::new(2.0, 0.0, 0.0), DVec3::splat(1.0));
        let u = a.union(b);
        assert_eq!(u.min.x, -3.0);
        assert_eq!(u.max.x, 3.0);
    }

    #[test]
    fn two_overlapping_aabbs_report_that_they_intersect() {
        let a = Aabb::from_centre_half(DVec3::ZERO, DVec3::splat(2.0));
        let b = Aabb::from_centre_half(DVec3::new(1.0, 0.0, 0.0), DVec3::splat(2.0));
        assert!(a.intersects(&b));
    }

    #[test]
    fn aabb_does_not_intersect_separated() {
        let a = Aabb::from_centre_half(DVec3::ZERO, DVec3::splat(1.0));
        let b = Aabb::from_centre_half(DVec3::new(5.0, 0.0, 0.0), DVec3::splat(1.0));
        assert!(!a.intersects(&b));
    }

    #[test]
    fn a_ray_aimed_at_a_box_passes_the_slab_test() {
        let aabb = Aabb::from_centre_half(DVec3::ZERO, DVec3::splat(1.0));
        let origin = DVec3::new(-5.0, 0.0, 0.0);
        let dir = DVec3::new(1.0, 0.0, 0.0);
        let inv_dir = dir.recip();
        let neg = [dir.x < 0.0, dir.y < 0.0, dir.z < 0.0];
        assert!(aabb.intersect_ray(origin, inv_dir, neg, 0.0, f64::INFINITY));
    }

    #[test]
    fn a_ray_passing_above_a_box_fails_the_slab_test() {
        let aabb = Aabb::from_centre_half(DVec3::ZERO, DVec3::splat(1.0));
        let origin = DVec3::new(-5.0, 10.0, 0.0);
        let dir = DVec3::new(1.0, 0.0, 0.0);
        let inv_dir = dir.recip();
        let neg = [dir.x < 0.0, dir.y < 0.0, dir.z < 0.0];
        assert!(!aabb.intersect_ray(origin, inv_dir, neg, 0.0, f64::INFINITY));
    }

    #[test]
    fn ray_hits_from_inside() {
        let aabb = Aabb::from_centre_half(DVec3::ZERO, DVec3::splat(1.0));
        let origin = DVec3::ZERO;
        let dir = DVec3::new(1.0, 0.0, 0.0);
        let inv_dir = dir.recip();
        let neg = [dir.x < 0.0, dir.y < 0.0, dir.z < 0.0];
        assert!(aabb.intersect_ray(origin, inv_dir, neg, 0.0, f64::INFINITY));
    }

    #[test]
    fn ray_on_face_with_zero_dir_component_hits() {
        // Ray starts on +Y face with dir having zero Y component.
        // Old code: (loy - origin.y) * inv_dir.y = (1.0 - 1.0) * INF = 0.0 * INF = NaN.
        let aabb = Aabb::from_centre_half(DVec3::ZERO, DVec3::splat(1.0));
        let origin = DVec3::new(0.0, 1.0, 0.0);
        let dir = DVec3::new(1.0, 0.0, 0.0);
        let inv_dir = dir.recip();
        let neg = [dir.x < 0.0, dir.y < 0.0, dir.z < 0.0];
        assert!(aabb.intersect_ray(origin, inv_dir, neg, 0.0, f64::INFINITY));
    }

    #[test]
    fn ray_on_face_with_zero_dir_component_parallel_miss() {
        // Ray outside AABB on Y, with zero Y direction → parallel miss.
        let aabb = Aabb::from_centre_half(DVec3::ZERO, DVec3::splat(1.0));
        let origin = DVec3::new(0.0, 5.0, 0.0);
        let dir = DVec3::new(1.0, 0.0, 0.0);
        let inv_dir = dir.recip();
        let neg = [dir.x < 0.0, dir.y < 0.0, dir.z < 0.0];
        assert!(!aabb.intersect_ray(origin, inv_dir, neg, 0.0, f64::INFINITY));
    }

    #[test]
    fn ray_slab_reports_entry_and_exit() {
        let aabb = Aabb::from_centre_half(DVec3::ZERO, DVec3::splat(1.0));
        let dir = DVec3::X;
        let neg = [dir.x < 0.0, dir.y < 0.0, dir.z < 0.0];
        // Origin inside: entry is behind the origin, exit ahead of it.
        let (near, far) = aabb.ray_slab(DVec3::ZERO, dir.recip(), neg).unwrap();
        assert!((near + 1.0).abs() < 1e-12);
        assert!((far - 1.0).abs() < 1e-12);
    }

    #[test]
    fn the_closest_point_of_a_box_is_on_its_surface_from_outside() {
        let aabb = Aabb::new(DVec3::ZERO, DVec3::new(1.0, 2.0, 3.0));
        // Past a face: only that axis moves.
        assert_eq!(
            aabb.closest_point(DVec3::new(0.5, 5.0, 1.0)),
            Some(DVec3::new(0.5, 2.0, 1.0))
        );
        // Past a corner: every axis clamps.
        assert_eq!(
            aabb.closest_point(DVec3::new(-4.0, -1.0, 9.0)),
            Some(DVec3::new(0.0, 0.0, 3.0))
        );
    }

    #[test]
    fn the_closest_point_of_a_box_to_a_point_inside_it_is_that_point() {
        let aabb = Aabb::new(DVec3::ZERO, DVec3::new(1.0, 2.0, 3.0));
        let inside = DVec3::new(0.25, 1.5, 2.75);
        assert_eq!(aabb.closest_point(inside), Some(inside));
        // A flat box is a real box: its face is its interior.
        let flat = Aabb::new(DVec3::ZERO, DVec3::new(1.0, 0.0, 1.0));
        assert_eq!(
            flat.closest_point(DVec3::new(0.5, 3.0, 0.5)),
            Some(DVec3::new(0.5, 0.0, 0.5))
        );
    }

    #[test]
    fn an_empty_or_nan_box_or_a_nan_point_has_no_closest_point() {
        let unit = Aabb::new(DVec3::ZERO, DVec3::ONE);
        assert_eq!(Aabb::EMPTY.closest_point(DVec3::ZERO), None);
        assert_eq!(
            Aabb::new(DVec3::ONE, DVec3::ZERO).closest_point(DVec3::ZERO),
            None
        );
        let poisoned = Aabb::new(DVec3::new(f64::NAN, 0.0, 0.0), DVec3::ONE);
        assert!(
            !poisoned.is_empty(),
            "a NaN box reads as non-empty, so is_empty alone would not refuse it"
        );
        assert_eq!(poisoned.closest_point(DVec3::ZERO), None);
        assert_eq!(unit.closest_point(DVec3::new(0.0, f64::NAN, 0.0)), None);
    }

    #[test]
    fn inflated_aabb_is_larger() {
        let aabb = Aabb::from_centre_half(DVec3::ZERO, DVec3::splat(1.0));
        let inflated = aabb.inflated(0.5);
        assert_eq!(inflated.min, DVec3::splat(-1.5));
        assert_eq!(inflated.max, DVec3::splat(1.5));
    }

    #[test]
    fn from_centre_half_roundtrips() {
        let aabb = Aabb::from_centre_half(DVec3::new(1.0, 2.0, 3.0), DVec3::new(0.5, 1.0, 1.5));
        assert_eq!(aabb.centre(), DVec3::new(1.0, 2.0, 3.0));
        assert_eq!(aabb.extents(), DVec3::new(1.0, 2.0, 3.0));
    }

    // -- Sphere --------------------------------------------------------------

    #[test]
    fn a_spheres_bounds_are_its_centre_expanded_by_its_radius() {
        let s = Sphere::new(DVec3::new(1.0, 2.0, 3.0), 0.5);
        let aabb = s.aabb();
        assert_eq!(aabb.min, DVec3::new(0.5, 1.5, 2.5));
        assert_eq!(aabb.max, DVec3::new(1.5, 2.5, 3.5));
    }

    // -- BoxCollider ---------------------------------------------------------

    #[test]
    fn a_box_colliders_bounds_are_its_centre_expanded_by_its_half_extents() {
        let b = BoxCollider::new(DVec3::ZERO, DVec3::splat(2.0));
        let aabb = b.aabb();
        assert_eq!(aabb.min, DVec3::splat(-2.0));
        assert_eq!(aabb.max, DVec3::splat(2.0));
    }

    // -- Capsule -------------------------------------------------------------

    #[test]
    fn a_capsules_bounds_reach_a_radius_past_each_cap_and_a_radius_sideways() {
        let c = Capsule::new(DVec3::ZERO, 0.5, 1.0);
        let aabb = c.aabb();
        assert_eq!(aabb.min, DVec3::new(-0.5, -1.5, -0.5));
        assert_eq!(aabb.max, DVec3::new(0.5, 1.5, 0.5));
    }

    #[test]
    fn a_capsules_end_points_exclude_the_radius_its_caps_add() {
        let c = Capsule::new(DVec3::new(0.0, 5.0, 0.0), 0.5, 2.0);
        assert_eq!(c.top(), DVec3::new(0.0, 7.0, 0.0));
        assert_eq!(c.bottom(), DVec3::new(0.0, 3.0, 0.0));
    }

    // -- LyingCapsule --------------------------------------------------------

    /// **The yaw convention, written out**: zero faces `-Z`, and each quarter
    /// turn carries the facing a right-handed quarter turn about `+Y` — to
    /// `-X`, `+Z`, then `+X` — with the feet the length behind the head.
    #[test]
    fn a_lying_capsules_feet_lie_behind_its_head_at_each_quarter_turn() {
        use std::f64::consts::{FRAC_PI_2, PI};
        let head = DVec3::new(1.0, 2.0, 3.0);
        for (yaw, facing) in [
            (0.0, DVec3::NEG_Z),
            (FRAC_PI_2, DVec3::NEG_X),
            (PI, DVec3::Z),
            (3.0 * FRAC_PI_2, DVec3::X),
        ] {
            let capsule = LyingCapsule::new(head, yaw, 0.3, 1.6);
            assert!(
                (capsule.facing() - facing).length() < 1e-12,
                "yaw {yaw} faces {:?}, not {facing:?}",
                capsule.facing()
            );
            let feet = head - facing * 1.6;
            assert!(
                (capsule.feet() - feet).length() < 1e-12,
                "yaw {yaw} puts the feet at {:?}, not {feet:?}",
                capsule.feet()
            );
        }
    }

    /// **The facing is `-Z` turned by the engine's own rotation about `+Y`**,
    /// at angles that are not quarter turns too — the claim the type's
    /// documentation makes about `rotation_from_scaled_axis`.
    #[test]
    fn a_lying_capsule_faces_where_the_engines_yaw_rotation_turns_minus_z() {
        for yaw in [-2.5, -0.7, 0.3, 1.1, 4.0] {
            let turned = crate::rotation_from_scaled_axis(DVec3::Y * yaw) * DVec3::NEG_Z;
            let facing = LyingCapsule::new(DVec3::ZERO, yaw, 0.3, 1.6).facing();
            assert!(
                (facing - turned).length() < 1e-12,
                "yaw {yaw}: {facing:?} against {turned:?}"
            );
        }
    }

    #[test]
    fn a_lying_capsules_bounds_reach_a_radius_past_its_head_and_feet() {
        let c = LyingCapsule::new(DVec3::new(0.0, 0.5, 0.0), 0.0, 0.5, 1.5);
        let aabb = c.aabb();
        assert!((aabb.min - DVec3::new(-0.5, 0.0, -0.5)).length() < 1e-12);
        assert!((aabb.max - DVec3::new(0.5, 1.0, 2.0)).length() < 1e-12);
    }

    /// **The pitch convention, written out**: a positive pitch sine puts the
    /// head end higher than the feet, the feet stay the length behind the head
    /// along the core, and a pitch of zero is the level capsule to the bit.
    #[test]
    fn a_positive_pitch_puts_the_head_above_the_feet_a_length_along_the_core() {
        let head = DVec3::new(1.0, 2.0, 3.0);
        let level = LyingCapsule::new(head, 0.0, 0.3, 1.6);
        assert_eq!(level.with_pitch_sine(0.0).feet(), level.feet());
        assert_eq!(level.feet(), head + DVec3::Z * 1.6);

        let raised = level.with_pitch_sine(0.6);
        let feet = raised.feet();
        assert!(
            (feet - (head + DVec3::new(0.0, -0.6 * 1.6, 0.8 * 1.6))).length() < 1e-12,
            "head up by a pitch sine of 0.6 puts the feet at {feet:?}"
        );
        assert!(((head - feet).length() - 1.6).abs() < 1e-12);
        assert!((raised.axis().length() - 1.0).abs() < 1e-12);

        let lowered = level.with_pitch_sine(-0.6);
        assert!(
            lowered.feet().y > head.y,
            "a negative pitch raises the feet"
        );
        assert_eq!(lowered.facing(), raised.facing(), "the yaw alone faces");
    }
}