crcbl-phys 0.8.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
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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
//! Contacts and joints: contact-solver rungs 1 to 5, recorded in
//! `docs/notes/simulation.md`.
//!
//! ```text
//!   PhysicsSystem::step(dt), in a system built with contacts
//!
//!   broadphase ─▶ narrow phase ─▶ islands ─▶ forces ─▶ solver ×substeps ─▶ sweep ─▶ sleep
//!   split trees   one manifold    wake what            integrate velocities  fast     timers,
//!   move buffer   per pair,       was touched,         warm start            bodies   split one
//!   pair set      feature ids     merge what           solve (soft, biased)  and      island,
//!                 matched to      began touching       integrate positions   bullets  sleep the
//!                 last tick's                          relax (rigid)         to their still ones
//!                 impulses                             then restitution      impacts
//! ```
//!
//! Collision runs **once a tick** and the solver runs
//! [`ContactSettings::substeps`] substeps, each with one biased and one relaxed
//! pass, warm-started from the tick before: the Soft Step of decision 1.
//! Restitution is its own pass after the substeps. See `broadphase.rs` in this
//! directory for the pairs, [`manifold`] for the shapes, `solver.rs` for the
//! arithmetic and `island.rs` for islands and sleep.
//!
//! # Opt in, per system
//!
//! A [`crate::PhysicsSystem`] made with [`crate::PhysicsSystem::new`] has no
//! contacts and steps exactly as it did before rung 1: one integration of `dt`
//! a call. Contacts are
//! [`crate::PhysicsSystem::with_contacts`]'s, because every sample built before
//! them — horde's ten thousand kinematic sprites, breakout's paddle — has
//! colliders that were never meant to push each other, and paying a
//! broadphase for them would be a cost those samples never asked for.
//!
//! # What rung 2 added
//!
//! Box against box, through a separating axis test whose last axis each
//! contact caches, with clipping, four-point reduction and flip-invariant
//! feature ids — see [`manifold`]. And friction at each manifold's centroid
//! with a twist term about its normal, in place of friction at every point —
//! see `solver.rs`.
//!
//! # What rung 3 added
//!
//! Persistent islands of dynamic bodies joined by touching contacts, merged as
//! contacts begin and split lazily, and **sleep**: an island all of whose
//! bodies have moved slower than [`ContactSettings::sleep_speed`] and turned
//! slower than [`ContactSettings::sleep_angular_speed`] for
//! [`ContactSettings::time_to_sleep`] leaves the awake set, and costs the
//! step, the broadphase update and the solver nothing until it wakes. A
//! sleeping island wakes when
//!
//! - a contact with one of its bodies begins touching a body that moves: an
//!   awake dynamic body, or a kinematic one with a velocity;
//! - one of its bodies is given a force, a torque, a velocity or a whole new
//!   body — [`crate::PhysicsSystem::apply_force`],
//!   [`crate::PhysicsSystem::apply_torque`],
//!   [`crate::PhysicsSystem::body_mut`], [`crate::PhysicsSystem::set_body`];
//! - one of its bodies, or a body touching one, is teleported with
//!   [`crate::PhysicsSystem::set_transform`], given a new collider or
//!   material, or loses its collider or its entity — a support taken away;
//! - a body that does not move is placed or created overlapping one of its
//!   bodies, which a teleported or new static collider is.
//!
//! A query wakes nothing, and neither does reading a body or a transform. See
//! `island.rs`.
//!
//! # What rung 4 added
//!
//! **Continuous collision**, after the solve: every awake dynamic body that
//! went far for its size this tick is swept along its path against the static
//! bodies and planes, and every [`crate::RigidBody::bullet`] that moved at
//! all against every other body as well; one that meets something is put
//! where it met it, and the rest of its tick is dropped. Time of impact is
//! conservative advancement with the body's turning bounded, so a spinning
//! corner is caught as well as a fast centre. A sleeping body is never
//! swept. [`ContactSettings::continuous`] turns it off, and
//! [`ContactCounters`] counts the bodies swept, the times of impact computed,
//! the bodies stopped and the time dropped. See `sweep.rs`.
//!
//! # Compounds
//!
//! A [`crate::ColliderComponent::Compound`] body is several boxes. **Each part
//! is a broadphase proxy of its own**, as each shape of a Box2D v3 body is:
//! the owner of a proxy is a body and a part, two parts of one body never
//! pair, and every contact is between two parts — so a compound resting on
//! the floor on two parts has two contacts with it, each a box manifold of up
//! to four points. Everything downstream is unchanged by that: a contact's
//! cached separating axis, its feature ids and its warm-start impulses are
//! the part pair's, and persist exactly as a lone box's do, because the pair
//! is the contact; islands, sleep and the wake rules work on bodies, which
//! own every part's contacts. The alternative — one proxy per body and a
//! narrow phase that walks part pairs, with feature ids widened by the part
//! indices — would put a part-against-part cull in every tick that the
//! broadphase's fat bounds and pair set already do once, and make a contact
//! hold several manifolds with different normals. The cost of the choice is
//! a proxy per part, which [`crate::CompoundShape::MAX_PARTS`] bounds, and
//! with it the points a pair of bodies can put in the solver: four per
//! touching pair of parts. One [`KineticContact`] is raised per contact, so a
//! compound landing flat on two parts raises two.
//!
//! # Meshes: rung 5's static triangle mesh
//!
//! A [`crate::ColliderComponent::Mesh`] on a static or kinematic body is
//! collided **triangle by triangle, each a broadphase proxy**, for the reasons
//! a compound's parts are: a contact is one body against one triangle, with
//! that triangle's manifold, ids and warm start, and the mesh itself joins no
//! island. A triangle is one-sided, and a contact on one of its inactive edges
//! pushes along its normal, so a body slides across a seam without catching;
//! the sweeps measure their gap to each triangle too. See
//! [`crate::TriangleMesh`] and [`manifold`].
//!
//! # Joints: the rest of rung 5
//!
//! A [`crate::Joint`] is a constraint in the same Soft Step: prepared with the
//! contacts, warm-started and solved **before them** in every substep, soft in
//! the biased solve and rigid in the relax, and its bodies joined into one
//! island, which a new joint wakes. Distance, revolute, prismatic, weld and
//! spherical joints are Box3D's, with its limits, motors and springs; a joint
//! breaks at a force or torque threshold and is reported. See `joint/mod.rs`.
//!
//! **Extra substeps per group**, decision 1's: a body can ask for more
//! substeps with [`crate::PhysicsSystem::set_substeps`], and every body joined
//! to it this tick, by a contact or a joint, runs them — in a pass of their
//! own, before the rest, the rest of the system paying nothing. See
//! `group.rs`.
//!
//! # What is not done yet
//!
//! Plan L3's six-degree-of-freedom joint with a lock, limit and motor per
//! axis, and a spring on the spherical joint. General convex hulls, and GJK
//! for spheres and capsules against them: the collider set has no hull, and
//! against a box the analytic pairs are exact. A body over a mesh has a
//! contact per triangle it is near, with no reduction across them. Two
//! dynamic bodies that are not bullets are never swept against each other,
//! so a spinning cube can still turn a corner into a ball. The solver is
//! scalar `f64` (rung 6 makes it wide).

pub(crate) mod broadphase;
mod group;
pub(crate) mod ignored;
pub(crate) mod island;
pub(crate) mod joint;
pub mod manifold;
pub mod shape;
pub(crate) mod solver;
pub(crate) mod sweep;

use std::hash::Hasher;

use crcbl_core::Pool;
use crcbl_ecs::Entity;
use glam::DVec3;

use self::broadphase::{Broadphase, PlaneBounds, ProxyId, ProxyKind};
use self::island::{IslandId, Islands};
use self::manifold::{MAX_POINTS, Manifold, SatCache};
use self::shape::ContactShape;
use crate::collider::Aabb;
use crate::material::SurfaceMaterial;
use crate::system::{AwakeSet, BodyId, BodyRecord, BodySet, StaticSet, canonical_bits};

/// Whether the collision filters of bodies `a` and `b` let them touch; a body
/// with no record keeps the default and collides.
fn filters_collide(records: &Pool<BodyRecord>, a: BodyId, b: BodyId) -> bool {
    let filter = |id: BodyId| {
        records
            .get(id)
            .map_or(crate::CollisionFilter::DEFAULT, |record| record.filter)
    };
    filter(a).collides_with(filter(b))
}

/// The contact pipeline's knobs, with the values
/// contact-solver decision 1 and Box2D v3 settled on as
/// defaults.
#[derive(Clone, Copy, Debug, PartialEq)]
pub struct ContactSettings {
    /// Solver substeps a tick. Collision runs once whatever this is.
    pub substeps: u32,
    /// How stiff a contact is, as the frequency of the spring it behaves as,
    /// in hertz. Capped at a quarter of the substep rate, so a spring is never
    /// stepped too coarsely to be stable. A contact with a static or kinematic
    /// body is twice as stiff.
    pub contact_hertz: f64,
    /// The contact spring's damping ratio: 1 is critical, and more is the
    /// overdamped push that stops a sunk body without bouncing it back out.
    pub damping_ratio: f64,
    /// The fastest a contact pushes two overlapping bodies apart, in m/s.
    pub push_out_speed: f64,
    /// How far apart two shapes may be and still have a contact, in metres,
    /// before speed is added: four times Box2D's 5 mm linear slop.
    /// The pipeline adds how far the pair can close in one tick, so a body
    /// that would cross a thin wall between two ticks meets a contact first.
    pub speculative_distance: f64,
    /// The approach speed below which a contact does not bounce, in m/s, and
    /// below which it raises no [`KineticContact`].
    pub restitution_threshold: f64,
    /// The fastest a body may move, in m/s.
    pub max_linear_speed: f64,
    /// The most a body may turn in one substep, in radians.
    pub max_rotation: f64,
    /// Whether last tick's impulses seed this tick's.
    pub warm_starting: bool,
    /// The smallest normal impulse over a tick, in N·s, that raises a
    /// [`KineticContact`].
    pub kinetic_impulse: f64,
    /// Whether islands sleep. Turned off, every sleeping island wakes on the
    /// next step.
    pub sleep: bool,
    /// How slow a dynamic body must move to count as still, in m/s.
    pub sleep_speed: f64,
    /// How slow a dynamic body must turn to count as still, in rad/s.
    pub sleep_angular_speed: f64,
    /// How long every body of an island must stay still before the island
    /// sleeps, in seconds.
    pub time_to_sleep: f64,
    /// Whether fast bodies and bullets are swept after the solve — rung 4's
    /// continuous collision; see `sweep.rs`. Turned off, speculative contacts
    /// are all that stop a fast body, as before rung 4.
    pub continuous: bool,
}

impl ContactSettings {
    /// Four substeps, 30 Hz contacts at damping ratio 10 pushing out at up to
    /// 3 m/s, a 2 cm speculative distance, bounces above 1 m/s,
    /// 400 m/s and a quarter turn a substep, warm starting on, any impulse
    /// of a newton-second raising an event, an island sleeping once its
    /// bodies have stayed under 5 cm/s and 0.1 rad/s for half a second, and
    /// fast bodies swept.
    ///
    /// **The sleep thresholds.** Half a second under 5 cm/s is decision 4's,
    /// and Box2D v3's `B2_TIME_TO_SLEEP` and default sleep threshold. Turning
    /// is judged twice: as Box2D judges it, by the speed it gives the body's
    /// farthest point ([`crate::ColliderComponent::max_extent`]), which must
    /// stay under the same 5 cm/s, and by angular speed, at 0.1 rad/s — the
    /// turn that moves a point half a metre out at 5 cm/s. The first bounds a
    /// large body's rim; the second keeps a body with no collider, which has
    /// no farthest point, from sleeping while it spins.
    pub const DEFAULT: Self = Self {
        substeps: 4,
        contact_hertz: 30.0,
        damping_ratio: 10.0,
        push_out_speed: 3.0,
        speculative_distance: 4.0 * 0.005,
        restitution_threshold: 1.0,
        max_linear_speed: 400.0,
        max_rotation: 0.25 * core::f64::consts::PI,
        warm_starting: true,
        kinetic_impulse: 1.0,
        sleep: true,
        sleep_speed: 0.05,
        sleep_angular_speed: 0.1,
        time_to_sleep: 0.5,
        continuous: true,
    };
}

impl Default for ContactSettings {
    fn default() -> Self {
        Self::DEFAULT
    }
}

/// How long each stage of one [`crate::PhysicsSystem::step_timed`] took, in
/// seconds, by the clock the caller passed.
#[derive(Clone, Copy, Debug, Default, PartialEq)]
pub struct StageTimes {
    /// Updating the proxies and finding new pairs.
    pub broadphase: f64,
    /// Every pair's manifold.
    pub narrow_phase: f64,
    /// The forces, every substep, the restitution pass and storing the
    /// impulses.
    pub solver: f64,
    /// Keeping the islands: giving new bodies theirs, waking and merging
    /// them, the sleep timers, a split and putting still islands to sleep.
    pub islands: f64,
    /// Sweeping the fast bodies and bullets.
    pub continuous: f64,
}

/// What the last step of a system with contacts did.
#[derive(Clone, Copy, Debug, Default, PartialEq)]
pub struct ContactCounters {
    /// Bodies that step: the awake ones, dynamic and kinematic.
    pub bodies: usize,
    /// Bodies whose island sleeps: nothing steps them or solves them.
    pub sleeping: usize,
    /// Islands awake.
    pub islands: usize,
    /// Islands asleep.
    pub sleeping_islands: usize,
    /// Pairs in the pair set: contacts that exist, touching or not, asleep
    /// or not.
    pub pairs: usize,
    /// Contacts the step collided whose manifold has a point, speculative
    /// ones included. A sleeping island's contacts are not collided, so they
    /// are not counted here, nor their points below.
    pub touching: usize,
    /// Manifold points over every contact.
    pub points: usize,
    /// Points whose impulse was carried over from the tick before.
    pub persisted: usize,
    /// Contacts that gained their first point this step.
    pub begun: u64,
    /// Contacts that lost their last point, or ended, this step.
    pub ended: u64,
    /// The deepest overlap any point had when the step's manifolds were built,
    /// in metres.
    pub worst_penetration: f64,
    /// Points the restitution pass bounced.
    pub bounces: u64,
    /// Over those, the sum of separating speed after over approach speed
    /// before.
    pub bounce_ratio_sum: f64,
    /// Over those, the sum of the restitution each was asked for.
    pub restitution_sum: f64,
    /// Bodies the step swept: awake dynamic bodies that moved fast enough
    /// for their size, and bullets that moved at all.
    pub swept: usize,
    /// Times of impact the sweeps computed — one per part of a swept body per
    /// shape its path's bounds reached — rung 4's "sweep candidates".
    pub sweep_candidates: usize,
    /// Swept bodies that met something on the way and were stopped there.
    pub sweep_hits: usize,
    /// The motion those bodies did not make, summed over them, in seconds of
    /// the tick: each stopped body's share of the tick after its time of
    /// impact, which is dropped rather than solved again.
    pub dropped_time: f64,
    /// Joints the step solved: those with an awake dynamic body.
    pub joints: usize,
    /// The worst positional drift of any of them once the step was done, in
    /// metres — rung 5's "joint error": see [`crate::JointDrift`].
    pub joint_error: f64,
    /// The worst angular drift of any of them, in radians.
    pub joint_angle_error: f64,
    /// Joints the step broke and took out.
    pub broken_joints: usize,
    /// Each stage's time, if the step was timed.
    pub stages: Option<StageTimes>,
}

impl ContactCounters {
    /// The mean bounce the restitution pass produced, as separating speed over
    /// approach speed, or `None` if nothing bounced.
    #[must_use]
    pub fn bounce_ratio(&self) -> Option<f64> {
        (self.bounces > 0).then(|| self.bounce_ratio_sum / self.bounces as f64)
    }

    /// Manifold points over touching contacts — rung 2's "points per
    /// manifold" — or `None` if nothing touched.
    #[must_use]
    pub fn points_per_manifold(&self) -> Option<f64> {
        (self.touching > 0).then(|| self.points as f64 / self.touching as f64)
    }

    /// The share of this step's points whose feature id matched one of the
    /// step before's, and so were warm-started — rung 2's "persisted-id
    /// ratio" — or `None` if there were no points. A stack at rest reads one;
    /// flickering ids read less.
    #[must_use]
    pub fn persisted_ratio(&self) -> Option<f64> {
        (self.points > 0).then(|| self.persisted as f64 / self.points as f64)
    }
}

/// Where a [`KineticContact`] came from.
///
/// `docs/plan/28-ballistics.md` names two sources, a ballistic sweep's chain
/// hit and a solver contact; the ballistic sweep chain is not built, so only
/// the solver's exists.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
#[non_exhaustive]
pub enum KineticSource {
    /// An impulse the contact solver applied.
    Contact,
}

/// Mass in motion meeting a body hard enough to matter —
/// `docs/plan/28-ballistics.md`'s event, raised by the contact solver.
///
/// One is raised per contact per step whose approach speed reached
/// [`ContactSettings::restitution_threshold`] and whose normal impulse over the
/// tick reached [`ContactSettings::kinetic_impulse`], so a crate resting on the
/// floor raises none.
#[derive(Clone, Copy, Debug, PartialEq)]
pub struct KineticContact {
    /// What produced it.
    pub source: KineticSource,
    /// The body that did the hitting: the faster into the contact of two
    /// dynamic bodies, or the static or kinematic one a dynamic body met.
    /// `None` for a plane, which is world geometry with no entity.
    pub impactor: Option<Entity>,
    /// The dynamic body that was hit.
    pub struck: Entity,
    /// Where, averaged over the contact's points.
    pub point: DVec3,
    /// The unit normal, from the impactor into the struck body.
    pub normal: DVec3,
    /// The impactor's velocity less the struck body's, at the contact, as the
    /// step began.
    pub relative_velocity: DVec3,
    /// The impactor's mass, in kilograms: infinite for a static, kinematic or
    /// plane impactor.
    pub impactor_mass: f64,
    /// The kinetic energy the normal impulse took out of the approach, in
    /// joules, summed over the contact's points with each point's own
    /// effective mass — exact for one point, an estimate for several.
    pub energy_deposited: f64,
    /// The normal impulse over the tick, in N·s.
    pub impulse: f64,
}

/// A plane added to a system with contacts.
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, PartialOrd, Ord)]
pub struct PlaneId(u32);

/// One side of a [`ContactReport`].
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum ContactBody {
    /// A registered entity.
    Entity(Entity),
    /// A plane.
    Plane(PlaneId),
}

/// A contact as [`crate::PhysicsSystem::contacts`] reports it.
#[derive(Clone, Copy, Debug, PartialEq)]
pub struct ContactReport {
    /// The contact's slot in the pool: the same for as long as the pair lasts.
    pub slot: u32,
    /// Shape `A`, the lower-ranked; the normal points away from it.
    pub a: ContactBody,
    /// Shape `B`.
    pub b: ContactBody,
    /// Which part of `a`'s collider: an index into its
    /// [`crate::CompoundShape::parts`] for a compound, and 0 for anything else.
    pub part_a: usize,
    /// Which part of `b`'s collider, likewise.
    pub part_b: usize,
    /// The manifold the last step built.
    pub manifold: Manifold,
    /// Each point's normal impulse as the last step left it, in manifold order.
    pub normal_impulses: [f64; MAX_POINTS],
}

/// What owns a proxy: a body's collider — which part of it, for a compound,
/// and part 0 of anything else — or a plane.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
enum Owner {
    Body(BodyId, usize),
    Plane(u32),
}

/// A plane's record.
#[derive(Clone, Copy, Debug)]
struct PlaneRecord {
    bounds: PlaneBounds,
    material: SurfaceMaterial,
}

/// The impulses a contact keeps for warm starting: one normal impulse per
/// point, matched to the next tick's points by feature id, and the friction
/// and twist the whole manifold carries at its centroid.
#[derive(Clone, Copy, Debug, Default, PartialEq)]
struct WarmImpulses {
    normal: [f64; MAX_POINTS],
    /// The friction impulse as a world vector, so a normal that turns between
    /// ticks still hands its successor the right amount.
    friction: DVec3,
    /// The twist impulse about the normal, in N·m·s.
    twist: f64,
}

/// A pair in the pool.
#[derive(Clone, Copy, Debug)]
struct Contact {
    a: ProxyId,
    b: ProxyId,
    manifold: Manifold,
    impulses: WarmImpulses,
    touching: bool,
    /// A box pair's last separating axis. It only ever saves work, so it is
    /// not part of the hashed state.
    cache: SatCache,
}

/// One side of a contact, resolved against the bodies for one tick.
#[derive(Clone, Copy, Debug)]
struct Side {
    shape: ContactShape,
    /// The body's index in the awake set, if it steps.
    awake: Option<usize>,
    entity: Option<Entity>,
    material: SurfaceMaterial,
    position: DVec3,
    velocity: DVec3,
    angular_velocity: DVec3,
    inverse_mass: f64,
    mass: f64,
}

/// The bodies a pipeline reads, lent by the system for one call.
#[derive(Clone, Copy)]
pub(crate) struct Bodies<'a> {
    pub(crate) records: &'a Pool<BodyRecord>,
    pub(crate) statics: &'a StaticSet,
    pub(crate) awake: &'a AwakeSet,
    pub(crate) islands: &'a Islands,
}

/// How one side of a contact takes part in a tick: what the narrow phase and
/// the solver need to know before they resolve the side in full, and what the
/// islands need to know about it.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
enum Presence {
    /// An awake dynamic body.
    Dynamic(BodyId),
    /// A kinematic body with a velocity.
    Moving,
    /// A static body, a kinematic one at rest, or a plane.
    Still,
    /// A body whose island sleeps.
    Asleep(BodyId),
}

impl Presence {
    /// Whether it moves this tick, so a contact with it must be collided.
    const fn moves(self) -> bool {
        matches!(self, Self::Dynamic(_) | Self::Moving)
    }

    /// The body, if it belongs to an island.
    const fn island_body(self) -> Option<BodyId> {
        match self {
            Self::Dynamic(id) | Self::Asleep(id) => Some(id),
            Self::Moving | Self::Still => None,
        }
    }
}

/// What a step's broadphase and narrow phase found that the islands must act
/// on, for the system to apply before it solves.
#[derive(Debug, Default)]
pub(crate) struct IslandEvents {
    /// Sleeping bodies to wake: touched by something that moves, or found
    /// overlapping something placed.
    pub(crate) wake: Vec<BodyId>,
    /// Pairs of island bodies whose contact began touching: their islands
    /// merge.
    pub(crate) links: Vec<(BodyId, BodyId)>,
    /// An island body of each contact between two island bodies that stopped
    /// touching: its island may be in pieces.
    pub(crate) unlinks: Vec<BodyId>,
    /// Awake dynamic bodies touching a moving kinematic body: they are not
    /// still, whatever their own speed.
    pub(crate) stirred: Vec<BodyId>,
}

impl IslandEvents {
    fn clear(&mut self) {
        self.wake.clear();
        self.links.clear();
        self.unlinks.clear();
        self.stirred.clear();
    }
}

/// The contact pipeline a system with contacts owns.
#[derive(Debug)]
pub(crate) struct ContactPipeline {
    pub(crate) settings: ContactSettings,
    broadphase: Broadphase,
    /// Indexed by proxy id.
    owners: Vec<Option<Owner>>,
    planes: Vec<PlaneRecord>,
    contacts: Vec<Option<Contact>>,
    free_contacts: Vec<u32>,
    new_pairs: Vec<(ProxyId, ProxyId)>,
    /// Bodies put to sleep by hand since the last step: a pair a restored
    /// sleeper finds with something still is its surroundings being rebuilt,
    /// not something dropped on it, and wakes nothing.
    restored: Vec<BodyId>,
    /// Touching contacts ended between steps — by a body or a collider taken
    /// out — for the next step's counters.
    ended_between_steps: u64,
    pub(crate) counters: ContactCounters,
    pub(crate) kinetic: Vec<KineticContact>,
    pub(crate) events: IslandEvents,
    /// The contacts inside an island being split.
    edges: Vec<(BodyId, BodyId)>,
    solver: solver::Scratch,
    continuous: sweep::Scratch,
    /// The joints: see `joint/mod.rs`.
    pub(crate) joints: joint::Joints,
    /// The pairs that never collide: see `ignored.rs`.
    pub(crate) ignored: ignored::IgnoredPairs,
}

impl ContactPipeline {
    pub(crate) fn new(settings: ContactSettings) -> Self {
        Self {
            settings,
            broadphase: Broadphase::new(),
            owners: Vec::new(),
            planes: Vec::new(),
            contacts: Vec::new(),
            free_contacts: Vec::new(),
            new_pairs: Vec::new(),
            restored: Vec::new(),
            ended_between_steps: 0,
            counters: ContactCounters::default(),
            kinetic: Vec::new(),
            events: IslandEvents::default(),
            edges: Vec::new(),
            solver: solver::Scratch::default(),
            continuous: sweep::Scratch::default(),
            joints: joint::Joints::default(),
            ignored: ignored::IgnoredPairs::default(),
        }
    }

    // ── Proxies ──────────────────────────────────────────────────────────

    /// Gives body `id` a proxy for each part of its collider, in part order,
    /// or none if the collider is a trigger.
    pub(crate) fn create_body_proxies(&mut self, id: BodyId, bodies: Bodies<'_>) -> Vec<ProxyId> {
        let Some(record) = bodies.records.get(id) else {
            return Vec::new();
        };
        let Some((_, component)) = record.collider.as_ref() else {
            return Vec::new();
        };
        let transform = transform_of(record, bodies);
        let kind = proxy_kind(record.set);
        let mut proxies = Vec::new();
        for part in 0..component.part_count() {
            let Some(shape) = ContactShape::placed_part(component, part, transform) else {
                continue;
            };
            let proxy = self
                .broadphase
                .create(kind, shape.aabb().expect("a body's shape is bounded"));
            self.set_owner(proxy, Owner::Body(id, part));
            proxies.push(proxy);
        }
        proxies
    }

    /// Adds a plane.
    pub(crate) fn add_plane(
        &mut self,
        normal: DVec3,
        offset: f64,
        material: SurfaceMaterial,
    ) -> PlaneId {
        let index = self.planes.len() as u32;
        let bounds = PlaneBounds { normal, offset };
        self.planes.push(PlaneRecord { bounds, material });
        let proxy = self.broadphase.create_plane(index, bounds);
        self.set_owner(proxy, Owner::Plane(index));
        PlaneId(index)
    }

    /// Removes one body's proxies and ends every contact any of them is part
    /// of, counting the touching ones as ended in the next step's counters.
    ///
    /// One walk over the pool finds them all, so a mesh of many triangles
    /// costs a walk and not a walk per triangle. The contacts are ended in the
    /// order ending each proxy's in turn would: by the proxy's place in
    /// `proxies`, then by slot, which is the order their slots are freed and
    /// so reused in. No contact joins two of them, since two proxies of one
    /// body never pair.
    pub(crate) fn destroy_proxies(&mut self, proxies: &[ProxyId]) {
        let mut rank: Vec<(ProxyId, usize)> = proxies
            .iter()
            .enumerate()
            .map(|(place, &proxy)| (proxy, place))
            .collect();
        rank.sort_unstable();
        let place = |proxy: ProxyId| {
            rank.binary_search_by_key(&proxy, |&(p, _)| p)
                .ok()
                .map(|k| rank[k].1)
        };
        let mut doomed: Vec<(usize, usize)> = self
            .contacts
            .iter()
            .enumerate()
            .filter_map(|(slot, contact)| {
                let contact = contact.as_ref()?;
                place(contact.a)
                    .or_else(|| place(contact.b))
                    .map(|place| (place, slot))
            })
            .collect();
        doomed.sort_unstable();
        for (_, slot) in doomed {
            if self.destroy_contact(slot) {
                self.ended_between_steps += 1;
            }
        }
        for &proxy in proxies {
            self.broadphase.destroy(proxy);
            if let Some(owner) = self.owners.get_mut(proxy as usize) {
                *owner = None;
            }
        }
    }

    /// Moves a proxy to the tree its body's set belongs in.
    pub(crate) fn body_changed_set(&mut self, proxy: ProxyId, set: BodySet) {
        self.broadphase.set_kind(proxy, proxy_kind(set));
    }

    /// Tells the broadphase a body was placed by hand.
    pub(crate) fn body_placed(&mut self, id: BodyId, bodies: Bodies<'_>) {
        let Some(record) = bodies.records.get(id) else {
            return;
        };
        let Some((_, component)) = record.collider.as_ref() else {
            return;
        };
        let transform = transform_of(record, bodies);
        for (part, &proxy) in record.proxies.iter().enumerate() {
            if let Some(bounds) =
                ContactShape::placed_part(component, part, transform).and_then(|shape| shape.aabb())
            {
                self.broadphase.update(proxy, bounds);
            }
        }
    }

    fn set_owner(&mut self, proxy: ProxyId, owner: Owner) {
        let index = proxy as usize;
        if index >= self.owners.len() {
            self.owners.resize(index + 1, None);
        }
        self.owners[index] = Some(owner);
    }

    // ── The step ─────────────────────────────────────────────────────────

    /// Brings every moving proxy's bounds up to date for a tick of `dt` and
    /// turns the pairs that are new into contacts.
    ///
    /// A sleeping body's proxy is not updated — it has not moved — and a new
    /// pair between a sleeping body and one that does not move either is a
    /// static or resting kinematic body placed or created where the sleeper
    /// is, which wakes it: the narrow phase never collides such a pair.
    pub(crate) fn update_pairs(&mut self, bodies: Bodies<'_>, dt: f64) {
        self.events.clear();
        let awake = bodies.awake;
        for index in 0..awake.ids.len() {
            let Some(record) = bodies.records.get(awake.ids[index]) else {
                continue;
            };
            let Some((_, component)) = record.collider.as_ref() else {
                continue;
            };
            let transform = &awake.transforms[index];
            let body = &awake.bodies[index];
            for (part, &proxy) in record.proxies.iter().enumerate() {
                let Some(shape) = ContactShape::placed_part(component, part, transform) else {
                    continue;
                };
                let Some(tight) = shape.aabb() else {
                    continue;
                };
                // Where it could be by the end of the tick, and a speculative
                // distance past that, turning included.
                let travel = body.velocity * dt;
                let swept = tight.union(Aabb::new(tight.min + travel, tight.max + travel));
                let turn =
                    body.angular_velocity.length() * dt * shape.reach_from(transform.position);
                self.broadphase.update(
                    proxy,
                    swept.inflated(self.settings.speculative_distance + turn),
                );
            }
        }

        let Self {
            broadphase,
            owners,
            planes,
            new_pairs,
            joints,
            ignored,
            ..
        } = self;
        new_pairs.clear();
        broadphase.find_new_pairs(
            |a, b| {
                // Two parts of one compound are one rigid body: they never
                // collide.
                let body = |proxy: ProxyId| match owners.get(proxy as usize) {
                    Some(Some(Owner::Body(id, _))) => Some(*id),
                    _ => None,
                };
                if body(a).is_some() && body(a) == body(b) {
                    return false;
                }
                // Nor do two bodies a joint keeps apart, that were told to
                // ignore each other, or whose collision filters part them.
                if let (Some(ba), Some(bb)) = (body(a), body(b))
                    && (joints.keeps_apart(ba, bb)
                        || ignored.contains(ba, bb)
                        || !filters_collide(bodies.records, ba, bb))
                {
                    return false;
                }
                let dynamic = |proxy: ProxyId| {
                    owner_side(owners, planes, proxy, bodies).is_some_and(|s| s.inverse_mass > 0.0)
                };
                dynamic(a) || dynamic(b)
            },
            new_pairs,
        );

        for pair in 0..self.new_pairs.len() {
            let (p, q) = self.new_pairs[pair];
            let (Some(sp), Some(sq)) = (self.side(p, bodies), self.side(q, bodies)) else {
                self.broadphase.remove_pair(p, q);
                continue;
            };
            match (self.presence(p, bodies), self.presence(q, bodies)) {
                (Some(Presence::Asleep(id)), Some(Presence::Still))
                | (Some(Presence::Still), Some(Presence::Asleep(id)))
                    if !self.restored.contains(&id) =>
                {
                    self.events.wake.push(id);
                }
                _ => {}
            }
            let (a, b) = if sq.shape.rank() < sp.shape.rank() {
                (q, p)
            } else {
                (p, q)
            };
            let contact = Contact {
                a,
                b,
                manifold: Manifold::EMPTY,
                impulses: WarmImpulses::default(),
                touching: false,
                cache: SatCache::default(),
            };
            match self.free_contacts.pop() {
                Some(slot) => self.contacts[slot as usize] = Some(contact),
                None => self.contacts.push(Some(contact)),
            }
        }
        self.restored.clear();
    }

    /// Notes that body `id` was put to sleep by hand, so the pairs it finds on
    /// the next step do not wake it.
    pub(crate) fn body_restored_asleep(&mut self, id: BodyId) {
        self.restored.push(id);
    }

    /// Every contact's manifold for a tick of `dt`, with last tick's impulses
    /// matched onto the points by feature id, and the pairs whose fat bounds
    /// parted ended.
    ///
    /// A contact neither of whose sides moves, and one of which sleeps, is
    /// left exactly as it was: that is a sleeping island's contact, with
    /// nothing to change it. A sleeping body touched by one that moves is
    /// woken, and contacts beginning or ending between two island bodies are
    /// reported for their islands to merge or be split.
    pub(crate) fn collide(&mut self, bodies: Bodies<'_>, dt: f64) {
        let counters = &mut self.counters;
        counters.touching = 0;
        counters.points = 0;
        counters.persisted = 0;
        counters.begun = 0;
        counters.ended = std::mem::take(&mut self.ended_between_steps);
        counters.worst_penetration = 0.0;

        for slot in 0..self.contacts.len() {
            let Some(mut contact) = self.contacts[slot] else {
                continue;
            };
            let (pa, pb) = (
                self.presence(contact.a, bodies),
                self.presence(contact.b, bodies),
            );
            if let (Some(pa), Some(pb)) = (pa, pb)
                && !pa.moves()
                && !pb.moves()
                && (matches!(pa, Presence::Asleep(_)) || matches!(pb, Presence::Asleep(_)))
            {
                continue;
            }
            let sides = (self.side(contact.a, bodies), self.side(contact.b, bodies));
            let (Some(a), Some(b), Some(pa), Some(pb)) = (sides.0, sides.1, pa, pb) else {
                self.counters.ended += u64::from(self.destroy_contact(slot));
                continue;
            };
            let island_pair = pa.island_body().zip(pb.island_body());
            if !self.broadphase.overlaps(contact.a, contact.b) {
                let was_touching = self.destroy_contact(slot);
                self.counters.ended += u64::from(was_touching);
                if was_touching && let Some((a, _)) = island_pair {
                    self.events.unlinks.push(a);
                }
                continue;
            }

            let manifold = if a.inverse_mass > 0.0 || b.inverse_mass > 0.0 {
                let reach = |side: &Side| {
                    side.velocity.length()
                        + side.angular_velocity.length() * side.shape.reach_from(side.position)
                };
                let speculative = self.settings.speculative_distance + dt * (reach(&a) + reach(&b));
                manifold::collide_cached(&a.shape, &b.shape, speculative, &mut contact.cache)
            } else {
                Manifold::EMPTY
            };

            let mut impulses = WarmImpulses::default();
            let mut manifold = manifold;
            let mut persisted = false;
            for (k, point) in manifold.points_mut().iter_mut().enumerate() {
                if let Some(old) = contact
                    .manifold
                    .points()
                    .iter()
                    .position(|p| p.id == point.id)
                {
                    impulses.normal[k] = contact.impulses.normal[old];
                    persisted = true;
                    self.counters.persisted += 1;
                }
                self.counters.worst_penetration =
                    self.counters.worst_penetration.max(-point.separation);
            }
            // The manifold's friction and twist carry over while any of its
            // points does: a contact that kept none is a new contact.
            if persisted {
                impulses.friction = contact.impulses.friction;
                impulses.twist = contact.impulses.twist;
            }

            let touching = !manifold.points().is_empty();
            if touching && !contact.touching {
                self.counters.begun += 1;
                if let Some(pair) = island_pair {
                    self.events.links.push(pair);
                }
            } else if !touching && contact.touching {
                self.counters.ended += 1;
                if let Some((a, _)) = island_pair {
                    self.events.unlinks.push(a);
                }
            }
            if touching {
                self.counters.touching += 1;
                self.counters.points += manifold.points().len();
                for (this, other) in [(pa, pb), (pb, pa)] {
                    match (this, other) {
                        // Linked as well as woken even when the contact did
                        // not just begin: a body that became dynamic while
                        // touching begins nothing, and this is where its
                        // island finds the one it touches.
                        (Presence::Asleep(id), Presence::Dynamic(other)) => {
                            self.events.wake.push(id);
                            self.events.links.push((id, other));
                        }
                        (Presence::Asleep(id), other) if other.moves() => {
                            self.events.wake.push(id);
                        }
                        (Presence::Dynamic(id), Presence::Moving) => {
                            self.events.stirred.push(id);
                        }
                        _ => {}
                    }
                }
            }
            self.contacts[slot] = Some(Contact {
                manifold,
                impulses,
                touching,
                ..contact
            });
        }
        self.counters.pairs = self.broadphase.pair_count();
    }

    /// Every body touching the body whose proxies are `proxies`, into `out`.
    pub(crate) fn touching_bodies(&self, proxies: &[ProxyId], out: &mut Vec<BodyId>) {
        // Sorted, so a mesh's thousands of proxies cost a search per contact
        // and not a scan.
        let mut sorted = proxies.to_vec();
        sorted.sort_unstable();
        let owns = |proxy: &ProxyId| sorted.binary_search(proxy).is_ok();
        for contact in self.contacts.iter().flatten() {
            if !contact.touching {
                continue;
            }
            let other = if owns(&contact.a) {
                contact.b
            } else if owns(&contact.b) {
                contact.a
            } else {
                continue;
            };
            if let Some(Some(Owner::Body(id, _))) = self.owners.get(other as usize) {
                out.push(*id);
            }
        }
    }

    /// Every touching contact between two bodies of island `island`, in pool
    /// order, then every joint between two of them: what [`Islands::split`]
    /// needs.
    pub(crate) fn island_edges(
        &mut self,
        records: &Pool<BodyRecord>,
        island: IslandId,
    ) -> &[(BodyId, BodyId)] {
        self.edges.clear();
        let in_island = |proxy: ProxyId| match self.owners.get(proxy as usize) {
            Some(Some(Owner::Body(id, _)))
                if records.get(*id).and_then(|record| record.island) == Some(island) =>
            {
                Some(*id)
            }
            _ => None,
        };
        for contact in self.contacts.iter().flatten() {
            if contact.touching
                && let (Some(a), Some(b)) = (in_island(contact.a), in_island(contact.b))
            {
                self.edges.push((a, b));
            }
        }
        self.joints.island_edges(records, island, &mut self.edges);
        &self.edges
    }

    /// Ends every contact between bodies `a` and `b`, counting the touching
    /// ones as ended in the next step's counters: what a joint that keeps
    /// them apart does when it is added.
    pub(crate) fn destroy_contacts_between(&mut self, a: BodyId, b: BodyId) {
        let body = |owners: &[Option<Owner>], proxy: ProxyId| match owners.get(proxy as usize) {
            Some(Some(Owner::Body(id, _))) => Some(*id),
            _ => None,
        };
        for slot in 0..self.contacts.len() {
            let Some(contact) = self.contacts[slot] else {
                continue;
            };
            let (p, q) = (body(&self.owners, contact.a), body(&self.owners, contact.b));
            if (p == Some(a) && q == Some(b)) || (p == Some(b) && q == Some(a)) {
                self.ended_between_steps += u64::from(self.destroy_contact(slot));
            }
        }
    }

    /// Ends every contact between body `id` and a body its collision filter
    /// no longer lets it touch, counting the touching ones as ended in the
    /// next step's counters: what changing a filter does.
    pub(crate) fn destroy_filtered_contacts_of(&mut self, id: BodyId, records: &Pool<BodyRecord>) {
        let body = |owners: &[Option<Owner>], proxy: ProxyId| match owners.get(proxy as usize) {
            Some(Some(Owner::Body(id, _))) => Some(*id),
            _ => None,
        };
        for slot in 0..self.contacts.len() {
            let Some(contact) = self.contacts[slot] else {
                continue;
            };
            let (p, q) = (body(&self.owners, contact.a), body(&self.owners, contact.b));
            let other = match (p, q) {
                (Some(p), Some(q)) if p == id => q,
                (Some(p), Some(q)) if q == id => p,
                _ => continue,
            };
            if !filters_collide(records, id, other) {
                self.ended_between_steps += u64::from(self.destroy_contact(slot));
            }
        }
    }

    /// Puts every proxy in `proxies` in the move buffer, so the next step
    /// looks for its pairs again: what a joint that kept two bodies apart
    /// does when it goes.
    pub(crate) fn rediscover(&mut self, proxies: &[ProxyId]) {
        for &proxy in proxies {
            self.broadphase.touch(proxy);
        }
    }

    /// The joints' part of the counters, once the step is done: how many
    /// were solved and how far the worst of them drifted.
    pub(crate) fn count_joints(&mut self, bodies: Bodies<'_>) {
        let counters = &mut self.counters;
        counters.joints = self.joints.prepared.len();
        counters.joint_error = 0.0;
        counters.joint_angle_error = 0.0;
        for (_, record) in self.joints.iter() {
            let (Some(a), Some(b)) = (bodies.records.get(record.a), bodies.records.get(record.b))
            else {
                continue;
            };
            if a.set != BodySet::Awake && b.set != BodySet::Awake {
                continue;
            }
            let drift = joint::drift(
                &record.joint,
                transform_of(a, bodies),
                transform_of(b, bodies),
            );
            counters.joint_error = counters.joint_error.max(drift.linear);
            counters.joint_angle_error = counters.joint_angle_error.max(drift.angular);
        }
    }

    /// Every live contact, in pool order.
    pub(crate) fn reports(&self, records: &Pool<BodyRecord>) -> Vec<ContactReport> {
        self.contacts
            .iter()
            .enumerate()
            .filter_map(|(slot, contact)| {
                let contact = contact.as_ref()?;
                Some(ContactReport {
                    slot: slot as u32,
                    a: self.contact_body(contact.a, records)?,
                    b: self.contact_body(contact.b, records)?,
                    part_a: self.part(contact.a),
                    part_b: self.part(contact.b),
                    manifold: contact.manifold,
                    normal_impulses: contact.impulses.normal,
                })
            })
            .collect()
    }

    /// Feeds the planes and every contact's manifold and carried impulses into
    /// a determinism hash.
    ///
    /// Contacts go in sorted by the two sides they join rather than in pool
    /// order, which depends on the history of calls that built the pool, and
    /// every float canonicalised, as [`crate::PhysicsSystem`]'s own state is.
    pub(crate) fn hash_state(&self, records: &Pool<BodyRecord>, hasher: &mut dyn Hasher) {
        let write = |hasher: &mut dyn Hasher, value: f64| {
            hasher.write(&canonical_bits(value).to_le_bytes());
        };
        for plane in &self.planes {
            for value in plane.bounds.normal.to_array() {
                write(hasher, value);
            }
            write(hasher, plane.bounds.offset);
        }
        /// A contact side as the sort sees it: an entity's first part, a
        /// plane, a side with no owner, then an entity's later parts — the
        /// tag, the entity or plane, and the part.
        ///
        /// A compound body has one contact per touching pair of parts, so the
        /// part is what keeps two contacts between the same two bodies apart
        /// and in one order. It goes in under a tag of its own, and only for a
        /// part past the first, so a state with no compound in it hashes as it
        /// did before compounds.
        type Key = (u8, u64, u64);
        let key = |proxy: ProxyId| -> Key {
            match self.contact_body(proxy, records) {
                Some(ContactBody::Entity(entity)) => match self.part(proxy) {
                    0 => (0, entity.to_bits(), 0),
                    part => (3, entity.to_bits(), part as u64),
                },
                Some(ContactBody::Plane(PlaneId(index))) => (1, u64::from(index), 0),
                None => (2, 0, 0),
            }
        };
        let mut live: Vec<(Key, Key, &Contact)> = self
            .contacts
            .iter()
            .flatten()
            .map(|contact| (key(contact.a), key(contact.b), contact))
            .collect();
        live.sort_unstable_by_key(|(a, b, _)| (*a, *b));
        for (a, b, contact) in live {
            hasher.write(&[a.0, b.0]);
            hasher.write(&a.1.to_le_bytes());
            hasher.write(&b.1.to_le_bytes());
            for side in [a, b] {
                if side.0 == 3 {
                    hasher.write(&side.2.to_le_bytes());
                }
            }
            let points = contact.manifold.points();
            hasher.write(&(points.len() as u32).to_le_bytes());
            for value in contact.manifold.normal.to_array() {
                write(hasher, value);
            }
            for (point, &impulse) in points.iter().zip(&contact.impulses.normal) {
                hasher.write(&point.id.to_le_bytes());
                for value in point.point.to_array() {
                    write(hasher, value);
                }
                write(hasher, point.separation);
                write(hasher, impulse);
            }
            for value in contact.impulses.friction.to_array() {
                write(hasher, value);
            }
            write(hasher, contact.impulses.twist);
        }
        self.joints.hash_state(records, hasher);
        self.ignored.hash_state(records, hasher);
    }

    fn contact_body(&self, proxy: ProxyId, records: &Pool<BodyRecord>) -> Option<ContactBody> {
        match (*self.owners.get(proxy as usize)?)? {
            Owner::Body(id, _) => Some(ContactBody::Entity(records.get(id)?.entity)),
            Owner::Plane(index) => Some(ContactBody::Plane(PlaneId(index))),
        }
    }

    /// Which part of its collider a proxy is: 0 for anything but a
    /// compound's later parts, planes included.
    fn part(&self, proxy: ProxyId) -> usize {
        match self.owners.get(proxy as usize) {
            Some(Some(Owner::Body(_, part))) => *part,
            _ => 0,
        }
    }

    /// Ends the contact in `slot`, and says whether it was touching.
    fn destroy_contact(&mut self, slot: usize) -> bool {
        let Some(contact) = self.contacts[slot].take() else {
            return false;
        };
        self.broadphase.remove_pair(contact.a, contact.b);
        self.free_contacts.push(slot as u32);
        contact.touching
    }

    fn side(&self, proxy: ProxyId, bodies: Bodies<'_>) -> Option<Side> {
        owner_side(&self.owners, &self.planes, proxy, bodies)
    }

    /// How a proxy's owner takes part this tick, read from its record without
    /// placing its shape: cheap enough to ask of every contact, asleep ones
    /// included.
    fn presence(&self, proxy: ProxyId, bodies: Bodies<'_>) -> Option<Presence> {
        match (*self.owners.get(proxy as usize)?)? {
            Owner::Plane(_) => Some(Presence::Still),
            Owner::Body(id, _) => body_presence(bodies, id),
        }
    }

    /// Whether the contact between `a` and `b` has a side the solver moves:
    /// an awake dynamic body.
    fn solvable(&self, a: ProxyId, b: ProxyId, bodies: Bodies<'_>) -> bool {
        let dynamic = |proxy| matches!(self.presence(proxy, bodies), Some(Presence::Dynamic(_)));
        dynamic(a) || dynamic(b)
    }
}

/// The broadphase tree a body in `set` belongs in: a sleeping body stays in the
/// moving tree, so waking it moves nothing.
const fn proxy_kind(set: BodySet) -> ProxyKind {
    match set {
        BodySet::Static => ProxyKind::Static,
        BodySet::Awake | BodySet::Sleeping => ProxyKind::Moving,
    }
}

/// How body `id` takes part in a tick, read from its record.
fn body_presence(bodies: Bodies<'_>, id: BodyId) -> Option<Presence> {
    let record = bodies.records.get(id)?;
    Some(match record.set {
        BodySet::Static => Presence::Still,
        BodySet::Sleeping => Presence::Asleep(id),
        BodySet::Awake => {
            let body = &bodies.awake.bodies[record.index];
            if body.is_dynamic() {
                Presence::Dynamic(id)
            } else if body.velocity != DVec3::ZERO || body.angular_velocity != DVec3::ZERO {
                Presence::Moving
            } else {
                Presence::Still
            }
        }
    })
}

/// A proxy's side of a contact, resolved against `bodies`.
fn owner_side(
    owners: &[Option<Owner>],
    planes: &[PlaneRecord],
    proxy: ProxyId,
    bodies: Bodies<'_>,
) -> Option<Side> {
    match (*owners.get(proxy as usize)?)? {
        Owner::Plane(index) => {
            let plane = planes.get(index as usize)?;
            Some(Side {
                shape: ContactShape::Plane {
                    normal: plane.bounds.normal,
                    offset: plane.bounds.offset,
                },
                awake: None,
                entity: None,
                material: plane.material,
                position: DVec3::ZERO,
                velocity: DVec3::ZERO,
                angular_velocity: DVec3::ZERO,
                inverse_mass: 0.0,
                mass: f64::INFINITY,
            })
        }
        Owner::Body(id, part) => {
            let record = bodies.records.get(id)?;
            let (_, component) = record.collider.as_ref()?;
            let transform = transform_of(record, bodies);
            let shape = ContactShape::placed_part(component, part, transform)?;
            let (awake, velocity, angular_velocity, inverse_mass, mass) = match record.set {
                BodySet::Static => (None, DVec3::ZERO, DVec3::ZERO, 0.0, f64::INFINITY),
                BodySet::Sleeping => {
                    let body = bodies.islands.body(record.island, record.index);
                    (None, DVec3::ZERO, DVec3::ZERO, body.inverse_mass, body.mass)
                }
                BodySet::Awake => {
                    let body = &bodies.awake.bodies[record.index];
                    (
                        Some(record.index),
                        body.velocity,
                        body.angular_velocity,
                        body.inverse_mass,
                        body.mass,
                    )
                }
            };
            Some(Side {
                shape,
                awake,
                entity: Some(record.entity),
                material: record.material,
                position: transform.position,
                velocity,
                angular_velocity,
                inverse_mass,
                mass,
            })
        }
    }
}

/// Where the body a record names is.
fn transform_of<'a>(record: &BodyRecord, bodies: Bodies<'a>) -> &'a crate::components::Transform {
    match record.set {
        BodySet::Static => &bodies.statics.transforms[record.index],
        BodySet::Awake => &bodies.awake.transforms[record.index],
        BodySet::Sleeping => bodies.islands.transform(record.island, record.index),
    }
}