oxiphysics-collision 0.1.1

Collision detection algorithms for the OxiPhysics engine
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
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
// Copyright 2026 COOLJAPAN OU (Team KitaSan)
// SPDX-License-Identifier: Apache-2.0

//! Core collision detection types.
//!
//! Provides the fundamental value types used throughout the collision pipeline:
//! contact points, contact manifolds, collision pairs, feature IDs, friction /
//! restitution material properties, collision filters, and island/group helpers.

use oxiphysics_core::math::{Real, Vec3};

// ── CollisionPair ─────────────────────────────────────────────────────────────

/// A pair of colliding object indices.
#[allow(dead_code)]
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub struct CollisionPair {
    /// Index of the first object.
    pub a: usize,
    /// Index of the second object.
    pub b: usize,
}

impl CollisionPair {
    /// Create a new collision pair.
    #[allow(dead_code)]
    pub fn new(a: usize, b: usize) -> Self {
        Self { a, b }
    }

    /// Return a canonicalized pair with `a <= b`.
    #[allow(dead_code)]
    pub fn canonical(a: usize, b: usize) -> Self {
        if a <= b {
            Self { a, b }
        } else {
            Self { a: b, b: a }
        }
    }

    /// Returns `true` if the given index is one of the pair's members.
    #[allow(dead_code)]
    pub fn contains(&self, idx: usize) -> bool {
        self.a == idx || self.b == idx
    }

    /// Returns the other member of the pair (panics if `idx` is not in the pair).
    #[allow(dead_code)]
    pub fn other(&self, idx: usize) -> usize {
        if self.a == idx {
            self.b
        } else if self.b == idx {
            self.a
        } else {
            panic!(
                "CollisionPair::other: index {idx} is not in pair ({}, {})",
                self.a, self.b
            )
        }
    }
}

// ── FeatureId ─────────────────────────────────────────────────────────────────

/// Identifies a geometric feature (vertex, edge, or face) on a shape.
///
/// Used by the contact manifold generator to determine which features are in
/// contact, enabling warm-starting and persistent manifold tracking.
#[allow(dead_code)]
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum FeatureId {
    /// A vertex feature with the given local index.
    Vertex(u32),
    /// An edge feature with the given local index.
    Edge(u32),
    /// A face feature with the given local index.
    Face(u32),
    /// Feature identity is unknown or not applicable.
    Unknown,
}

impl FeatureId {
    /// Returns `true` if this is a vertex feature.
    #[allow(dead_code)]
    pub fn is_vertex(self) -> bool {
        matches!(self, FeatureId::Vertex(_))
    }

    /// Returns `true` if this is an edge feature.
    #[allow(dead_code)]
    pub fn is_edge(self) -> bool {
        matches!(self, FeatureId::Edge(_))
    }

    /// Returns `true` if this is a face feature.
    #[allow(dead_code)]
    pub fn is_face(self) -> bool {
        matches!(self, FeatureId::Face(_))
    }

    /// Extract the raw index, or `None` if `Unknown`.
    #[allow(dead_code)]
    pub fn index(self) -> Option<u32> {
        match self {
            FeatureId::Vertex(i) | FeatureId::Edge(i) | FeatureId::Face(i) => Some(i),
            FeatureId::Unknown => None,
        }
    }
}

// ── Contact ───────────────────────────────────────────────────────────────────

/// A single contact point between two objects.
#[allow(dead_code)]
#[derive(Debug, Clone)]
pub struct Contact {
    /// Contact point on body A in world space.
    pub point_a: Vec3,
    /// Contact point on body B in world space.
    pub point_b: Vec3,
    /// Contact normal (from B towards A).
    pub normal: Vec3,
    /// Penetration depth (positive means overlapping).
    pub depth: Real,
}

impl Contact {
    /// Create a new contact point.
    #[allow(dead_code)]
    pub fn new(point_a: Vec3, point_b: Vec3, normal: Vec3, depth: Real) -> Self {
        Self {
            point_a,
            point_b,
            normal,
            depth,
        }
    }

    /// The midpoint of the two contact points.
    #[allow(dead_code)]
    pub fn point(&self) -> Vec3 {
        (self.point_a + self.point_b) * 0.5
    }
}

/// An extended contact point that also carries feature IDs and solver impulses.
///
/// This is the richer variant used by the persistent manifold and warm-starting
/// system.  The simpler [`Contact`] is kept for backward compatibility.
#[allow(dead_code)]
#[derive(Debug, Clone)]
pub struct RichContact {
    /// Contact point on body A in world space.
    pub point_a: Vec3,
    /// Contact point on body B in world space.
    pub point_b: Vec3,
    /// Contact normal (from B towards A).
    pub normal: Vec3,
    /// Penetration depth (positive means overlapping).
    pub depth: Real,
    /// Feature on shape A that generated this contact.
    pub feature_a: FeatureId,
    /// Feature on shape B that generated this contact.
    pub feature_b: FeatureId,
    /// Accumulated normal impulse (used for warm-starting the constraint solver).
    pub impulse_n: Real,
    /// Accumulated tangent impulse along the first friction direction.
    pub impulse_t1: Real,
    /// Accumulated tangent impulse along the second friction direction.
    pub impulse_t2: Real,
}

impl RichContact {
    /// Create a `RichContact` with feature and impulse fields zeroed.
    #[allow(dead_code)]
    pub fn new(point_a: Vec3, point_b: Vec3, normal: Vec3, depth: Real) -> Self {
        Self {
            point_a,
            point_b,
            normal,
            depth,
            feature_a: FeatureId::Unknown,
            feature_b: FeatureId::Unknown,
            impulse_n: 0.0,
            impulse_t1: 0.0,
            impulse_t2: 0.0,
        }
    }

    /// Create from a plain [`Contact`], zeroing feature IDs and impulses.
    #[allow(dead_code)]
    pub fn from_contact(c: &Contact) -> Self {
        Self::new(c.point_a, c.point_b, c.normal, c.depth)
    }

    /// Downgrade to a plain [`Contact`] (drops feature IDs and impulses).
    #[allow(dead_code)]
    pub fn to_contact(&self) -> Contact {
        Contact {
            point_a: self.point_a,
            point_b: self.point_b,
            normal: self.normal,
            depth: self.depth,
        }
    }

    /// The midpoint of the two contact points.
    #[allow(dead_code)]
    pub fn point(&self) -> Vec3 {
        (self.point_a + self.point_b) * 0.5
    }

    /// Build a pair of orthogonal tangent vectors from the contact normal.
    ///
    /// Returns `(t1, t2)` forming a right-handed frame with `self.normal`.
    #[allow(dead_code)]
    pub fn tangent_basis(&self) -> (Vec3, Vec3) {
        let n = self.normal;
        // Choose a reference vector not parallel to n
        let ref_vec = if n.x.abs() < 0.9 {
            Vec3::new(1.0, 0.0, 0.0)
        } else {
            Vec3::new(0.0, 1.0, 0.0)
        };
        let t1 = n.cross(&ref_vec).normalize();
        let t2 = n.cross(&t1);
        (t1, t2)
    }

    /// Returns `true` if the contact is a penetrating contact (depth > 0).
    #[allow(dead_code)]
    pub fn is_penetrating(&self) -> bool {
        self.depth > 0.0
    }
}

// ── ContactManifold ───────────────────────────────────────────────────────────

/// Maximum number of contact points stored per manifold.
pub const MAX_CONTACTS: usize = 4;

/// A manifold holding multiple contact points.
#[allow(dead_code)]
#[derive(Debug, Clone)]
pub struct ContactManifold {
    /// The collision pair.
    pub pair: CollisionPair,
    /// Contact points in this manifold.
    pub contacts: Vec<Contact>,
    /// Number of frames this manifold has been alive.
    pub age: u32,
}

impl ContactManifold {
    /// Create a new empty contact manifold for the given pair.
    #[allow(dead_code)]
    pub fn new(pair: CollisionPair) -> Self {
        Self {
            pair,
            contacts: Vec::new(),
            age: 0,
        }
    }

    /// Add a contact to the manifold, capping at [`MAX_CONTACTS`].
    ///
    /// When the manifold is full the contact with the shallowest penetration
    /// depth is replaced if the new contact is deeper.
    #[allow(dead_code)]
    pub fn add_contact(&mut self, contact: Contact) {
        if self.contacts.len() < MAX_CONTACTS {
            self.contacts.push(contact);
        } else {
            // Replace shallowest contact if new one is deeper
            if let Some(min_idx) = self
                .contacts
                .iter()
                .enumerate()
                .min_by(|(_, a), (_, b)| {
                    a.depth
                        .partial_cmp(&b.depth)
                        .unwrap_or(std::cmp::Ordering::Equal)
                })
                .map(|(i, _)| i)
                && contact.depth > self.contacts[min_idx].depth
            {
                self.contacts[min_idx] = contact;
            }
        }
    }

    /// Returns `true` if there are no contacts.
    #[allow(dead_code)]
    pub fn is_empty(&self) -> bool {
        self.contacts.is_empty()
    }

    /// Number of contact points.
    #[allow(dead_code)]
    pub fn len(&self) -> usize {
        self.contacts.len()
    }

    /// Maximum penetration depth across all contact points.
    #[allow(dead_code)]
    pub fn max_depth(&self) -> Real {
        self.contacts
            .iter()
            .map(|c| c.depth)
            .fold(f64::NEG_INFINITY, f64::max)
    }

    /// Average contact normal. Returns the zero vector if no contacts.
    #[allow(dead_code)]
    pub fn average_normal(&self) -> Vec3 {
        if self.contacts.is_empty() {
            return Vec3::zeros();
        }
        let sum: Vec3 = self
            .contacts
            .iter()
            .map(|c| c.normal)
            .fold(Vec3::zeros(), |a, b| a + b);
        let n = sum / (self.contacts.len() as Real);
        let norm = n.norm();
        if norm > 1e-12 {
            n / norm
        } else {
            Vec3::zeros()
        }
    }

    /// Remove contacts whose depth is below `threshold` (e.g., bodies separated).
    #[allow(dead_code)]
    pub fn prune_shallow(&mut self, threshold: Real) {
        self.contacts.retain(|c| c.depth >= threshold);
    }

    /// Increment the manifold age counter.
    #[allow(dead_code)]
    pub fn tick(&mut self) {
        self.age = self.age.saturating_add(1);
    }
}

// ── RichContactManifold ───────────────────────────────────────────────────────

/// A manifold holding [`RichContact`] points that carry feature IDs and
/// solver impulses.  Used by the warm-starting / persistent manifold system.
#[allow(dead_code)]
#[derive(Debug, Clone)]
pub struct RichContactManifold {
    /// The collision pair.
    pub pair: CollisionPair,
    /// Contact points.
    pub contacts: Vec<RichContact>,
    /// Number of frames this manifold has been alive.
    pub age: u32,
}

impl RichContactManifold {
    /// Create a new empty manifold.
    #[allow(dead_code)]
    pub fn new(pair: CollisionPair) -> Self {
        Self {
            pair,
            contacts: Vec::new(),
            age: 0,
        }
    }

    /// Add a contact, capping at [`MAX_CONTACTS`].
    #[allow(dead_code)]
    pub fn add_contact(&mut self, contact: RichContact) {
        if self.contacts.len() < MAX_CONTACTS {
            self.contacts.push(contact);
        } else {
            if let Some(min_idx) = self
                .contacts
                .iter()
                .enumerate()
                .min_by(|(_, a), (_, b)| {
                    a.depth
                        .partial_cmp(&b.depth)
                        .unwrap_or(std::cmp::Ordering::Equal)
                })
                .map(|(i, _)| i)
                && contact.depth > self.contacts[min_idx].depth
            {
                self.contacts[min_idx] = contact;
            }
        }
    }

    /// Transfer accumulated impulses from `old` matching on feature IDs (warm-starting).
    #[allow(dead_code)]
    pub fn warm_start_from(&mut self, old: &RichContactManifold) {
        for c in &mut self.contacts {
            if let Some(prev) = old
                .contacts
                .iter()
                .find(|p| p.feature_a == c.feature_a && p.feature_b == c.feature_b)
            {
                c.impulse_n = prev.impulse_n;
                c.impulse_t1 = prev.impulse_t1;
                c.impulse_t2 = prev.impulse_t2;
            }
        }
    }

    /// Returns `true` if there are no contacts.
    #[allow(dead_code)]
    pub fn is_empty(&self) -> bool {
        self.contacts.is_empty()
    }

    /// Number of contact points.
    #[allow(dead_code)]
    pub fn len(&self) -> usize {
        self.contacts.len()
    }

    /// Maximum penetration depth.
    #[allow(dead_code)]
    pub fn max_depth(&self) -> Real {
        self.contacts
            .iter()
            .map(|c| c.depth)
            .fold(f64::NEG_INFINITY, f64::max)
    }

    /// Increment the age counter.
    #[allow(dead_code)]
    pub fn tick(&mut self) {
        self.age = self.age.saturating_add(1);
    }
}

// ── PhysicsMaterial ───────────────────────────────────────────────────────────

/// Physical surface properties used by the constraint solver.
#[allow(dead_code)]
#[derive(Debug, Clone, Copy)]
pub struct PhysicsMaterial {
    /// Coefficient of restitution in \[0, 1\]; 0 = perfectly inelastic.
    pub restitution: Real,
    /// Static friction coefficient.
    pub friction_static: Real,
    /// Dynamic (kinetic) friction coefficient.
    pub friction_dynamic: Real,
}

impl Default for PhysicsMaterial {
    fn default() -> Self {
        Self {
            restitution: 0.3,
            friction_static: 0.6,
            friction_dynamic: 0.4,
        }
    }
}

impl PhysicsMaterial {
    /// Create a material with explicit parameters.
    #[allow(dead_code)]
    pub fn new(restitution: Real, friction_static: Real, friction_dynamic: Real) -> Self {
        Self {
            restitution,
            friction_static,
            friction_dynamic,
        }
    }

    /// Combine two materials using geometric-mean mixing for friction and
    /// the minimum for restitution (conservative).
    #[allow(dead_code)]
    pub fn combine(a: &Self, b: &Self) -> Self {
        Self {
            restitution: a.restitution.min(b.restitution),
            friction_static: (a.friction_static * b.friction_static).sqrt(),
            friction_dynamic: (a.friction_dynamic * b.friction_dynamic).sqrt(),
        }
    }
}

// ── CollisionFilter ───────────────────────────────────────────────────────────

/// Bitmask-based collision filter.
///
/// Body A collides with body B if and only if:
/// `A.mask & B.category != 0`.
#[allow(dead_code)]
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub struct CollisionFilter {
    /// Categories this body belongs to (bit flags).
    pub category: u32,
    /// Which categories this body should collide with (bit flags).
    pub mask: u32,
}

impl Default for CollisionFilter {
    fn default() -> Self {
        Self {
            category: 0xFFFF_FFFF,
            mask: 0xFFFF_FFFF,
        }
    }
}

impl CollisionFilter {
    /// Create a filter with explicit category and mask.
    #[allow(dead_code)]
    pub fn new(category: u32, mask: u32) -> Self {
        Self { category, mask }
    }

    /// Returns `true` if `self` and `other` should collide.
    #[allow(dead_code)]
    pub fn should_collide(&self, other: &CollisionFilter) -> bool {
        (self.mask & other.category) != 0 && (other.mask & self.category) != 0
    }

    /// A filter that collides with everything (default).
    #[allow(dead_code)]
    pub fn all() -> Self {
        Self::default()
    }

    /// A filter that collides with nothing.
    #[allow(dead_code)]
    pub fn none() -> Self {
        Self {
            category: 0,
            mask: 0,
        }
    }
}

// ── CollisionEvent ────────────────────────────────────────────────────────────

/// High-level event emitted by the collision pipeline for game logic.
#[allow(dead_code)]
#[derive(Debug, Clone)]
pub enum CollisionEvent {
    /// Two bodies began touching this frame.
    ContactStarted(CollisionPair),
    /// Two bodies that were touching are now separated.
    ContactEnded(CollisionPair),
    /// A trigger (sensor) was entered.
    TriggerEntered {
        /// Index of the sensor body.
        sensor: usize,
        /// Index of the body that entered the sensor.
        body: usize,
    },
    /// A trigger (sensor) was exited.
    TriggerExited {
        /// Index of the sensor body.
        sensor: usize,
        /// Index of the body that exited the sensor.
        body: usize,
    },
}

impl CollisionEvent {
    /// Return the pair of indices involved in the event.
    #[allow(dead_code)]
    pub fn pair(&self) -> (usize, usize) {
        match self {
            CollisionEvent::ContactStarted(p) | CollisionEvent::ContactEnded(p) => (p.a, p.b),
            CollisionEvent::TriggerEntered { sensor, body }
            | CollisionEvent::TriggerExited { sensor, body } => (*sensor, *body),
        }
    }

    /// Returns `true` if this event starts a contact or trigger.
    #[allow(dead_code)]
    pub fn is_enter(&self) -> bool {
        matches!(
            self,
            CollisionEvent::ContactStarted(_) | CollisionEvent::TriggerEntered { .. }
        )
    }

    /// Returns `true` if this event ends a contact or trigger.
    #[allow(dead_code)]
    pub fn is_exit(&self) -> bool {
        !self.is_enter()
    }
}

// ── IslandId ──────────────────────────────────────────────────────────────────

/// Identifier for a rigid-body island (connected component in the contact graph).
///
/// Bodies in the same island share constraints and are solved together.  The
/// sentinel value [`IslandId::NONE`] marks a body that has not been assigned to
/// any island.
#[allow(dead_code)]
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct IslandId(pub u32);

impl IslandId {
    /// Sentinel: body has not been assigned to any island.
    pub const NONE: Self = IslandId(u32::MAX);

    /// Returns `true` if this is the sentinel value.
    #[allow(dead_code)]
    pub fn is_none(self) -> bool {
        self == Self::NONE
    }

    /// Returns `true` if this is a valid island id.
    #[allow(dead_code)]
    pub fn is_some(self) -> bool {
        self != Self::NONE
    }

    /// Raw integer index.
    #[allow(dead_code)]
    pub fn index(self) -> usize {
        self.0 as usize
    }
}

impl Default for IslandId {
    fn default() -> Self {
        Self::NONE
    }
}

impl std::fmt::Display for IslandId {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        if self.is_none() {
            write!(f, "IslandId::NONE")
        } else {
            write!(f, "IslandId({})", self.0)
        }
    }
}

// ── RigidBodyKind ─────────────────────────────────────────────────────────────

/// Classifies how a rigid body participates in dynamics.
#[allow(dead_code)]
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum RigidBodyKind {
    /// Fully simulated: responds to forces, collisions, and constraints.
    Dynamic,
    /// Has infinite mass; cannot be moved by forces or impulses.
    Static,
    /// Kinematically driven: moves under scripted velocity but pushes dynamic bodies.
    Kinematic,
    /// Sensor (trigger): generates overlap events but no contact impulses.
    Sensor,
}

impl RigidBodyKind {
    /// Returns `true` if the body can be moved by the solver.
    #[allow(dead_code)]
    pub fn is_movable(self) -> bool {
        matches!(self, RigidBodyKind::Dynamic | RigidBodyKind::Kinematic)
    }

    /// Returns `true` if the body generates contact forces.
    #[allow(dead_code)]
    pub fn generates_contacts(self) -> bool {
        !matches!(self, RigidBodyKind::Sensor)
    }

    /// Returns `true` if the body participates in island building.
    #[allow(dead_code)]
    pub fn is_island_member(self) -> bool {
        matches!(self, RigidBodyKind::Dynamic)
    }
}

// ── BodyGroup ─────────────────────────────────────────────────────────────────

/// A strongly-connected group of rigid bodies linked by persistent contacts.
///
/// Groups are computed from the contact graph each frame to allow the solver to
/// partition work and detect sleeping candidates.
#[allow(dead_code)]
#[derive(Debug, Clone)]
pub struct BodyGroup {
    /// Island this group belongs to (may span multiple groups).
    pub island: IslandId,
    /// Indices of bodies in this group.
    pub bodies: Vec<usize>,
    /// Whether every body in the group is at rest (can be put to sleep).
    pub all_sleeping: bool,
}

impl BodyGroup {
    /// Create a new group from a list of body indices.
    #[allow(dead_code)]
    pub fn new(island: IslandId, bodies: Vec<usize>) -> Self {
        Self {
            island,
            bodies,
            all_sleeping: false,
        }
    }

    /// Number of bodies in the group.
    #[allow(dead_code)]
    pub fn len(&self) -> usize {
        self.bodies.len()
    }

    /// Returns `true` if the group has no members.
    #[allow(dead_code)]
    pub fn is_empty(&self) -> bool {
        self.bodies.is_empty()
    }

    /// Returns `true` if `body_idx` is a member of this group.
    #[allow(dead_code)]
    pub fn contains(&self, body_idx: usize) -> bool {
        self.bodies.contains(&body_idx)
    }

    /// Mark all bodies as sleeping/not-sleeping.
    #[allow(dead_code)]
    pub fn set_sleeping(&mut self, sleeping: bool) {
        self.all_sleeping = sleeping;
    }
}

// ── ContactConstraint ─────────────────────────────────────────────────────────

/// A position-level contact constraint ready to be fed to a constraint solver.
///
/// Carries the pair, penetration depth, normal, and warm-start impulse.
#[allow(dead_code)]
#[derive(Debug, Clone)]
pub struct ContactConstraint {
    /// The colliding pair.
    pub pair: CollisionPair,
    /// Contact normal (from B toward A, unit length).
    pub normal: Vec3,
    /// Penetration depth (positive = overlapping).
    pub depth: Real,
    /// World-space contact point (midpoint of the two witness points).
    pub point: Vec3,
    /// Accumulated normal impulse from the previous frame (warm-start).
    pub impulse_n: Real,
    /// Accumulated tangent impulse 1 from the previous frame.
    pub impulse_t1: Real,
    /// Accumulated tangent impulse 2 from the previous frame.
    pub impulse_t2: Real,
    /// Effective restitution coefficient for this contact.
    pub restitution: Real,
    /// Effective friction coefficient for this contact.
    pub friction: Real,
}

impl ContactConstraint {
    /// Create a constraint with zero warm-start impulses and default material.
    #[allow(dead_code)]
    pub fn new(pair: CollisionPair, normal: Vec3, depth: Real, point: Vec3) -> Self {
        Self {
            pair,
            normal,
            depth,
            point,
            impulse_n: 0.0,
            impulse_t1: 0.0,
            impulse_t2: 0.0,
            restitution: 0.3,
            friction: 0.5,
        }
    }

    /// Build a pair of tangent vectors for friction from the contact normal.
    #[allow(dead_code)]
    pub fn tangent_basis(&self) -> (Vec3, Vec3) {
        let n = self.normal;
        let ref_vec = if n.x.abs() < 0.9 {
            Vec3::new(1.0, 0.0, 0.0)
        } else {
            Vec3::new(0.0, 1.0, 0.0)
        };
        let t1 = n.cross(&ref_vec).normalize();
        let t2 = n.cross(&t1);
        (t1, t2)
    }

    /// Returns `true` if the contact is currently penetrating.
    #[allow(dead_code)]
    pub fn is_penetrating(&self) -> bool {
        self.depth > 0.0
    }

    /// Zero all accumulated impulses (invalidates warm-start data).
    #[allow(dead_code)]
    pub fn clear_impulses(&mut self) {
        self.impulse_n = 0.0;
        self.impulse_t1 = 0.0;
        self.impulse_t2 = 0.0;
    }
}

// ── ContactVelocityConstraint ─────────────────────────────────────────────────

/// A velocity-level (impulse-based) contact constraint.
///
/// Pre-computed quantities that remain constant during the solver iteration.
#[allow(dead_code)]
#[derive(Debug, Clone)]
pub struct ContactVelocityConstraint {
    /// The colliding pair.
    pub pair: CollisionPair,
    /// Contact normal.
    pub normal: Vec3,
    /// Target relative velocity along the normal (bias + restitution).
    pub velocity_bias: Real,
    /// Effective mass along the normal direction.
    pub effective_mass_n: Real,
    /// Effective mass along tangent 1.
    pub effective_mass_t1: Real,
    /// Effective mass along tangent 2.
    pub effective_mass_t2: Real,
    /// Friction coefficient.
    pub friction: Real,
    /// Accumulated normal impulse (clamped to >= 0 during solve).
    pub impulse_n: Real,
    /// Accumulated tangent impulse 1.
    pub impulse_t1: Real,
    /// Accumulated tangent impulse 2.
    pub impulse_t2: Real,
}

impl ContactVelocityConstraint {
    /// Create a velocity constraint with zeroed impulses.
    #[allow(dead_code)]
    pub fn new(pair: CollisionPair, normal: Vec3) -> Self {
        Self {
            pair,
            normal,
            velocity_bias: 0.0,
            effective_mass_n: 1.0,
            effective_mass_t1: 1.0,
            effective_mass_t2: 1.0,
            friction: 0.5,
            impulse_n: 0.0,
            impulse_t1: 0.0,
            impulse_t2: 0.0,
        }
    }

    /// Returns `true` if there is any accumulated impulse.
    #[allow(dead_code)]
    pub fn has_impulse(&self) -> bool {
        self.impulse_n.abs() > 1e-12
            || self.impulse_t1.abs() > 1e-12
            || self.impulse_t2.abs() > 1e-12
    }

    /// The total impulse magnitude (for debugging/telemetry).
    #[allow(dead_code)]
    pub fn impulse_magnitude(&self) -> Real {
        (self.impulse_n * self.impulse_n
            + self.impulse_t1 * self.impulse_t1
            + self.impulse_t2 * self.impulse_t2)
            .sqrt()
    }

    /// Warm-start: copy impulses from a previous-frame constraint.
    #[allow(dead_code)]
    pub fn warm_start_from(&mut self, prev: &ContactVelocityConstraint) {
        self.impulse_n = prev.impulse_n;
        self.impulse_t1 = prev.impulse_t1;
        self.impulse_t2 = prev.impulse_t2;
    }
}

// ── ContactBatch ──────────────────────────────────────────────────────────────

/// A batch of velocity constraints to be solved together (same island).
#[allow(dead_code)]
#[derive(Debug, Default)]
pub struct ContactBatch {
    /// All velocity constraints in the batch.
    pub constraints: Vec<ContactVelocityConstraint>,
    /// The island this batch belongs to.
    pub island: IslandId,
}

impl ContactBatch {
    /// Create an empty batch for the given island.
    #[allow(dead_code)]
    pub fn new(island: IslandId) -> Self {
        Self {
            constraints: Vec::new(),
            island,
        }
    }

    /// Add a constraint to the batch.
    #[allow(dead_code)]
    pub fn push(&mut self, c: ContactVelocityConstraint) {
        self.constraints.push(c);
    }

    /// Number of constraints.
    #[allow(dead_code)]
    pub fn len(&self) -> usize {
        self.constraints.len()
    }

    /// Returns `true` if no constraints are present.
    #[allow(dead_code)]
    pub fn is_empty(&self) -> bool {
        self.constraints.is_empty()
    }

    /// Total accumulated normal impulse across all constraints.
    #[allow(dead_code)]
    pub fn total_normal_impulse(&self) -> Real {
        self.constraints.iter().map(|c| c.impulse_n).sum()
    }

    /// Clear all impulses (invalidates warm-start data for this batch).
    #[allow(dead_code)]
    pub fn clear_impulses(&mut self) {
        for c in &mut self.constraints {
            c.impulse_n = 0.0;
            c.impulse_t1 = 0.0;
            c.impulse_t2 = 0.0;
        }
    }
}

// ── CollisionMatrix ───────────────────────────────────────────────────────────

/// Dense bitmatrix of which category bits collide with which others.
///
/// Up to 32 categories are supported.  The matrix is symmetric: if `(i, j)` is
/// set then `(j, i)` is also set.
#[allow(dead_code)]
#[derive(Debug, Clone)]
pub struct CollisionMatrix {
    pub(super) rows: [u32; 32],
}

impl Default for CollisionMatrix {
    fn default() -> Self {
        Self::all_collide()
    }
}

impl CollisionMatrix {
    /// Create a matrix where every category collides with every other.
    #[allow(dead_code)]
    pub fn all_collide() -> Self {
        Self {
            rows: [u32::MAX; 32],
        }
    }

    /// Create a matrix where no categories collide.
    #[allow(dead_code)]
    pub fn none_collide() -> Self {
        Self { rows: [0u32; 32] }
    }

    /// Enable collision between categories `a` and `b` (symmetric).
    #[allow(dead_code)]
    pub fn enable(&mut self, a: u8, b: u8) {
        let a = (a & 31) as usize;
        let b = (b & 31) as usize;
        self.rows[a] |= 1 << b;
        self.rows[b] |= 1 << a;
    }

    /// Disable collision between categories `a` and `b` (symmetric).
    #[allow(dead_code)]
    pub fn disable(&mut self, a: u8, b: u8) {
        let a = (a & 31) as usize;
        let b = (b & 31) as usize;
        self.rows[a] &= !(1 << b);
        self.rows[b] &= !(1 << a);
    }

    /// Returns `true` if categories `a` and `b` should collide.
    #[allow(dead_code)]
    pub fn should_collide(&self, a: u8, b: u8) -> bool {
        let a = (a & 31) as usize;
        let b = (b & 31) as usize;
        (self.rows[a] >> b) & 1 == 1
    }
}

// ── ContactStats ──────────────────────────────────────────────────────────────

/// Per-frame contact statistics collected by the pipeline.
#[allow(dead_code)]
#[derive(Debug, Default, Clone, Copy)]
pub struct ContactStats {
    /// Total number of broadphase pairs tested.
    pub broadphase_pairs: u32,
    /// Number of pairs that passed to narrowphase.
    pub narrowphase_pairs: u32,
    /// Number of contacts generated by narrowphase.
    pub contacts_generated: u32,
    /// Number of contacts removed due to manifold aging.
    pub contacts_expired: u32,
    /// Number of islands built.
    pub islands: u32,
}

impl ContactStats {
    /// Create a zeroed stats block.
    #[allow(dead_code)]
    pub fn new() -> Self {
        Self::default()
    }

    /// Returns the ratio of narrowphase work to broadphase pairs.
    ///
    /// A value close to 1 means the broadphase is poor at rejection.
    #[allow(dead_code)]
    pub fn narrowphase_ratio(&self) -> f64 {
        if self.broadphase_pairs == 0 {
            return 0.0;
        }
        self.narrowphase_pairs as f64 / self.broadphase_pairs as f64
    }

    /// Reset all counters.
    #[allow(dead_code)]
    pub fn reset(&mut self) {
        *self = Self::default();
    }
}

// ── tests ─────────────────────────────────────────────────────────────────────

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

    #[test]
    fn test_collision_pair_basic() {
        let pair = CollisionPair::new(0, 1);
        assert_eq!(pair.a, 0);
        assert_eq!(pair.b, 1);
    }

    #[test]
    fn test_collision_pair_canonical() {
        let p1 = CollisionPair::canonical(3, 1);
        assert!(p1.a <= p1.b);
        let p2 = CollisionPair::canonical(1, 3);
        assert_eq!(p1, p2);
    }

    #[test]
    fn test_collision_pair_contains() {
        let p = CollisionPair::new(2, 7);
        assert!(p.contains(2));
        assert!(p.contains(7));
        assert!(!p.contains(5));
    }

    #[test]
    fn test_collision_pair_other() {
        let p = CollisionPair::new(2, 7);
        assert_eq!(p.other(2), 7);
        assert_eq!(p.other(7), 2);
    }

    #[test]
    #[should_panic]
    fn test_collision_pair_other_panics() {
        let p = CollisionPair::new(2, 7);
        let _ = p.other(99);
    }

    #[test]
    fn test_feature_id() {
        assert!(FeatureId::Vertex(0).is_vertex());
        assert!(FeatureId::Edge(2).is_edge());
        assert!(FeatureId::Face(5).is_face());
        assert_eq!(FeatureId::Face(5).index(), Some(5));
        assert_eq!(FeatureId::Unknown.index(), None);
    }

    #[test]
    fn test_contact_manifold_basic() {
        let pair = CollisionPair::new(0, 1);
        let mut manifold = ContactManifold::new(pair);
        manifold.add_contact(Contact {
            point_a: Vec3::zeros(),
            point_b: Vec3::new(0.0, -0.1, 0.0),
            normal: Vec3::new(0.0, 1.0, 0.0),
            depth: 0.1,
        });
        assert_eq!(manifold.contacts.len(), 1);
    }

    fn make_contact(depth: f64) -> Contact {
        Contact {
            point_a: Vec3::zeros(),
            point_b: Vec3::zeros(),
            normal: Vec3::new(0.0, 1.0, 0.0),
            depth,
        }
    }

    #[test]
    fn test_manifold_max_contacts_cap() {
        let pair = CollisionPair::new(0, 1);
        let mut manifold = ContactManifold::new(pair);
        for i in 0..8 {
            manifold.add_contact(make_contact(i as f64 * 0.01));
        }
        assert!(manifold.contacts.len() <= MAX_CONTACTS);
    }

    #[test]
    fn test_manifold_max_depth() {
        let pair = CollisionPair::new(0, 1);
        let mut m = ContactManifold::new(pair);
        m.add_contact(make_contact(0.1));
        m.add_contact(make_contact(0.5));
        m.add_contact(make_contact(0.3));
        assert!((m.max_depth() - 0.5).abs() < 1e-12);
    }

    #[test]
    fn test_manifold_prune_shallow() {
        let pair = CollisionPair::new(0, 1);
        let mut m = ContactManifold::new(pair);
        m.add_contact(make_contact(0.1));
        m.add_contact(make_contact(-0.05));
        m.prune_shallow(0.0);
        assert_eq!(m.contacts.len(), 1);
        assert!((m.contacts[0].depth - 0.1).abs() < 1e-12);
    }

    #[test]
    fn test_manifold_average_normal() {
        let pair = CollisionPair::new(0, 1);
        let mut m = ContactManifold::new(pair);
        m.add_contact(make_contact(0.1));
        m.add_contact(make_contact(0.2));
        let avg = m.average_normal();
        assert!((avg.norm() - 1.0).abs() < 1e-10);
    }

    #[test]
    fn test_manifold_empty_average_normal() {
        let m = ContactManifold::new(CollisionPair::new(0, 1));
        let avg = m.average_normal();
        assert_eq!(avg.norm(), 0.0);
    }

    #[test]
    fn test_rich_contact_tangent_basis_orthogonal() {
        let c = RichContact::new(Vec3::zeros(), Vec3::zeros(), Vec3::new(0.0, 1.0, 0.0), 0.1);
        let (t1, t2) = c.tangent_basis();
        assert!(c.normal.dot(&t1).abs() < 1e-10, "t1 not perp to normal");
        assert!(c.normal.dot(&t2).abs() < 1e-10, "t2 not perp to normal");
        assert!(t1.dot(&t2).abs() < 1e-10, "t1 not perp to t2");
    }

    #[test]
    fn test_physics_material_combine() {
        let a = PhysicsMaterial::new(0.8, 0.4, 0.3);
        let b = PhysicsMaterial::new(0.5, 0.9, 0.6);
        let c = PhysicsMaterial::combine(&a, &b);
        assert!((c.restitution - 0.5).abs() < 1e-12, "min restitution");
        let expected_fs = (0.4_f64 * 0.9).sqrt();
        assert!((c.friction_static - expected_fs).abs() < 1e-12);
    }

    #[test]
    fn test_collision_filter_default_collides() {
        let f1 = CollisionFilter::default();
        let f2 = CollisionFilter::default();
        assert!(f1.should_collide(&f2));
    }

    #[test]
    fn test_collision_filter_none_no_collide() {
        let f1 = CollisionFilter::none();
        let f2 = CollisionFilter::default();
        assert!(!f1.should_collide(&f2));
    }

    #[test]
    fn test_collision_filter_category_mask() {
        // player (cat 1) collides with wall (cat 2) if its mask includes cat 2
        let player = CollisionFilter::new(0b0001, 0b0010);
        let wall = CollisionFilter::new(0b0010, 0b0001);
        assert!(player.should_collide(&wall));
        // two players: cat 1, mask 0b0010 → they should NOT collide with each other
        let player2 = CollisionFilter::new(0b0001, 0b0010);
        assert!(!player.should_collide(&player2)); // mask 0b0010 & cat 0b0001 == 0
    }

    #[test]
    fn test_collision_event_pair() {
        let ev = CollisionEvent::ContactStarted(CollisionPair::new(3, 7));
        assert_eq!(ev.pair(), (3, 7));
        assert!(ev.is_enter());
        assert!(!ev.is_exit());
    }

    #[test]
    fn test_collision_event_trigger() {
        let ev = CollisionEvent::TriggerEntered {
            sensor: 10,
            body: 2,
        };
        assert_eq!(ev.pair(), (10, 2));
        assert!(ev.is_enter());
        let ev2 = CollisionEvent::TriggerExited {
            sensor: 10,
            body: 2,
        };
        assert!(ev2.is_exit());
    }

    #[test]
    fn test_rich_manifold_warm_start_from() {
        let pair = CollisionPair::new(0, 1);
        let mut old = RichContactManifold::new(pair);
        let mut c = RichContact::new(Vec3::zeros(), Vec3::zeros(), Vec3::new(0.0, 1.0, 0.0), 0.1);
        c.feature_a = FeatureId::Face(0);
        c.feature_b = FeatureId::Face(1);
        c.impulse_n = 5.0;
        old.add_contact(c);

        let mut new_m = RichContactManifold::new(pair);
        let mut c2 = RichContact::new(Vec3::zeros(), Vec3::zeros(), Vec3::new(0.0, 1.0, 0.0), 0.12);
        c2.feature_a = FeatureId::Face(0);
        c2.feature_b = FeatureId::Face(1);
        new_m.add_contact(c2);

        new_m.warm_start_from(&old);
        assert!((new_m.contacts[0].impulse_n - 5.0).abs() < 1e-12);
    }

    #[test]
    fn test_manifold_tick_age() {
        let mut m = ContactManifold::new(CollisionPair::new(0, 1));
        assert_eq!(m.age, 0);
        m.tick();
        m.tick();
        assert_eq!(m.age, 2);
    }

    #[test]
    fn test_rich_contact_from_contact_roundtrip() {
        let c = Contact {
            point_a: Vec3::new(1.0, 0.0, 0.0),
            point_b: Vec3::new(2.0, 0.0, 0.0),
            normal: Vec3::new(0.0, 1.0, 0.0),
            depth: 0.3,
        };
        let rc = RichContact::from_contact(&c);
        assert_eq!(rc.depth, 0.3);
        assert_eq!(rc.feature_a, FeatureId::Unknown);
        let back = rc.to_contact();
        assert!((back.depth - 0.3).abs() < 1e-12);
    }

    #[test]
    fn test_rich_contact_is_penetrating() {
        let c_pen = RichContact::new(Vec3::zeros(), Vec3::zeros(), Vec3::new(0.0, 1.0, 0.0), 0.1);
        let c_sep = RichContact::new(Vec3::zeros(), Vec3::zeros(), Vec3::new(0.0, 1.0, 0.0), -0.1);
        assert!(c_pen.is_penetrating());
        assert!(!c_sep.is_penetrating());
    }

    // ── IslandId tests ───────────────────────────────────────────────────────

    #[test]
    fn island_id_none_is_sentinel() {
        let id = IslandId::NONE;
        assert!(id.is_none());
        assert!(!id.is_some());
    }

    #[test]
    fn island_id_valid() {
        let id = IslandId(3);
        assert!(!id.is_none());
        assert!(id.is_some());
        assert_eq!(id.index(), 3);
    }

    #[test]
    fn island_id_default_is_none() {
        let id = IslandId::default();
        assert!(id.is_none());
    }

    #[test]
    fn island_id_ordering() {
        assert!(IslandId(0) < IslandId(1));
        assert!(IslandId(0) < IslandId::NONE);
    }

    #[test]
    fn island_id_display_none() {
        let s = format!("{}", IslandId::NONE);
        assert!(s.contains("NONE"), "display: {s}");
    }

    #[test]
    fn island_id_display_value() {
        let s = format!("{}", IslandId(42));
        assert!(s.contains("42"), "display: {s}");
    }

    // ── RigidBodyKind tests ──────────────────────────────────────────────────

    #[test]
    fn rigid_body_kind_movable() {
        assert!(RigidBodyKind::Dynamic.is_movable());
        assert!(RigidBodyKind::Kinematic.is_movable());
        assert!(!RigidBodyKind::Static.is_movable());
        assert!(!RigidBodyKind::Sensor.is_movable());
    }

    #[test]
    fn rigid_body_kind_generates_contacts() {
        assert!(RigidBodyKind::Dynamic.generates_contacts());
        assert!(RigidBodyKind::Static.generates_contacts());
        assert!(RigidBodyKind::Kinematic.generates_contacts());
        assert!(!RigidBodyKind::Sensor.generates_contacts());
    }

    #[test]
    fn rigid_body_kind_island_member() {
        assert!(RigidBodyKind::Dynamic.is_island_member());
        assert!(!RigidBodyKind::Static.is_island_member());
        assert!(!RigidBodyKind::Kinematic.is_island_member());
        assert!(!RigidBodyKind::Sensor.is_island_member());
    }

    // ── BodyGroup tests ──────────────────────────────────────────────────────

    #[test]
    fn body_group_basic() {
        let g = BodyGroup::new(IslandId(0), vec![1, 2, 3]);
        assert_eq!(g.len(), 3);
        assert!(g.contains(2));
        assert!(!g.contains(99));
        assert!(!g.is_empty());
    }

    #[test]
    fn body_group_empty() {
        let g = BodyGroup::new(IslandId(0), vec![]);
        assert!(g.is_empty());
        assert_eq!(g.len(), 0);
    }

    #[test]
    fn body_group_set_sleeping() {
        let mut g = BodyGroup::new(IslandId(0), vec![1]);
        assert!(!g.all_sleeping);
        g.set_sleeping(true);
        assert!(g.all_sleeping);
        g.set_sleeping(false);
        assert!(!g.all_sleeping);
    }

    // ── ContactConstraint tests ──────────────────────────────────────────────

    #[test]
    fn contact_constraint_new() {
        let pair = CollisionPair::new(0, 1);
        let c = ContactConstraint::new(pair, Vec3::new(0.0, 1.0, 0.0), 0.05, Vec3::zeros());
        assert!(c.is_penetrating());
        assert_eq!(c.impulse_n, 0.0);
        assert_eq!(c.impulse_t1, 0.0);
        assert_eq!(c.impulse_t2, 0.0);
    }

    #[test]
    fn contact_constraint_not_penetrating() {
        let pair = CollisionPair::new(0, 1);
        let c = ContactConstraint::new(pair, Vec3::new(0.0, 1.0, 0.0), -0.01, Vec3::zeros());
        assert!(!c.is_penetrating());
    }

    #[test]
    fn contact_constraint_tangent_basis_orthogonal() {
        let pair = CollisionPair::new(0, 1);
        let c = ContactConstraint::new(pair, Vec3::new(0.0, 1.0, 0.0), 0.1, Vec3::zeros());
        let (t1, t2) = c.tangent_basis();
        assert!(c.normal.dot(&t1).abs() < 1e-10, "t1 not perp to normal");
        assert!(c.normal.dot(&t2).abs() < 1e-10, "t2 not perp to normal");
        assert!(t1.dot(&t2).abs() < 1e-10, "t1 not perp to t2");
    }

    #[test]
    fn contact_constraint_clear_impulses() {
        let pair = CollisionPair::new(0, 1);
        let mut c = ContactConstraint::new(pair, Vec3::new(0.0, 1.0, 0.0), 0.1, Vec3::zeros());
        c.impulse_n = 5.0;
        c.impulse_t1 = 2.0;
        c.impulse_t2 = -1.0;
        c.clear_impulses();
        assert_eq!(c.impulse_n, 0.0);
        assert_eq!(c.impulse_t1, 0.0);
        assert_eq!(c.impulse_t2, 0.0);
    }

    // ── ContactVelocityConstraint tests ──────────────────────────────────────

    #[test]
    fn velocity_constraint_new_zeroed() {
        let c = ContactVelocityConstraint::new(CollisionPair::new(0, 1), Vec3::new(0.0, 1.0, 0.0));
        assert!(!c.has_impulse());
        assert_eq!(c.impulse_magnitude(), 0.0);
    }

    #[test]
    fn velocity_constraint_has_impulse_after_set() {
        let mut c =
            ContactVelocityConstraint::new(CollisionPair::new(0, 1), Vec3::new(0.0, 1.0, 0.0));
        c.impulse_n = 3.0;
        assert!(c.has_impulse());
    }

    #[test]
    fn velocity_constraint_impulse_magnitude() {
        let mut c =
            ContactVelocityConstraint::new(CollisionPair::new(0, 1), Vec3::new(0.0, 1.0, 0.0));
        c.impulse_n = 3.0;
        c.impulse_t1 = 4.0;
        let mag = c.impulse_magnitude();
        assert!((mag - 5.0).abs() < 1e-10, "magnitude={mag}");
    }

    #[test]
    fn velocity_constraint_warm_start_from() {
        let mut prev =
            ContactVelocityConstraint::new(CollisionPair::new(0, 1), Vec3::new(0.0, 1.0, 0.0));
        prev.impulse_n = 7.0;
        prev.impulse_t1 = -2.0;
        prev.impulse_t2 = 1.5;

        let mut curr =
            ContactVelocityConstraint::new(CollisionPair::new(0, 1), Vec3::new(0.0, 1.0, 0.0));
        curr.warm_start_from(&prev);
        assert!((curr.impulse_n - 7.0).abs() < 1e-12);
        assert!((curr.impulse_t1 - (-2.0)).abs() < 1e-12);
        assert!((curr.impulse_t2 - 1.5).abs() < 1e-12);
    }

    // ── ContactBatch tests ────────────────────────────────────────────────────

    #[test]
    fn contact_batch_empty() {
        let batch = ContactBatch::new(IslandId(0));
        assert!(batch.is_empty());
        assert_eq!(batch.len(), 0);
        assert_eq!(batch.total_normal_impulse(), 0.0);
    }

    #[test]
    fn contact_batch_push_and_len() {
        let mut batch = ContactBatch::new(IslandId(1));
        let c = ContactVelocityConstraint::new(CollisionPair::new(0, 1), Vec3::new(0.0, 1.0, 0.0));
        batch.push(c);
        assert_eq!(batch.len(), 1);
        assert!(!batch.is_empty());
    }

    #[test]
    fn contact_batch_total_normal_impulse() {
        let mut batch = ContactBatch::new(IslandId(0));
        let mut c1 =
            ContactVelocityConstraint::new(CollisionPair::new(0, 1), Vec3::new(0.0, 1.0, 0.0));
        c1.impulse_n = 3.0;
        let mut c2 =
            ContactVelocityConstraint::new(CollisionPair::new(1, 2), Vec3::new(0.0, 1.0, 0.0));
        c2.impulse_n = 5.0;
        batch.push(c1);
        batch.push(c2);
        assert!((batch.total_normal_impulse() - 8.0).abs() < 1e-12);
    }

    #[test]
    fn contact_batch_clear_impulses() {
        let mut batch = ContactBatch::new(IslandId(0));
        let mut c =
            ContactVelocityConstraint::new(CollisionPair::new(0, 1), Vec3::new(0.0, 1.0, 0.0));
        c.impulse_n = 9.0;
        batch.push(c);
        batch.clear_impulses();
        assert_eq!(batch.total_normal_impulse(), 0.0);
    }

    // ── CollisionMatrix tests ─────────────────────────────────────────────────

    #[test]
    fn collision_matrix_all_collide() {
        let m = CollisionMatrix::all_collide();
        for a in 0..32u8 {
            for b in 0..32u8 {
                assert!(
                    m.should_collide(a, b),
                    "all_collide: ({a},{b}) should collide"
                );
            }
        }
    }

    #[test]
    fn collision_matrix_none_collide() {
        let m = CollisionMatrix::none_collide();
        for a in 0..32u8 {
            for b in 0..32u8 {
                assert!(
                    !m.should_collide(a, b),
                    "none_collide: ({a},{b}) should not collide"
                );
            }
        }
    }

    #[test]
    fn collision_matrix_enable_symmetric() {
        let mut m = CollisionMatrix::none_collide();
        m.enable(2, 5);
        assert!(m.should_collide(2, 5));
        assert!(m.should_collide(5, 2), "must be symmetric");
        assert!(!m.should_collide(2, 3));
    }

    #[test]
    fn collision_matrix_disable_symmetric() {
        let mut m = CollisionMatrix::all_collide();
        m.disable(3, 7);
        assert!(!m.should_collide(3, 7));
        assert!(!m.should_collide(7, 3), "must be symmetric");
        assert!(m.should_collide(3, 8), "other pairs unaffected");
    }

    #[test]
    fn collision_matrix_self_collision() {
        let mut m = CollisionMatrix::none_collide();
        m.enable(4, 4);
        assert!(m.should_collide(4, 4));
    }

    // ── ContactStats tests ────────────────────────────────────────────────────

    #[test]
    fn contact_stats_default_zeroed() {
        let s = ContactStats::new();
        assert_eq!(s.broadphase_pairs, 0);
        assert_eq!(s.narrowphase_pairs, 0);
        assert_eq!(s.contacts_generated, 0);
    }

    #[test]
    fn contact_stats_narrowphase_ratio_zero_broadphase() {
        let s = ContactStats::new();
        assert_eq!(s.narrowphase_ratio(), 0.0);
    }

    #[test]
    fn contact_stats_narrowphase_ratio() {
        let s = ContactStats {
            broadphase_pairs: 100,
            narrowphase_pairs: 25,
            ..ContactStats::default()
        };
        assert!((s.narrowphase_ratio() - 0.25).abs() < 1e-12);
    }

    #[test]
    fn contact_stats_reset() {
        let mut s = ContactStats {
            broadphase_pairs: 50,
            contacts_generated: 10,
            islands: 3,
            ..ContactStats::default()
        };
        s.reset();
        assert_eq!(s.broadphase_pairs, 0);
        assert_eq!(s.contacts_generated, 0);
        assert_eq!(s.islands, 0);
    }
}