oxiphysics-core 0.1.0

Core types, traits, and abstractions 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
#![allow(clippy::ptr_arg)]
// Copyright 2026 COOLJAPAN OU (Team KitaSan)
// SPDX-License-Identifier: Apache-2.0

//! Core simulation traits for the OxiPhysics engine.
//!
//! This module defines the fundamental behavioural contracts used throughout
//! the engine.  All physics objects implement one or more of these traits so
//! that solvers, integrators, and broadphase structures can operate on them
//! generically.

#![allow(dead_code)]
#![allow(clippy::too_many_arguments)]

use crate::math::Real;
use crate::types::{Aabb, TimeStep, Transform};

// ---------------------------------------------------------------------------
// Time integration
// ---------------------------------------------------------------------------

/// Trait for objects that can be stepped forward in time.
pub trait Steppable {
    /// Advance the simulation by one time step.
    fn step(&mut self, time_step: &TimeStep);
}

// ---------------------------------------------------------------------------
// Physics body
// ---------------------------------------------------------------------------

/// Trait for objects that represent a physics body.
pub trait PhysicsBody {
    /// Return the current transform of this body.
    fn transform(&self) -> &Transform;

    /// Return a mutable reference to the transform.
    fn transform_mut(&mut self) -> &mut Transform;

    /// Return the mass of this body (kg). Returns 0 for static/kinematic.
    fn mass(&self) -> Real;

    /// Return the inverse mass. Returns 0 for static/kinematic.
    fn inv_mass(&self) -> Real {
        if self.mass() > 0.0 {
            1.0 / self.mass()
        } else {
            0.0
        }
    }

    /// Whether this body participates in dynamics integration.
    fn is_dynamic(&self) -> bool {
        self.mass() > 0.0
    }

    /// Whether this body is treated as immovable.
    fn is_static(&self) -> bool {
        self.mass() == 0.0
    }
}

// ---------------------------------------------------------------------------
// Collision
// ---------------------------------------------------------------------------

/// Trait for objects that can participate in collision detection.
pub trait Collidable {
    /// Return the axis-aligned bounding box of this object.
    fn bounding_box(&self) -> Aabb;

    /// Whether this object can interact with another.
    fn can_collide_with(&self, _other: &dyn Collidable) -> bool {
        true
    }
}

// ---------------------------------------------------------------------------
// Force source
// ---------------------------------------------------------------------------

/// Trait for objects that apply forces to bodies (gravity fields, springs, etc.).
pub trait ForceSource {
    /// Compute the force (and optionally torque) acting on a body at the
    /// given transform with the given velocity.
    ///
    /// Returns `(force, torque)` both as `[f64; 3]` in world space.
    fn force_at(
        &self,
        transform: &Transform,
        velocity: [Real; 3],
        mass: Real,
    ) -> ([Real; 3], [Real; 3]);
}

// ---------------------------------------------------------------------------
// Constraint
// ---------------------------------------------------------------------------

/// Trait for position/velocity-level constraints between bodies.
pub trait PhysicsConstraint {
    /// Number of degrees of freedom constrained (1–6).
    fn dof(&self) -> usize;

    /// Evaluate the positional violation of this constraint.
    /// Returns a slice of `dof()` scalar violations.
    fn positional_error(&self) -> Vec<Real>;

    /// Whether this constraint is currently active.
    fn is_active(&self) -> bool;

    /// Compliance of the constraint (XPBD softness). 0 = rigid.
    fn compliance(&self) -> Real {
        0.0
    }
}

// ---------------------------------------------------------------------------
// Integrator
// ---------------------------------------------------------------------------

/// Trait for numerical time integrators.
pub trait Integrator {
    /// Advance state `q` (positions) and `v` (velocities) by `dt` given
    /// forces `f`.
    ///
    /// `dof` is the number of degrees of freedom.
    fn integrate(&self, q: &mut [Real], v: &mut [Real], f: &[Real], inv_mass: &[Real], dt: Real);
}

/// Semi-implicit (symplectic) Euler integrator.
///
/// Updates velocity first, then position:
/// ```text
/// v_{n+1} = v_n + (f_n / m) * dt
/// q_{n+1} = q_n + v_{n+1} * dt
/// ```
pub struct SemiImplicitEuler;

impl Integrator for SemiImplicitEuler {
    fn integrate(&self, q: &mut [Real], v: &mut [Real], f: &[Real], inv_mass: &[Real], dt: Real) {
        let n = q.len().min(v.len()).min(f.len()).min(inv_mass.len());
        for i in 0..n {
            v[i] += f[i] * inv_mass[i] * dt;
            q[i] += v[i] * dt;
        }
    }
}

/// Classic explicit (forward) Euler integrator.
///
/// Updates position first using *old* velocity, then updates velocity:
/// ```text
/// q_{n+1} = q_n + v_n * dt
/// v_{n+1} = v_n + (f_n / m) * dt
/// ```
pub struct ExplicitEuler;

impl Integrator for ExplicitEuler {
    fn integrate(&self, q: &mut [Real], v: &mut [Real], f: &[Real], inv_mass: &[Real], dt: Real) {
        let n = q.len().min(v.len()).min(f.len()).min(inv_mass.len());
        for i in 0..n {
            q[i] += v[i] * dt;
            v[i] += f[i] * inv_mass[i] * dt;
        }
    }
}

/// 4th-order Runge-Kutta integrator (position only, given an acceleration field).
///
/// Uses `f` as the acceleration (force/mass) directly.
pub struct RungeKutta4;

impl RungeKutta4 {
    /// Integrate positions `q` with velocities `v` under accelerations `a = f * inv_mass`,
    /// using the classic RK4 scheme.  Both `q` and `v` are updated.
    pub fn integrate_rk4(q: &mut [Real], v: &mut [Real], accel: &[Real], dt: Real) {
        let n = q.len().min(v.len()).min(accel.len());
        let h = dt;
        let h2 = h / 2.0;
        let h6 = h / 6.0;
        // k1 = (v, a)
        // k2 = (v + h/2*a, a)   (constant acceleration assumption)
        // k3 = (v + h/2*a, a)
        // k4 = (v + h*a, a)
        for i in 0..n {
            let a = accel[i];
            // Velocity increments
            let kv1 = a;
            let kv2 = a; // constant force assumption
            let kv3 = a;
            let kv4 = a;
            // Position increments
            let kq1 = v[i];
            let kq2 = v[i] + h2 * kv1;
            let kq3 = v[i] + h2 * kv2;
            let kq4 = v[i] + h * kv3;
            v[i] += h6 * (kv1 + 2.0 * kv2 + 2.0 * kv3 + kv4);
            q[i] += h6 * (kq1 + 2.0 * kq2 + 2.0 * kq3 + kq4);
        }
    }
}

impl Integrator for RungeKutta4 {
    fn integrate(&self, q: &mut [Real], v: &mut [Real], f: &[Real], inv_mass: &[Real], dt: Real) {
        let n = q.len().min(v.len()).min(f.len()).min(inv_mass.len());
        let accel: Vec<Real> = (0..n).map(|i| f[i] * inv_mass[i]).collect();
        Self::integrate_rk4(q, v, &accel, dt);
    }
}

// ---------------------------------------------------------------------------
// Broadphase
// ---------------------------------------------------------------------------

/// Trait for broadphase collision detection structures.
pub trait Broadphase {
    /// Insert an object with the given id and AABB.
    fn insert(&mut self, id: u64, aabb: Aabb);

    /// Update the AABB for an existing object.
    fn update(&mut self, id: u64, aabb: Aabb);

    /// Remove an object.
    fn remove(&mut self, id: u64);

    /// Return all pairs of overlapping ids.
    fn overlapping_pairs(&self) -> Vec<(u64, u64)>;

    /// Return all ids whose AABB overlaps the query AABB.
    fn query_aabb(&self, query: &Aabb) -> Vec<u64>;
}

// ---------------------------------------------------------------------------
// Narrowphase
// ---------------------------------------------------------------------------

/// A contact point produced by narrowphase collision detection.
#[derive(Debug, Clone)]
pub struct ContactPoint {
    /// Contact point in world space.
    pub point: [Real; 3],
    /// Contact normal pointing from body B into body A.
    pub normal: [Real; 3],
    /// Penetration depth (positive = penetrating).
    pub depth: Real,
}

/// Trait for narrowphase collision queries.
pub trait Narrowphase {
    /// Test whether two convex shapes (described by their transforms) overlap.
    fn test_overlap(&self, transform_a: &Transform, transform_b: &Transform) -> bool;

    /// Compute contact points between two shapes.
    fn contacts(&self, transform_a: &Transform, transform_b: &Transform) -> Vec<ContactPoint>;
}

// ---------------------------------------------------------------------------
// Serialisable physics state
// ---------------------------------------------------------------------------

/// Trait for objects whose full physics state can be captured and restored.
pub trait PhysicsStateful {
    /// The type representing the saved state.
    type State;

    /// Capture the current state.
    fn save_state(&self) -> Self::State;

    /// Restore from a previously saved state.
    fn restore_state(&mut self, state: Self::State);
}

// ---------------------------------------------------------------------------
// Damping
// ---------------------------------------------------------------------------

/// Trait for objects that model energy dissipation.
pub trait Damped {
    /// Return the linear damping coefficient.
    fn linear_damping(&self) -> Real;

    /// Return the angular damping coefficient.
    fn angular_damping(&self) -> Real;

    /// Compute damped velocity components from current velocity over `dt`.
    fn damp_velocity(&self, velocity: Real, dt: Real, is_angular: bool) -> Real {
        let d = if is_angular {
            self.angular_damping()
        } else {
            self.linear_damping()
        };
        velocity * (1.0 - d * dt).max(0.0)
    }
}

// ---------------------------------------------------------------------------
// Sleeping / activation
// ---------------------------------------------------------------------------

/// Trait for bodies that can sleep when below activity thresholds.
pub trait Sleepable {
    /// Whether the body is currently sleeping.
    fn is_sleeping(&self) -> bool;

    /// Wake the body from sleep.
    fn wake(&mut self);

    /// Put the body to sleep, zeroing velocities.
    fn sleep(&mut self);

    /// Test whether the body should fall asleep given its speed squared and
    /// the world's thresholds.
    fn should_sleep(
        &self,
        speed_sq: Real,
        threshold_sq: Real,
        accumulated_sleep_time: Real,
        time_to_sleep: Real,
    ) -> bool {
        speed_sq < threshold_sq && accumulated_sleep_time >= time_to_sleep
    }
}

// ---------------------------------------------------------------------------
// Renderer output
// ---------------------------------------------------------------------------

/// Trait for physics objects that can provide render-ready data.
pub trait Renderable {
    /// Return the interpolated world-space transform for rendering at
    /// sub-step fraction `alpha` ∈ \[0,1\].
    fn render_transform(&self, alpha: Real) -> Transform;
}

// ---------------------------------------------------------------------------
// Physics material
// ---------------------------------------------------------------------------

/// Physical surface properties.
#[derive(Debug, Clone)]
pub struct PhysicsMaterial {
    /// Coefficient of static friction.
    pub static_friction: Real,
    /// Coefficient of dynamic friction.
    pub dynamic_friction: Real,
    /// Restitution (bounciness), 0 = perfectly inelastic, 1 = perfectly elastic.
    pub restitution: Real,
    /// Rolling friction coefficient (for spheres/cylinders).
    pub rolling_friction: Real,
}

impl Default for PhysicsMaterial {
    fn default() -> Self {
        Self {
            static_friction: 0.5,
            dynamic_friction: 0.4,
            restitution: 0.3,
            rolling_friction: 0.01,
        }
    }
}

impl PhysicsMaterial {
    /// Create a rubber-like material.
    pub fn rubber() -> Self {
        Self {
            static_friction: 1.0,
            dynamic_friction: 0.8,
            restitution: 0.7,
            rolling_friction: 0.02,
        }
    }

    /// Create a steel-on-steel material.
    pub fn steel() -> Self {
        Self {
            static_friction: 0.74,
            dynamic_friction: 0.57,
            restitution: 0.6,
            rolling_friction: 0.001,
        }
    }

    /// Create a frictionless ice-like material.
    pub fn ice() -> Self {
        Self {
            static_friction: 0.03,
            dynamic_friction: 0.02,
            restitution: 0.05,
            rolling_friction: 0.001,
        }
    }

    /// Combine two materials using geometric mean for friction and min for restitution
    /// (a common convention in game physics).
    pub fn combine(&self, other: &PhysicsMaterial) -> PhysicsMaterial {
        PhysicsMaterial {
            static_friction: (self.static_friction * other.static_friction).sqrt(),
            dynamic_friction: (self.dynamic_friction * other.dynamic_friction).sqrt(),
            restitution: self.restitution.min(other.restitution),
            rolling_friction: (self.rolling_friction * other.rolling_friction).sqrt(),
        }
    }
}

/// Trait for objects that expose a surface material.
pub trait HasMaterial {
    /// Return the physics material for this object's surface.
    fn material(&self) -> &PhysicsMaterial;
}

// ---------------------------------------------------------------------------
// Debug / diagnostics
// ---------------------------------------------------------------------------

/// Trait for physics objects that can emit diagnostic information.
pub trait PhysicsDiagnostics {
    /// Return a vector of (name, value) pairs for debug display.
    fn diagnostics(&self) -> Vec<(&'static str, Real)>;
}

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

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

    use crate::VelocityVerlet;

    use crate::fabrik_solve;

    // ── SemiImplicitEuler ─────────────────────────────────────────────────────

    #[test]
    fn test_semi_implicit_euler_constant_force() {
        let integrator = SemiImplicitEuler;
        let mut q = [0.0f64];
        let mut v = [0.0f64];
        let f = [1.0f64]; // constant force
        let inv_mass = [1.0f64]; // mass = 1 kg
        integrator.integrate(&mut q, &mut v, &f, &inv_mass, 1.0);
        // v = 0 + 1*1 = 1; q = 0 + 1*1 = 1
        assert!((v[0] - 1.0).abs() < 1e-12, "v={}", v[0]);
        assert!((q[0] - 1.0).abs() < 1e-12, "q={}", q[0]);
    }

    #[test]
    fn test_semi_implicit_euler_zero_force() {
        let integrator = SemiImplicitEuler;
        let mut q = [5.0f64];
        let mut v = [2.0f64];
        let f = [0.0f64];
        let inv_mass = [1.0f64];
        integrator.integrate(&mut q, &mut v, &f, &inv_mass, 0.5);
        // v unchanged = 2; q = 5 + 2*0.5 = 6
        assert!((v[0] - 2.0).abs() < 1e-12);
        assert!((q[0] - 6.0).abs() < 1e-12);
    }

    // ── ExplicitEuler ─────────────────────────────────────────────────────────

    #[test]
    fn test_explicit_euler_differs_from_semi_implicit() {
        // With a non-zero force the two integrators give different results.
        let f = [2.0f64];
        let inv_mass = [1.0f64];
        let dt = 1.0;

        let mut q_si = [0.0f64];
        let mut v_si = [0.0f64];
        SemiImplicitEuler.integrate(&mut q_si, &mut v_si, &f, &inv_mass, dt);

        let mut q_ex = [0.0f64];
        let mut v_ex = [0.0f64];
        ExplicitEuler.integrate(&mut q_ex, &mut v_ex, &f, &inv_mass, dt);

        // Semi-implicit: v=2, q=2; Explicit: q=0 first, v=2
        assert!((v_si[0] - 2.0).abs() < 1e-12);
        assert!((q_si[0] - 2.0).abs() < 1e-12);
        assert!((v_ex[0] - 2.0).abs() < 1e-12);
        assert!((q_ex[0] - 0.0).abs() < 1e-12); // position integrated with OLD v=0
    }

    // ── RungeKutta4 ───────────────────────────────────────────────────────────

    #[test]
    fn test_rk4_constant_acceleration() {
        // Under constant acceleration a=2, starting from rest:
        // q(dt) = ½*a*dt², v(dt) = a*dt
        let mut q = [0.0f64];
        let mut v = [0.0f64];
        let a = [2.0f64];
        let inv_mass = [1.0f64];
        RungeKutta4.integrate(&mut q, &mut v, &a, &inv_mass, 1.0);
        assert!((v[0] - 2.0).abs() < 1e-10, "v={}", v[0]);
        // RK4 with constant a gives q = v0*dt + a*dt²/2 (exact for const a)
        assert!((q[0] - 1.0).abs() < 1e-10, "q={}", q[0]);
    }

    #[test]
    fn test_rk4_with_initial_velocity() {
        let mut q = [0.0f64];
        let mut v = [3.0f64];
        let a = [0.0f64]; // no force
        let inv_mass = [1.0f64];
        RungeKutta4.integrate(&mut q, &mut v, &a, &inv_mass, 2.0);
        // q = 0 + 3*2 = 6, v = 3 unchanged
        assert!((q[0] - 6.0).abs() < 1e-10, "q={}", q[0]);
        assert!((v[0] - 3.0).abs() < 1e-10, "v={}", v[0]);
    }

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

    #[test]
    fn test_material_combine_friction_geometric_mean() {
        let a = PhysicsMaterial {
            static_friction: 4.0,
            dynamic_friction: 4.0,
            restitution: 0.5,
            rolling_friction: 0.01,
        };
        let b = PhysicsMaterial {
            static_friction: 1.0,
            dynamic_friction: 1.0,
            restitution: 0.8,
            rolling_friction: 0.01,
        };
        let c = a.combine(&b);
        // geometric mean of 4 and 1 = 2
        assert!((c.static_friction - 2.0).abs() < 1e-12);
        assert!((c.dynamic_friction - 2.0).abs() < 1e-12);
        // min restitution
        assert!((c.restitution - 0.5).abs() < 1e-12);
    }

    #[test]
    fn test_material_presets_valid() {
        let rubber = PhysicsMaterial::rubber();
        assert!(rubber.static_friction > 0.0);
        assert!(rubber.restitution <= 1.0);

        let steel = PhysicsMaterial::steel();
        assert!(steel.dynamic_friction > 0.0);

        let ice = PhysicsMaterial::ice();
        assert!(ice.static_friction < 0.1);
    }

    // ── Damped via PhysicsMaterial-like manual impl ───────────────────────────

    struct SimpleDamped {
        lin: Real,
        ang: Real,
    }
    impl Damped for SimpleDamped {
        fn linear_damping(&self) -> Real {
            self.lin
        }
        fn angular_damping(&self) -> Real {
            self.ang
        }
    }

    #[test]
    fn test_damped_velocity() {
        let d = SimpleDamped { lin: 0.5, ang: 0.2 };
        let v_after = d.damp_velocity(10.0, 1.0, false);
        // factor = 1 - 0.5*1 = 0.5; v = 10 * 0.5 = 5
        assert!((v_after - 5.0).abs() < 1e-12, "v={}", v_after);
    }

    #[test]
    fn test_damped_velocity_clamp_zero() {
        let d = SimpleDamped { lin: 2.0, ang: 0.0 };
        // factor = 1 - 2.0*1.0 = -1 → clamped to 0
        let v_after = d.damp_velocity(10.0, 1.0, false);
        assert_eq!(v_after, 0.0);
    }

    // ── Sleepable via manual impl ─────────────────────────────────────────────

    #[test]
    fn test_sleepable_should_sleep() {
        struct DummySleepable;
        impl Sleepable for DummySleepable {
            fn is_sleeping(&self) -> bool {
                false
            }
            fn wake(&mut self) {}
            fn sleep(&mut self) {}
        }
        let s = DummySleepable;
        assert!(s.should_sleep(0.0001, 0.01, 1.0, 0.5));
        assert!(!s.should_sleep(0.1, 0.01, 1.0, 0.5)); // too fast
        assert!(!s.should_sleep(0.0001, 0.01, 0.1, 0.5)); // not long enough
    }

    // ── ContactPoint ─────────────────────────────────────────────────────────

    #[test]
    fn test_contact_point_construction() {
        let cp = ContactPoint {
            point: [1.0, 2.0, 3.0],
            normal: [0.0, 1.0, 0.0],
            depth: 0.05,
        };
        assert!((cp.depth - 0.05).abs() < 1e-12);
        assert_eq!(cp.normal[1], 1.0);
    }

    // ── Integrator length mismatch safety ────────────────────────────────────

    #[test]
    fn test_integrator_length_mismatch_safe() {
        // Should not panic when arrays have different lengths.
        let integrator = SemiImplicitEuler;
        let mut q = vec![0.0, 0.0, 0.0];
        let mut v = vec![1.0, 1.0];
        let f = vec![0.0, 0.0, 0.0];
        let inv_mass = vec![1.0, 1.0, 1.0];
        // min length is 2; should process only 2 elements without panic.
        integrator.integrate(&mut q, &mut v, &f, &inv_mass, 0.1);
        assert_eq!(q[2], 0.0); // third element untouched
    }

    // ── Velocity Verlet ───────────────────────────────────────────────────────

    #[test]
    fn test_velocity_verlet_constant_accel() {
        let mut q = [0.0f64];
        let mut v = [0.0f64];
        let a = [2.0f64];
        let dt = 1.0;
        VelocityVerlet.integrate(&mut q, &mut v, &a, &[1.0], dt);
        // q = 0 + 0*1 + 0.5*2*1 = 1; v = 0 + 2*1 = 2
        assert!((q[0] - 1.0).abs() < 1e-12, "q={}", q[0]);
        assert!((v[0] - 2.0).abs() < 1e-12, "v={}", v[0]);
    }

    // ── Leapfrog ──────────────────────────────────────────────────────────────

    #[test]
    fn test_leapfrog_energy_conservation() {
        // For a harmonic oscillator x'' = -x, energy E = v²/2 + x²/2 is nearly
        // conserved by the leapfrog integrator over many steps.
        let omega2 = 1.0f64; // spring constant / mass = 1
        let dt = 0.01;
        let n_steps = 1000;
        let mut x = 1.0f64;
        let mut v = 0.0f64;
        // Kick-start half-step for leapfrog
        let a0 = -omega2 * x;
        let mut v_half = v + 0.5 * a0 * dt;
        let e0 = 0.5 * v * v + 0.5 * x * x;
        for _ in 0..n_steps {
            x += v_half * dt;
            let a = -omega2 * x;
            v_half += a * dt;
            v = v_half - 0.5 * a * dt;
        }
        let e_final = 0.5 * v * v + 0.5 * x * x;
        // Leapfrog is symplectic; energy oscillates but does not drift.
        assert!((e_final - e0).abs() < 0.01, "E0={e0}, Ef={e_final}");
    }

    // ── VelocityVerlet multi-step ─────────────────────────────────────────────

    #[test]
    fn test_velocity_verlet_free_fall() {
        // Under constant gravity g = -9.81, starting from rest:
        // y(t) = -1/2 g t²
        let g = -9.81f64;
        let dt = 0.001;
        let n = 1000;
        let mut y = 0.0f64;
        let mut vy = 0.0f64;
        let mut ay = g;
        for _ in 0..n {
            y += vy * dt + 0.5 * ay * dt * dt;
            let ay_new = g;
            vy += 0.5 * (ay + ay_new) * dt;
            ay = ay_new;
        }
        let t = dt * n as f64;
        let expected = 0.5 * g * t * t;
        assert!((y - expected).abs() < 1e-8, "y={y}, expected={expected}");
    }

    // ── Runge-Kutta adaptive ──────────────────────────────────────────────────

    #[test]
    fn test_rkf45_step_count_positive() {
        // RKF45 step for a simple linear ODE
        let mut y = [1.0f64];
        let a = [0.0f64];
        let inv_mass = [1.0f64];
        RungeKutta4.integrate(&mut y, &mut a.clone(), &a, &inv_mass, 0.1);
        assert!(y[0].is_finite());
    }

    // ── PhysicsMaterial edge cases ────────────────────────────────────────────

    #[test]
    fn test_material_combine_zero_friction() {
        let a = PhysicsMaterial {
            static_friction: 0.0,
            dynamic_friction: 0.0,
            restitution: 0.5,
            rolling_friction: 0.0,
        };
        let b = PhysicsMaterial::rubber();
        let c = a.combine(&b);
        // sqrt(0 * x) = 0
        assert_eq!(c.static_friction, 0.0);
        assert_eq!(c.dynamic_friction, 0.0);
    }

    #[test]
    fn test_material_combine_min_restitution() {
        let a = PhysicsMaterial {
            restitution: 0.9,
            ..PhysicsMaterial::default()
        };
        let b = PhysicsMaterial {
            restitution: 0.1,
            ..PhysicsMaterial::default()
        };
        let c = a.combine(&b);
        assert!((c.restitution - 0.1).abs() < 1e-12);
    }

    // ── InverseKinematics trait (FABRIK) ──────────────────────────────────────

    #[test]
    fn test_fabrik_reaches_target_1dof() {
        // 2-joint arm (2 bones) with total reach = 2.0; target at distance 1.0 → reachable
        let bones = vec![[0.0f64, 0.0, 0.0], [1.0, 0.0, 0.0], [2.0, 0.0, 0.0]];
        let lengths = vec![1.0f64, 1.0];
        let target = [1.0, 0.5, 0.0]; // within reach
        let result = fabrik_solve(&bones, &lengths, &target, 50, 1e-6);
        // end effector should be at target
        let ee = result.last().unwrap();
        let d = ((ee[0] - target[0]).powi(2)
            + (ee[1] - target[1]).powi(2)
            + (ee[2] - target[2]).powi(2))
        .sqrt();
        assert!(d < 0.01, "FABRIK end effector distance to target: {d}");
    }

    #[test]
    fn test_fabrik_unreachable_extends_toward_target() {
        // 1-joint arm with reach 1.0; target at distance 3.0 → unreachable
        let bones = vec![[0.0f64, 0.0, 0.0], [1.0, 0.0, 0.0]];
        let lengths = vec![1.0f64];
        let target = [3.0, 0.0, 0.0];
        let result = fabrik_solve(&bones, &lengths, &target, 20, 1e-6);
        // End effector should be pointing toward target
        let ee = result.last().unwrap();
        assert!(
            ee[0] > 0.9,
            "end effector should extend toward target: x={}",
            ee[0]
        );
    }
}

// ---------------------------------------------------------------------------
// Velocity Verlet integrator
// ---------------------------------------------------------------------------

/// Velocity Verlet integration scheme.
///
/// Updates positions using both current and next accelerations:
/// ```text
/// q_{n+1} = q_n + v_n * dt + 0.5 * a_n * dt²
/// v_{n+1} = v_n + 0.5 * (a_n + a_{n+1}) * dt
/// ```
/// Since `a_{n+1}` is not generally available (forces depend on positions),
/// this implementation uses `a_n` for both terms, equivalent to the
/// "Störmer-Verlet" update.
pub struct VelocityVerlet;

impl Integrator for VelocityVerlet {
    fn integrate(&self, q: &mut [Real], v: &mut [Real], f: &[Real], inv_mass: &[Real], dt: Real) {
        let n = q.len().min(v.len()).min(f.len()).min(inv_mass.len());
        let dt2 = dt * dt;
        for i in 0..n {
            let a = f[i] * inv_mass[i];
            q[i] += v[i] * dt + 0.5 * a * dt2;
            v[i] += a * dt;
        }
    }
}

// ---------------------------------------------------------------------------
// Leapfrog integrator
// ---------------------------------------------------------------------------

/// Leapfrog (Störmer-Verlet) integration.
///
/// The leapfrog scheme stores velocities at half-integer time steps:
/// ```text
/// v_{n+1/2} = v_{n-1/2} + a_n * dt
/// q_{n+1}   = q_n + v_{n+1/2} * dt
/// ```
/// This implementation initialises the half-step velocity from the
/// full-step velocity on first call, making it self-starting.
pub struct Leapfrog;

impl Integrator for Leapfrog {
    fn integrate(&self, q: &mut [Real], v: &mut [Real], f: &[Real], inv_mass: &[Real], dt: Real) {
        let n = q.len().min(v.len()).min(f.len()).min(inv_mass.len());
        for i in 0..n {
            let a = f[i] * inv_mass[i];
            // Kick: advance velocity by half-step
            let v_half = v[i] + a * dt * 0.5;
            // Drift: advance position
            q[i] += v_half * dt;
            // Kick: advance velocity to full step
            v[i] = v_half + a * dt * 0.5;
        }
    }
}

// ---------------------------------------------------------------------------
// Adams-Bashforth 2nd-order integrator
// ---------------------------------------------------------------------------

/// Adams-Bashforth 2-step explicit integrator.
///
/// Requires the acceleration from the previous step (`f_prev`).  When
/// `f_prev` is `None` the method falls back to a single Euler step.
pub struct AdamsBashforth2 {
    /// Stored acceleration from the previous step (optional first-step bootstrap).
    pub f_prev: Option<Vec<Real>>,
}

impl AdamsBashforth2 {
    /// Create a new Adams-Bashforth 2-step integrator.
    pub fn new() -> Self {
        Self { f_prev: None }
    }

    /// Reset the stored previous-step forces.
    pub fn reset(&mut self) {
        self.f_prev = None;
    }

    /// Integrate state using the 2-step formula.
    pub fn integrate_ab2(
        &mut self,
        q: &mut [Real],
        v: &mut [Real],
        f: &[Real],
        inv_mass: &[Real],
        dt: Real,
    ) {
        let n = q.len().min(v.len()).min(f.len()).min(inv_mass.len());
        match &self.f_prev {
            None => {
                // Bootstrap with forward Euler.
                for i in 0..n {
                    let a = f[i] * inv_mass[i];
                    v[i] += a * dt;
                    q[i] += v[i] * dt;
                }
            }
            Some(fp) => {
                let m = fp.len().min(n);
                for i in 0..m {
                    let a_curr = f[i] * inv_mass[i];
                    let a_prev = fp[i] * inv_mass[i];
                    // AB2: v_{n+1} = v_n + dt*(3/2 * a_n - 1/2 * a_{n-1})
                    let a_eff = 1.5 * a_curr - 0.5 * a_prev;
                    v[i] += a_eff * dt;
                    q[i] += v[i] * dt;
                }
            }
        }
        self.f_prev = Some(f[..n].to_vec());
    }
}

impl Default for AdamsBashforth2 {
    fn default() -> Self {
        Self::new()
    }
}

impl Integrator for AdamsBashforth2 {
    fn integrate(&self, q: &mut [Real], v: &mut [Real], f: &[Real], inv_mass: &[Real], dt: Real) {
        // Stateless fallback: just use Euler.
        let n = q.len().min(v.len()).min(f.len()).min(inv_mass.len());
        for i in 0..n {
            let a = f[i] * inv_mass[i];
            v[i] += a * dt;
            q[i] += v[i] * dt;
        }
    }
}

// ---------------------------------------------------------------------------
// Implicit (backward) Euler integrator
// ---------------------------------------------------------------------------

/// Implicit (backward) Euler integrator — unconditionally stable.
///
/// For linear systems `F = k * x` the implicit update solves:
/// ```text
/// v_{n+1} = v_n + a_{n+1} * dt   (where a_{n+1} uses new position)
/// q_{n+1} = q_n + v_{n+1} * dt
/// ```
/// In the absence of position-dependent forces this reduces to the
/// semi-implicit Euler integrator.
pub struct ImplicitEuler;

impl Integrator for ImplicitEuler {
    fn integrate(&self, q: &mut [Real], v: &mut [Real], f: &[Real], inv_mass: &[Real], dt: Real) {
        // For constant forces this is the same as semi-implicit Euler.
        let n = q.len().min(v.len()).min(f.len()).min(inv_mass.len());
        for i in 0..n {
            v[i] += f[i] * inv_mass[i] * dt;
            q[i] += v[i] * dt;
        }
    }
}

// ---------------------------------------------------------------------------
// Midpoint (RK2) integrator
// ---------------------------------------------------------------------------

/// Explicit midpoint (Runge-Kutta 2) integrator.
///
/// ```text
/// k1_v = a_n,  k1_q = v_n
/// k2_v = a_n,  k2_q = v_n + k1_v * dt/2
/// v_{n+1} = v_n + k2_v * dt
/// q_{n+1} = q_n + k2_q * dt
/// ```
/// For constant forces this gives the same result as the explicit Euler
/// update for velocity but uses the midpoint velocity for position.
pub struct Midpoint;

impl Integrator for Midpoint {
    fn integrate(&self, q: &mut [Real], v: &mut [Real], f: &[Real], inv_mass: &[Real], dt: Real) {
        let n = q.len().min(v.len()).min(f.len()).min(inv_mass.len());
        let h2 = dt / 2.0;
        for i in 0..n {
            let a = f[i] * inv_mass[i];
            let v_mid = v[i] + a * h2;
            v[i] += a * dt;
            q[i] += v_mid * dt;
        }
    }
}

// ---------------------------------------------------------------------------
// Constraint solver helpers
// ---------------------------------------------------------------------------

/// XPBD position-level constraint correction for a distance constraint.
///
/// Corrects the positions of two bodies so that the distance between
/// `p_a` and `p_b` equals `rest_length`, using XPBD compliance.
///
/// Returns the position deltas `(delta_a, delta_b)` for each body.
///
/// # Arguments
/// * `p_a` / `p_b` — current world-space positions of each body's anchor
/// * `inv_mass_a` / `inv_mass_b` — inverse masses of each body
/// * `rest_length` — target separation distance
/// * `compliance` — XPBD compliance (= 1 / stiffness), 0 = rigid
/// * `dt` — substep duration
pub fn xpbd_distance_correction(
    p_a: [Real; 3],
    p_b: [Real; 3],
    inv_mass_a: Real,
    inv_mass_b: Real,
    rest_length: Real,
    compliance: Real,
    dt: Real,
) -> ([Real; 3], [Real; 3]) {
    let dx = [p_b[0] - p_a[0], p_b[1] - p_a[1], p_b[2] - p_a[2]];
    let dist = (dx[0] * dx[0] + dx[1] * dx[1] + dx[2] * dx[2]).sqrt();
    if dist < 1e-12 {
        return ([0.0; 3], [0.0; 3]);
    }
    let n = [dx[0] / dist, dx[1] / dist, dx[2] / dist];
    let c = dist - rest_length;
    let alpha = compliance / (dt * dt);
    let w = inv_mass_a + inv_mass_b;
    if w < 1e-12 {
        return ([0.0; 3], [0.0; 3]);
    }
    let lambda = -c / (w + alpha);
    let da = [
        -lambda * inv_mass_a * n[0],
        -lambda * inv_mass_a * n[1],
        -lambda * inv_mass_a * n[2],
    ];
    let db = [
        lambda * inv_mass_b * n[0],
        lambda * inv_mass_b * n[1],
        lambda * inv_mass_b * n[2],
    ];
    (da, db)
}

/// Velocity-level constraint impulse for a contact constraint (Baumgarte-free).
///
/// Resolves relative normal velocity at a contact point given restitution.
/// Returns the scalar impulse magnitude.
///
/// # Arguments
/// * `v_rel_n` — relative normal velocity (vB − vA) · normal
/// * `inv_mass_a` / `inv_mass_b` — inverse masses
/// * `inv_inertia_a` / `inv_inertia_b` — inverse inertia scalars
/// * `r_a` / `r_b` — contact offset vectors (world)
/// * `normal` — contact normal (unit vector)
/// * `restitution` — coefficient of restitution
#[allow(clippy::too_many_arguments)]
pub fn contact_impulse_scalar(
    v_rel_n: Real,
    inv_mass_a: Real,
    inv_mass_b: Real,
    inv_inertia_a: Real,
    inv_inertia_b: Real,
    r_a: [Real; 3],
    r_b: [Real; 3],
    normal: [Real; 3],
) -> Real {
    // Effective mass along normal including angular terms.
    fn cross_sq(r: [Real; 3], n: [Real; 3]) -> Real {
        let cx = r[1] * n[2] - r[2] * n[1];
        let cy = r[2] * n[0] - r[0] * n[2];
        let cz = r[0] * n[1] - r[1] * n[0];
        cx * cx + cy * cy + cz * cz
    }
    let k = inv_mass_a
        + inv_mass_b
        + inv_inertia_a * cross_sq(r_a, normal)
        + inv_inertia_b * cross_sq(r_b, normal);
    if k < 1e-12 { 0.0 } else { -v_rel_n / k }
}

// ---------------------------------------------------------------------------
// FABRIK inverse kinematics
// ---------------------------------------------------------------------------

/// FABRIK (Forward And Backward Reaching Inverse Kinematics) solver.
///
/// Solves an n-joint chain to place the end-effector at `target`.
///
/// # Arguments
/// * `bones` — initial positions of each joint (n+1 points for n segments)
/// * `lengths` — bone lengths (must have length == bones.len() - 1)
/// * `target` — desired end-effector position
/// * `max_iter` — maximum FABRIK iterations
/// * `tolerance` — convergence threshold (Euclidean distance)
///
/// Returns the updated joint positions.
pub fn fabrik_solve(
    bones: &[[Real; 3]],
    lengths: &[Real],
    target: &[Real; 3],
    max_iter: usize,
    tolerance: Real,
) -> Vec<[Real; 3]> {
    let n = bones.len();
    if n < 2 || lengths.len() < n - 1 {
        return bones.to_vec();
    }
    let mut joints: Vec<[Real; 3]> = bones.to_vec();
    let root = joints[0];
    let total_length: Real = lengths.iter().sum();

    // Check if target is reachable
    let dist_to_target = dist3(&joints[0], target);
    if dist_to_target >= total_length {
        // Stretch toward target
        for i in 0..n - 1 {
            let d = dist3(&joints[i], target);
            let t = lengths[i] / d;
            joints[i + 1] = lerp3(&joints[i], target, t);
        }
        return joints;
    }

    for _ in 0..max_iter {
        // Forward pass: start from end effector
        joints[n - 1] = *target;
        for i in (0..n - 1).rev() {
            let d = dist3(&joints[i + 1], &joints[i]);
            if d < 1e-12 {
                continue;
            }
            let t = lengths[i] / d;
            joints[i] = lerp3(&joints[i + 1], &joints[i], t);
        }

        // Backward pass: start from root
        joints[0] = root;
        for i in 0..n - 1 {
            let d = dist3(&joints[i], &joints[i + 1]);
            if d < 1e-12 {
                continue;
            }
            let t = lengths[i] / d;
            joints[i + 1] = lerp3(&joints[i], &joints[i + 1], t);
        }

        // Check convergence
        let ee_dist = dist3(&joints[n - 1], target);
        if ee_dist < tolerance {
            break;
        }
    }
    joints
}

/// Euclidean distance between two 3D points.
fn dist3(a: &[Real; 3], b: &[Real; 3]) -> Real {
    let dx = b[0] - a[0];
    let dy = b[1] - a[1];
    let dz = b[2] - a[2];
    (dx * dx + dy * dy + dz * dz).sqrt()
}

/// Linear interpolation between two 3D points.
fn lerp3(a: &[Real; 3], b: &[Real; 3], t: Real) -> [Real; 3] {
    [
        a[0] + t * (b[0] - a[0]),
        a[1] + t * (b[1] - a[1]),
        a[2] + t * (b[2] - a[2]),
    ]
}

// ---------------------------------------------------------------------------
// Position-based dynamics (PBD) constraint solver
// ---------------------------------------------------------------------------

/// Trait for PBD position constraints.
pub trait PbdConstraint {
    /// Evaluate constraint C(p) — returns violation (0 = satisfied).
    fn evaluate(&self, positions: &[[Real; 3]]) -> Real;

    /// Compute constraint gradient ∇C with respect to each particle position.
    fn gradient(&self, positions: &[[Real; 3]]) -> Vec<[Real; 3]>;

    /// Particle indices involved in this constraint.
    fn particle_indices(&self) -> &[usize];

    /// Stiffness in range \[0, 1\] (1 = fully rigid, 0 = no correction).
    fn stiffness(&self) -> Real {
        1.0
    }
}

/// A simple PBD distance constraint between two particles.
pub struct PbdDistanceConstraint {
    /// Indices of the two particles.
    pub indices: [usize; 2],
    /// Rest (target) distance.
    pub rest_length: Real,
    /// Stiffness in \[0, 1\].
    pub k: Real,
}

impl PbdDistanceConstraint {
    /// Create a new distance constraint.
    pub fn new(i: usize, j: usize, rest_length: Real, stiffness: Real) -> Self {
        Self {
            indices: [i, j],
            rest_length,
            k: stiffness,
        }
    }
}

impl PbdConstraint for PbdDistanceConstraint {
    fn evaluate(&self, positions: &[[Real; 3]]) -> Real {
        let [i, j] = self.indices;
        if i >= positions.len() || j >= positions.len() {
            return 0.0;
        }
        let p = &positions[i];
        let q = &positions[j];
        dist3(&[p[0], p[1], p[2]], &[q[0], q[1], q[2]]) - self.rest_length
    }

    fn gradient(&self, positions: &[[Real; 3]]) -> Vec<[Real; 3]> {
        let [i, j] = self.indices;
        if i >= positions.len() || j >= positions.len() {
            return vec![[0.0; 3]; 2];
        }
        let p = positions[i];
        let q = positions[j];
        let dx = [q[0] - p[0], q[1] - p[1], q[2] - p[2]];
        let d = (dx[0] * dx[0] + dx[1] * dx[1] + dx[2] * dx[2]).sqrt();
        if d < 1e-12 {
            return vec![[0.0; 3]; 2];
        }
        let n = [dx[0] / d, dx[1] / d, dx[2] / d];
        // ∇C_i = -n, ∇C_j = +n
        vec![[-n[0], -n[1], -n[2]], [n[0], n[1], n[2]]]
    }

    fn particle_indices(&self) -> &[usize] {
        &self.indices
    }

    fn stiffness(&self) -> Real {
        self.k
    }
}

/// Solve one PBD iteration over a set of constraints.
///
/// Updates `positions` in-place by applying each constraint correction
/// weighted by stiffness and inverse masses.
pub fn pbd_solve_iteration(
    positions: &mut Vec<[Real; 3]>,
    inv_masses: &[Real],
    constraints: &[&dyn PbdConstraint],
) {
    for c in constraints {
        let c_val = c.evaluate(positions);
        let grads = c.gradient(positions);
        let indices = c.particle_indices();
        if grads.len() < indices.len() {
            continue;
        }
        // Compute denominator: sum of w_i * |∇C_i|²
        let mut denom = 0.0;
        for (k, &idx) in indices.iter().enumerate() {
            if idx < inv_masses.len() {
                let g = grads[k];
                denom += inv_masses[idx] * (g[0] * g[0] + g[1] * g[1] + g[2] * g[2]);
            }
        }
        if denom < 1e-12 {
            continue;
        }
        let lambda = -c_val / denom * c.stiffness();
        for (k, &idx) in indices.iter().enumerate() {
            if idx < positions.len() && idx < inv_masses.len() {
                let g = grads[k];
                let w = inv_masses[idx];
                positions[idx][0] += lambda * w * g[0];
                positions[idx][1] += lambda * w * g[1];
                positions[idx][2] += lambda * w * g[2];
            }
        }
    }
}

// ---------------------------------------------------------------------------
// Collision filter trait
// ---------------------------------------------------------------------------

/// Trait for filtering which body pairs participate in collision detection.
pub trait CollisionFilter {
    /// Return `true` if the pair `(id_a, id_b)` should be tested for collision.
    fn should_collide(&self, id_a: u64, id_b: u64) -> bool;
}

/// A simple bitmask-based collision filter.
///
/// Body A collides with body B only if `mask_a & layer_b != 0` AND
/// `mask_b & layer_a != 0`.
pub struct LayerFilter {
    /// Collision layer bits for each body id.
    pub layers: std::collections::HashMap<u64, u32>,
    /// Collision mask bits for each body id.
    pub masks: std::collections::HashMap<u64, u32>,
}

impl LayerFilter {
    /// Create an empty layer filter.
    pub fn new() -> Self {
        Self {
            layers: std::collections::HashMap::new(),
            masks: std::collections::HashMap::new(),
        }
    }

    /// Register a body with its layer and mask.
    pub fn register(&mut self, id: u64, layer: u32, mask: u32) {
        self.layers.insert(id, layer);
        self.masks.insert(id, mask);
    }
}

impl Default for LayerFilter {
    fn default() -> Self {
        Self::new()
    }
}

impl CollisionFilter for LayerFilter {
    fn should_collide(&self, id_a: u64, id_b: u64) -> bool {
        let layer_a = self.layers.get(&id_a).copied().unwrap_or(0xFFFF_FFFF);
        let layer_b = self.layers.get(&id_b).copied().unwrap_or(0xFFFF_FFFF);
        let mask_a = self.masks.get(&id_a).copied().unwrap_or(0xFFFF_FFFF);
        let mask_b = self.masks.get(&id_b).copied().unwrap_or(0xFFFF_FFFF);
        (mask_a & layer_b) != 0 && (mask_b & layer_a) != 0
    }
}

// ---------------------------------------------------------------------------
// Sensor trait
// ---------------------------------------------------------------------------

/// Trait for physics sensors (trigger volumes, proximity detectors, etc.).
pub trait PhysicsSensor {
    /// Return `true` if the sensor is currently triggered by any body.
    fn is_triggered(&self) -> bool;

    /// Return the IDs of all bodies currently overlapping this sensor.
    fn overlapping_bodies(&self) -> Vec<u64>;

    /// Called each frame to update the sensor's overlap state.
    fn update(&mut self, body_positions: &[(u64, [Real; 3])]);
}

/// A spherical proximity sensor.
pub struct SphereSensor {
    /// World-space center.
    pub center: [Real; 3],
    /// Trigger radius.
    pub radius: Real,
    /// Currently overlapping body IDs.
    pub current_overlaps: Vec<u64>,
}

impl SphereSensor {
    /// Create a new sphere sensor.
    pub fn new(center: [Real; 3], radius: Real) -> Self {
        Self {
            center,
            radius,
            current_overlaps: Vec::new(),
        }
    }
}

impl PhysicsSensor for SphereSensor {
    fn is_triggered(&self) -> bool {
        !self.current_overlaps.is_empty()
    }

    fn overlapping_bodies(&self) -> Vec<u64> {
        self.current_overlaps.clone()
    }

    fn update(&mut self, body_positions: &[(u64, [Real; 3])]) {
        let r2 = self.radius * self.radius;
        self.current_overlaps = body_positions
            .iter()
            .filter_map(|(id, pos)| {
                let dx = pos[0] - self.center[0];
                let dy = pos[1] - self.center[1];
                let dz = pos[2] - self.center[2];
                if dx * dx + dy * dy + dz * dz <= r2 {
                    Some(*id)
                } else {
                    None
                }
            })
            .collect();
    }
}

// ---------------------------------------------------------------------------
// Interpolation trait for physics state
// ---------------------------------------------------------------------------

/// Trait for physics state objects that support linear interpolation.
pub trait PhysicsInterpolatable: Sized {
    /// Linearly interpolate between `self` and `other` at blend factor `t ∈ [0,1]`.
    fn lerp_state(&self, other: &Self, t: Real) -> Self;
}

// ---------------------------------------------------------------------------
// Extra tests for expanded traits
// ---------------------------------------------------------------------------

#[cfg(test)]
mod expanded_tests {
    use super::*;
    use crate::AdamsBashforth2;
    use crate::LayerFilter;
    use crate::Leapfrog;
    use crate::Midpoint;
    use crate::PbdConstraint;
    use crate::PbdDistanceConstraint;
    use crate::SphereSensor;
    use crate::VelocityVerlet;
    use crate::dist3;
    use crate::fabrik_solve;
    use crate::pbd_solve_iteration;
    use crate::xpbd_distance_correction;

    // ── VelocityVerlet ────────────────────────────────────────────────────────

    #[test]
    fn test_velocity_verlet_no_force() {
        let mut q = [3.0f64];
        let mut v = [2.0f64];
        VelocityVerlet.integrate(&mut q, &mut v, &[0.0], &[1.0], 0.5);
        // q = 3 + 2*0.5 + 0 = 4; v = 2
        assert!((q[0] - 4.0).abs() < 1e-12, "q={}", q[0]);
        assert!((v[0] - 2.0).abs() < 1e-12, "v={}", v[0]);
    }

    // ── Leapfrog ──────────────────────────────────────────────────────────────

    #[test]
    fn test_leapfrog_rest() {
        let mut q = [1.0f64];
        let mut v = [0.0f64];
        Leapfrog.integrate(&mut q, &mut v, &[0.0], &[1.0], 1.0);
        assert!(
            (q[0] - 1.0).abs() < 1e-12,
            "q should not change: q={}",
            q[0]
        );
    }

    // ── Midpoint ──────────────────────────────────────────────────────────────

    #[test]
    fn test_midpoint_position_uses_midpoint_velocity() {
        let mut q = [0.0f64];
        let mut v = [0.0f64];
        // a = 4.0; v_mid = 0 + 4*0.5*0.5 = 1; q = 0 + 1*0.5*(2?) wait let dt=1
        // a=4, h2=0.5; v_mid = 0 + 4*0.5 = 2; q = 0 + 2*1 = 2; v = 0 + 4*1 = 4
        Midpoint.integrate(&mut q, &mut v, &[4.0], &[1.0], 1.0);
        assert!((q[0] - 2.0).abs() < 1e-12, "q={}", q[0]);
        assert!((v[0] - 4.0).abs() < 1e-12, "v={}", v[0]);
    }

    // ── Adams-Bashforth 2 ─────────────────────────────────────────────────────

    #[test]
    fn test_ab2_bootstrap_euler() {
        let mut ab2 = AdamsBashforth2::new();
        let mut q = [0.0f64];
        let mut v = [0.0f64];
        ab2.integrate_ab2(&mut q, &mut v, &[2.0], &[1.0], 1.0);
        // First step = Euler: v = 0 + 2*1 = 2; q = 0 + 2*1 = 2
        assert!((v[0] - 2.0).abs() < 1e-12);
        assert!((q[0] - 2.0).abs() < 1e-12);
    }

    #[test]
    fn test_ab2_second_step() {
        let mut ab2 = AdamsBashforth2::new();
        let mut q = [0.0f64];
        let mut v = [0.0f64];
        ab2.integrate_ab2(&mut q, &mut v, &[2.0], &[1.0], 1.0);
        // Step 2: same force; AB2: a_eff = 1.5*2 - 0.5*2 = 2; same as Euler
        ab2.integrate_ab2(&mut q, &mut v, &[2.0], &[1.0], 1.0);
        assert!(v[0].is_finite());
        assert!(q[0].is_finite());
    }

    // ── XPBD distance correction ──────────────────────────────────────────────

    #[test]
    fn test_xpbd_distance_rigid() {
        let pa = [0.0, 0.0, 0.0];
        let pb = [2.0, 0.0, 0.0];
        // rest_length = 1.0 → violation = 1.0; equal masses
        let (da, db) = xpbd_distance_correction(pa, pb, 1.0, 1.0, 1.0, 0.0, 0.01);
        // Bodies should move together: da positive, db negative
        assert!(da[0] > 0.0, "da.x should be positive: {:?}", da);
        assert!(db[0] < 0.0, "db.x should be negative: {:?}", db);
        // Correction magnitudes equal for equal masses
        assert!((da[0].abs() - db[0].abs()).abs() < 1e-10);
    }

    #[test]
    fn test_xpbd_distance_no_violation() {
        let pa = [0.0, 0.0, 0.0];
        let pb = [1.0, 0.0, 0.0];
        let (da, db) = xpbd_distance_correction(pa, pb, 1.0, 1.0, 1.0, 0.0, 0.01);
        assert!(da[0].abs() < 1e-12);
        assert!(db[0].abs() < 1e-12);
    }

    #[test]
    fn test_xpbd_static_body() {
        // inv_mass_b = 0 → body B doesn't move
        let (da, db) =
            xpbd_distance_correction([0.0, 0.0, 0.0], [2.0, 0.0, 0.0], 1.0, 0.0, 1.0, 0.0, 0.01);
        assert!(da[0].abs() > 0.0);
        assert_eq!(db[0], 0.0);
    }

    // ── PBD distance constraint ───────────────────────────────────────────────

    #[test]
    fn test_pbd_distance_constraint_evaluate() {
        let c = PbdDistanceConstraint::new(0, 1, 1.0, 1.0);
        let positions = vec![[0.0, 0.0, 0.0f64], [2.0, 0.0, 0.0]];
        let violation = c.evaluate(&positions);
        assert!((violation - 1.0).abs() < 1e-12, "violation={}", violation);
    }

    #[test]
    fn test_pbd_solve_iteration() {
        let c = PbdDistanceConstraint::new(0, 1, 1.0, 1.0);
        let constraints: Vec<&dyn PbdConstraint> = vec![&c];
        let mut positions = vec![[0.0, 0.0, 0.0f64], [3.0, 0.0, 0.0]];
        let inv_masses = [1.0f64, 1.0];
        pbd_solve_iteration(&mut positions, &inv_masses, &constraints);
        // After correction distance should be closer to 1.0
        let d = (positions[1][0] - positions[0][0]).abs();
        assert!(d < 3.0, "distance should decrease: {d}");
    }

    // ── Layer filter ──────────────────────────────────────────────────────────

    #[test]
    fn test_layer_filter_collision() {
        let mut filter = LayerFilter::new();
        filter.register(1, 0b01, 0b10); // layer 1, collides with layer 2
        filter.register(2, 0b10, 0b01); // layer 2, collides with layer 1
        assert!(filter.should_collide(1, 2));
        assert!(filter.should_collide(2, 1));
    }

    #[test]
    fn test_layer_filter_no_collision() {
        let mut filter = LayerFilter::new();
        filter.register(1, 0b01, 0b01); // layer 1, only collides with layer 1
        filter.register(2, 0b10, 0b10); // layer 2, only collides with layer 2
        assert!(!filter.should_collide(1, 2));
    }

    // ── SphereSensor ──────────────────────────────────────────────────────────

    #[test]
    fn test_sphere_sensor_triggers() {
        let mut sensor = SphereSensor::new([0.0; 3], 5.0);
        let bodies = vec![(1u64, [1.0, 0.0, 0.0]), (2u64, [10.0, 0.0, 0.0])];
        sensor.update(&bodies);
        assert!(sensor.is_triggered());
        assert_eq!(sensor.overlapping_bodies(), vec![1]);
    }

    #[test]
    fn test_sphere_sensor_no_trigger() {
        let mut sensor = SphereSensor::new([0.0; 3], 1.0);
        let bodies = vec![(1u64, [5.0, 0.0, 0.0])];
        sensor.update(&bodies);
        assert!(!sensor.is_triggered());
    }

    // ── FABRIK ────────────────────────────────────────────────────────────────

    #[test]
    fn test_fabrik_2joint_chain() {
        let bones = vec![[0.0, 0.0, 0.0f64], [1.0, 0.0, 0.0], [2.0, 0.0, 0.0]];
        let lengths = vec![1.0f64, 1.0];
        let target = [1.0, 1.0, 0.0];
        let result = fabrik_solve(&bones, &lengths, &target, 50, 1e-6);
        assert_eq!(result.len(), 3);
        // Root should stay fixed
        assert!((result[0][0]).abs() < 1e-10);
        // End effector near target (reachable)
        let ee = result[2];
        let d = ((ee[0] - target[0]).powi(2) + (ee[1] - target[1]).powi(2)).sqrt();
        assert!(d < 0.01, "end effector dist to target: {d}");
    }

    #[test]
    fn test_fabrik_preserves_bone_lengths() {
        let bones = vec![[0.0, 0.0, 0.0f64], [1.0, 0.0, 0.0], [2.0, 0.0, 0.0]];
        let lengths = vec![1.0f64, 1.0];
        let target = [0.5, 0.5, 0.0];
        let result = fabrik_solve(&bones, &lengths, &target, 20, 1e-6);
        // Check that bone lengths are approximately preserved
        for i in 0..2 {
            let d = dist3(result[i], result[i + 1]);
            assert!((d - lengths[i]).abs() < 1e-6, "bone {i} length: {d}");
        }
    }
}