alice-physics 3.1.0

Deterministic physics engine: 128-bit fixed-point rigid bodies with bit-exact, replayable results, typed observation and rollback for search and planning, plus FEM, CFD and 3D-print analysis modules
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
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
//! Integration tests for ALICE-Physics
//!
//! These tests verify end-to-end behaviour of the physics engine using only the
//! public API re-exported from the crate root. All tests run deterministically
//! — no floating-point, no randomness.

use alice_physics::collider::AABB;
use alice_physics::joint::BallJoint;
use alice_physics::{
    bvh::BvhPrimitive, Cloth, CollisionFilter, ContactEventType, DistanceConstraint, Fix128,
    ForceField, ForceFieldInstance, Joint, LinearBvh, PhysicsConfig, PhysicsWorld, QuatFix,
    RigidBody, Rope, SleepConfig, Vec3Fix, Vehicle, VehicleConfig,
};

// ============================================================================
// Helper
// ============================================================================

/// Run a world for `steps` frames with the given `dt`.
fn run_world(world: &mut PhysicsWorld, steps: usize, dt: Fix128) {
    for _ in 0..steps {
        world.step(dt);
    }
}

// ============================================================================
// Test 1 — Free-fall determinism
// ============================================================================

/// A body under gravity should fall. Running the same simulation twice must
/// produce bit-exact identical results (determinism guarantee).
#[test]
fn test_free_fall_determinism() {
    fn simulate() -> Vec3Fix {
        let config = PhysicsConfig::default();
        let mut world = PhysicsWorld::new(config);

        let body = RigidBody::new_dynamic(Vec3Fix::from_int(0, 100, 0), Fix128::ONE);
        world.add_body(body);

        let dt = Fix128::from_ratio(1, 60);
        run_world(&mut world, 60, dt);

        world.bodies[0].position
    }

    let pos1 = simulate();
    let pos2 = simulate();

    // Bit-exact equality — not just "close"
    assert_eq!(pos1.x.hi, pos2.x.hi, "x.hi diverged");
    assert_eq!(pos1.x.lo, pos2.x.lo, "x.lo diverged");
    assert_eq!(pos1.y.hi, pos2.y.hi, "y.hi diverged");
    assert_eq!(pos1.y.lo, pos2.y.lo, "y.lo diverged");
    assert_eq!(pos1.z.hi, pos2.z.hi, "z.hi diverged");
    assert_eq!(pos1.z.lo, pos2.z.lo, "z.lo diverged");

    // The body must have fallen below its starting height
    assert!(
        pos1.y < Fix128::from_int(100),
        "Body did not fall: y = {:?}",
        pos1.y
    );
}

// ============================================================================
// Test 2 — Multi-step bit-exact replay
// ============================================================================

/// Run 120 steps, serialize state, continue 60 more steps, restore, continue
/// 60 more steps — final positions must match.
#[test]
fn test_multi_step_bit_exact() {
    fn make_world() -> PhysicsWorld {
        let config = PhysicsConfig::default();
        let mut world = PhysicsWorld::new(config);

        let body = RigidBody::new_dynamic(Vec3Fix::from_int(3, 50, -7), Fix128::from_ratio(3, 2));
        world.add_body(body);
        world
    }

    let dt = Fix128::from_ratio(1, 60);

    // Run 1: simulate 120 steps then record position
    let mut world_a = make_world();
    run_world(&mut world_a, 120, dt);
    let pos_a = world_a.bodies[0].position;

    // Run 2: identical initial conditions — must match
    let mut world_b = make_world();
    run_world(&mut world_b, 120, dt);
    let pos_b = world_b.bodies[0].position;

    assert_eq!(pos_a.x.hi, pos_b.x.hi);
    assert_eq!(pos_a.x.lo, pos_b.x.lo);
    assert_eq!(pos_a.y.hi, pos_b.y.hi);
    assert_eq!(pos_a.y.lo, pos_b.y.lo);
    assert_eq!(pos_a.z.hi, pos_b.z.hi);
    assert_eq!(pos_a.z.lo, pos_b.z.lo);
}

// ============================================================================
// Test 3 — State serialization / rollback
// ============================================================================

/// Simulate 30 steps, save state, simulate 30 more, restore saved state,
/// simulate 30 more — the two "after-restore" runs must be bit-exact.
#[test]
fn test_state_serialization_rollback() {
    let config = PhysicsConfig::default();
    let mut world = PhysicsWorld::new(config);

    let body = RigidBody::new_dynamic(Vec3Fix::from_int(10, 20, 30), Fix128::ONE);
    world.add_body(body);

    let dt = Fix128::from_ratio(1, 60);

    // Warm-up
    run_world(&mut world, 30, dt);

    // Snapshot
    let snapshot = world.serialize_state();

    // Continue from snapshot — branch A
    run_world(&mut world, 30, dt);
    let pos_branch_a = world.bodies[0].position;

    // Restore and continue — branch B
    world.deserialize_state(&snapshot);
    run_world(&mut world, 30, dt);
    let pos_branch_b = world.bodies[0].position;

    // Both branches from the same snapshot must produce identical results
    assert_eq!(
        pos_branch_a.x.hi, pos_branch_b.x.hi,
        "rollback diverged at x.hi"
    );
    assert_eq!(
        pos_branch_a.y.hi, pos_branch_b.y.hi,
        "rollback diverged at y.hi"
    );
    assert_eq!(
        pos_branch_a.z.hi, pos_branch_b.z.hi,
        "rollback diverged at z.hi"
    );
}

// ============================================================================
// Test 4 — Distance constraint stability
// ============================================================================

/// Two dynamic bodies connected by a distance constraint. After many steps the
/// actual distance between their centres must remain close to the target.
#[test]
fn test_constraint_stability() {
    let config = PhysicsConfig::default();
    let mut world = PhysicsWorld::new(config);

    let target_dist = Fix128::from_int(5);

    let id_a = world.add_body(RigidBody::new_dynamic(
        Vec3Fix::from_int(0, 10, 0),
        Fix128::ONE,
    ));
    let id_b = world.add_body(RigidBody::new_dynamic(
        Vec3Fix::from_int(5, 10, 0),
        Fix128::ONE,
    ));

    let constraint = DistanceConstraint {
        body_a: id_a,
        body_b: id_b,
        local_anchor_a: Vec3Fix::ZERO,
        local_anchor_b: Vec3Fix::ZERO,
        target_distance: target_dist,
        compliance: Fix128::ZERO,
        cached_lambda: Fix128::ZERO,
    };
    world.add_distance_constraint(constraint);

    let dt = Fix128::from_ratio(1, 60);
    run_world(&mut world, 120, dt);

    let pa = world.bodies[id_a].position;
    let pb = world.bodies[id_b].position;
    let actual_dist = (pb - pa).length();

    let error = if actual_dist > target_dist {
        actual_dist - target_dist
    } else {
        target_dist - actual_dist
    };

    // Allow at most 10% error due to gravity pulling both bodies
    let tolerance = Fix128::from_ratio(5, 10); // 0.5 units
    assert!(
        error < tolerance,
        "Constraint violated: dist error = hi={} lo={}",
        error.hi,
        error.lo
    );
}

// ============================================================================
// Test 5 — BVH broad-phase correctness
// ============================================================================

/// Build a BVH with several AABBs and verify that a range query returns exactly
/// the expected hit set.
#[test]
fn test_bvh_query_correctness() {
    // Place 5 unit cubes at integer positions along the X axis
    let prims: Vec<BvhPrimitive> = (0u32..5)
        .map(|i| {
            let x = Fix128::from_int(i64::from(i) * 3); // spaced 3 units apart
            BvhPrimitive {
                aabb: AABB::new(
                    Vec3Fix::new(x, Fix128::ZERO, Fix128::ZERO),
                    Vec3Fix::new(x + Fix128::ONE, Fix128::ONE, Fix128::ONE),
                ),
                index: i,
                morton: 0,
            }
        })
        .collect();

    let bvh = LinearBvh::build(prims);

    // Query covering only the first primitive (x ∈ [0, 1])
    let q = AABB::new(Vec3Fix::from_int(0, 0, 0), Vec3Fix::from_int(1, 1, 1));
    let hits = bvh.query(&q);
    assert!(
        hits.contains(&0),
        "Expected primitive 0 in hit set, got {hits:?}"
    );

    // Query in a gap between primitives — should have no hits
    let gap = AABB::new(Vec3Fix::from_int(1, 0, 0), Vec3Fix::from_int(2, 1, 1));
    let gap_hits = bvh.query(&gap);
    // Primitive 0 ends at x=1 and primitive 1 starts at x=3; the gap [1,2] might
    // touch primitive 0 depending on the integer rounding in the node.  We only
    // assert primitives 2-4 are absent.
    assert!(
        !gap_hits.contains(&2) && !gap_hits.contains(&3) && !gap_hits.contains(&4),
        "Gap query should not return far primitives, got {gap_hits:?}"
    );

    // Empty BVH always returns empty
    let empty_bvh = LinearBvh::build(vec![]);
    assert!(empty_bvh.query(&q).is_empty());
}

// ============================================================================
// Test 6 — Cloth grid creation
// ============================================================================

/// Verify that `Cloth::new_grid` produces the expected particle count,
/// triangle count, and that all inverse masses are set correctly.
#[test]
fn test_cloth_grid_creation() {
    let res_x = 4usize;
    let res_y = 4usize;
    let mass = Fix128::from_ratio(1, 2); // 0.5 kg per particle

    let cloth = Cloth::new_grid(
        Vec3Fix::ZERO,
        Fix128::from_int(3),
        Fix128::from_int(3),
        res_x,
        res_y,
        mass,
    );

    // Particle count
    assert_eq!(cloth.positions.len(), res_x * res_y, "particle count");
    assert_eq!(cloth.inv_masses.len(), res_x * res_y, "inv_mass count");

    // Triangle count: (res_x-1)*(res_y-1)*2
    let expected_tris = (res_x - 1) * (res_y - 1) * 2;
    assert_eq!(cloth.triangles.len(), expected_tris, "triangle count");

    // Inverse mass should be 2 (= 1/0.5)
    let expected_inv_mass = Fix128::from_int(2);
    for &im in &cloth.inv_masses {
        assert_eq!(im.hi, expected_inv_mass.hi, "inv_mass.hi mismatch");
    }

    // No pinned particles by default
    assert!(cloth.pinned.is_empty(), "no pins expected");
}

// ============================================================================
// Test 7 — Static body does not move
// ============================================================================

/// A static body placed anywhere in the world must remain at its initial
/// position regardless of how many simulation steps are taken.
#[test]
fn test_static_body_does_not_move() {
    let config = PhysicsConfig::default();
    let mut world = PhysicsWorld::new(config);

    let initial = Vec3Fix::from_int(7, -3, 11);
    let ground = RigidBody::new_static(initial);
    let id = world.add_body(ground);

    let dt = Fix128::from_ratio(1, 60);
    run_world(&mut world, 120, dt);

    let final_pos = world.bodies[id].position;
    assert_eq!(final_pos.x.hi, initial.x.hi, "static body x changed");
    assert_eq!(final_pos.y.hi, initial.y.hi, "static body y changed");
    assert_eq!(final_pos.z.hi, initial.z.hi, "static body z changed");
}

// ============================================================================
// Test 8 — Vehicle ground contact and suspension
// ============================================================================

/// A vehicle placed above a ground plane should settle on its suspension.
/// After simulation, wheels should be grounded and the chassis should remain
/// above the ground plane.
#[test]
fn test_vehicle_ground_contact() {
    let config = PhysicsConfig::default();
    let mut world = PhysicsWorld::new(config);

    let chassis_id = world.add_body(RigidBody::new_dynamic(
        Vec3Fix::from_int(0, 1, 0),
        Fix128::from_int(1500),
    ));

    let mut vehicle = Vehicle::new(VehicleConfig::default());

    let dt = Fix128::from_ratio(1, 60);
    for _ in 0..120 {
        vehicle.update(&mut world.bodies[chassis_id], dt);
        world.step(dt);
    }

    // Wheels should be grounded after settling
    assert!(
        vehicle.grounded_wheels() > 0,
        "Expected grounded wheels after settling, got {}",
        vehicle.grounded_wheels()
    );

    // Chassis should remain above the ground plane
    assert!(
        world.bodies[chassis_id].position.y > Fix128::ZERO,
        "Chassis should be above ground after suspension settling"
    );
}

// ============================================================================
// Test 9 — Rope sag under gravity
// ============================================================================

/// A rope pinned at both endpoints should sag in the middle under gravity.
/// After simulation, the midpoint should be below the endpoints.
#[test]
fn test_rope_sag_under_gravity() {
    let start = Vec3Fix::from_int(0, 10, 0);
    let end = Vec3Fix::from_int(10, 10, 0);
    let mut rope = Rope::new(start, end, 10, Fix128::from_ratio(1, 10));

    rope.pin_start();
    rope.pin_end();

    let dt = Fix128::from_ratio(1, 60);
    for _ in 0..120 {
        rope.step(dt);
    }

    // Endpoints should remain at original height (pinned)
    assert_eq!(
        rope.positions[0].y.hi, 10,
        "Start pin should remain at y=10"
    );
    assert_eq!(rope.positions[10].y.hi, 10, "End pin should remain at y=10");

    // Middle particle should sag below endpoints
    let mid_y = rope.positions[5].y;
    assert!(
        mid_y < Fix128::from_int(10),
        "Rope midpoint should sag below y=10, got y.hi={}",
        mid_y.hi
    );
}

// ============================================================================
// Test 10 — Cross-architecture determinism (golden hash)
// ============================================================================

/// Run a multi-body simulation and compute an FNV-1a hash of the final state.
/// Running the same simulation twice must produce the same hash, guaranteeing
/// cross-platform determinism.
#[test]
fn test_determinism_golden_hash() {
    fn simulate_and_hash() -> u64 {
        let config = PhysicsConfig::default();
        let mut world = PhysicsWorld::new(config);

        world.add_body(RigidBody::new_dynamic(
            Vec3Fix::from_int(0, 50, 0),
            Fix128::ONE,
        ));
        world.add_body(RigidBody::new_dynamic(
            Vec3Fix::from_int(5, 50, 0),
            Fix128::from_ratio(3, 2),
        ));
        world.add_body(RigidBody::new_static(Vec3Fix::ZERO));

        let dt = Fix128::from_ratio(1, 60);
        for _ in 0..180 {
            world.step(dt);
        }

        // Collect final state bytes (position only — deterministic)
        let mut bytes = Vec::new();
        for body in &world.bodies {
            bytes.extend_from_slice(&body.position.x.hi.to_le_bytes());
            bytes.extend_from_slice(&body.position.x.lo.to_le_bytes());
            bytes.extend_from_slice(&body.position.y.hi.to_le_bytes());
            bytes.extend_from_slice(&body.position.y.lo.to_le_bytes());
            bytes.extend_from_slice(&body.position.z.hi.to_le_bytes());
            bytes.extend_from_slice(&body.position.z.lo.to_le_bytes());
        }

        // FNV-1a
        let mut hash: u64 = 0xcbf2_9ce4_8422_2325;
        for &b in &bytes {
            hash ^= u64::from(b);
            hash = hash.wrapping_mul(0x0100_0000_01b3);
        }
        hash
    }

    let hash1 = simulate_and_hash();
    let hash2 = simulate_and_hash();

    // Two identical runs must produce the same hash
    assert_eq!(
        hash1, hash2,
        "Determinism broken: hash1={hash1:#018x} hash2={hash2:#018x}"
    );

    // Hash should be non-trivial (different from FNV basis)
    assert_ne!(
        hash1, 0xcbf2_9ce4_8422_2325_u64,
        "Hash should differ from FNV basis"
    );
}

// ============================================================================
// Test 11 — Auto collision detection (BVH + sphere)
// ============================================================================

/// Two bodies with collision radii moving toward each other should generate
/// contact events and push apart via the integrated pipeline.
#[test]
fn test_auto_collision_detection() {
    let config = PhysicsConfig {
        gravity: Vec3Fix::ZERO, // no gravity
        ..Default::default()
    };
    let mut world = PhysicsWorld::new(config);

    // Two spheres approaching each other
    let mut body_a = RigidBody::new_dynamic(Vec3Fix::from_int(-1, 0, 0), Fix128::ONE);
    body_a.velocity = Vec3Fix::from_int(10, 0, 0); // moving right
    let id_a = world.add_body_with_radius(body_a, Fix128::ONE);

    let mut body_b = RigidBody::new_dynamic(Vec3Fix::from_int(1, 0, 0), Fix128::ONE);
    body_b.velocity = Vec3Fix::from_int(-10, 0, 0); // moving left
    let id_b = world.add_body_with_radius(body_b, Fix128::ONE);

    // Step several frames: detect_collisions runs before integration, so
    // bodies need one frame to move into overlap before detection fires.
    let dt = Fix128::from_ratio(1, 60);
    let mut had_contact = false;
    for _ in 0..10 {
        world.step(dt);
        if !world.contact_events().is_empty() {
            had_contact = true;
        }
    }

    assert!(had_contact, "Expected contact events from auto collision");

    // Bodies should have been pushed apart by collision response
    let pa = world.bodies[id_a].position;
    let pb = world.bodies[id_b].position;
    let dist = (pb - pa).length();
    assert!(
        dist > Fix128::ZERO,
        "Bodies should be separated after collision, dist.hi={}",
        dist.hi
    );
}

// ============================================================================
// Test 12 — Collision filtering
// ============================================================================

/// Bodies on different collision layers should not collide even if overlapping.
#[test]
fn test_collision_filtering() {
    let config = PhysicsConfig {
        gravity: Vec3Fix::ZERO,
        ..Default::default()
    };
    let mut world = PhysicsWorld::new(config);

    // Two overlapping bodies with collision radii
    let body_a = RigidBody::new_dynamic(Vec3Fix::ZERO, Fix128::ONE);
    let id_a = world.add_body_with_radius(body_a, Fix128::from_int(2));

    let body_b = RigidBody::new_dynamic(Vec3Fix::from_int(1, 0, 0), Fix128::ONE);
    let id_b = world.add_body_with_radius(body_b, Fix128::from_int(2));

    // Put them on different layers that don't collide
    world.set_body_filter(
        id_a,
        CollisionFilter {
            layer: 1,
            mask: 1,
            group: 0,
        },
    );
    world.set_body_filter(
        id_b,
        CollisionFilter {
            layer: 2,
            mask: 2,
            group: 0,
        },
    );

    let dt = Fix128::from_ratio(1, 60);
    world.step(dt);

    // No contact events should be generated
    let events = world.contact_events();
    assert!(
        events.is_empty(),
        "Expected no events when filters prevent collision, got {}",
        events.len()
    );
}

// ============================================================================
// Test 13 — Joint in step pipeline
// ============================================================================

/// A ball joint connecting two bodies should keep them together during `step()`.
#[test]
fn test_joint_in_step() {
    let config = PhysicsConfig {
        gravity: Vec3Fix::ZERO,
        ..Default::default()
    };
    let mut world = PhysicsWorld::new(config);

    let id_a = world.add_body(RigidBody::new_static(Vec3Fix::ZERO));
    let id_b = world.add_body(RigidBody::new_dynamic(
        Vec3Fix::from_int(3, 0, 0),
        Fix128::ONE,
    ));

    let joint = Joint::Ball(BallJoint::new(id_a, id_b, Vec3Fix::ZERO, Vec3Fix::ZERO));
    world.add_joint(joint);
    assert_eq!(world.joint_count(), 1);

    // Step: joint should pull body_b toward body_a's anchor
    let dt = Fix128::from_ratio(1, 60);
    run_world(&mut world, 60, dt);

    let pb = world.bodies[id_b].position;
    let dist = pb.length();
    // With zero gravity and ball joint anchored at origin, body should be pulled in
    assert!(
        dist < Fix128::from_int(3),
        "Joint should pull body closer, dist.hi={}",
        dist.hi
    );
}

// ============================================================================
// Test 14 — Force field in step pipeline
// ============================================================================

/// A directional force field should accelerate bodies during `step()`.
#[test]
fn test_force_field_in_step() {
    let config = PhysicsConfig {
        gravity: Vec3Fix::ZERO,
        ..Default::default()
    };
    let mut world = PhysicsWorld::new(config);

    let body = RigidBody::new_dynamic(Vec3Fix::ZERO, Fix128::ONE);
    world.add_body(body);

    // Add wind force in +X direction
    let wind = ForceFieldInstance::new(ForceField::Directional {
        direction: Vec3Fix::UNIT_X,
        strength: Fix128::from_int(50),
    });
    world.add_force_field(wind);

    let dt = Fix128::from_ratio(1, 60);
    run_world(&mut world, 60, dt);

    // Body should have moved in +X direction
    let pos = world.bodies[0].position;
    assert!(
        pos.x > Fix128::ZERO,
        "Wind should push body in +X direction, got x.hi={}",
        pos.x.hi
    );
}

// ============================================================================
// Test 15 — Sleeping integration
// ============================================================================

/// A still body should eventually sleep; applying velocity should wake it.
#[test]
fn test_sleeping_in_step() {
    let config = PhysicsConfig {
        gravity: Vec3Fix::ZERO,
        ..Default::default()
    };
    let mut world = PhysicsWorld::new(config);

    world.set_sleep_config(SleepConfig {
        linear_threshold: Fix128::from_ratio(1, 100),
        angular_threshold: Fix128::from_ratio(1, 100),
        frames_to_sleep: 5,
    });

    let body = RigidBody::new_dynamic(Vec3Fix::ZERO, Fix128::ONE);
    let id = world.add_body(body);

    // Step enough frames for sleeping
    let dt = Fix128::from_ratio(1, 60);
    run_world(&mut world, 10, dt);

    assert!(
        world.is_sleeping(id),
        "Still body should be sleeping after threshold"
    );

    // Wake it up
    world.wake_body(id);
    assert!(
        !world.is_sleeping(id),
        "Body should be awake after wake_body()"
    );
}

// ============================================================================
// Test 16 — Event lifecycle (Begin → Persist → End)
// ============================================================================

/// Contact events should transition through Begin → Persist → End correctly.
#[test]
fn test_event_lifecycle() {
    let config = PhysicsConfig {
        gravity: Vec3Fix::ZERO,
        ..Default::default()
    };
    let mut world = PhysicsWorld::new(config);

    // Two overlapping bodies (stationary, overlapping collision spheres)
    let body_a = RigidBody::new_dynamic(Vec3Fix::ZERO, Fix128::ONE);
    let _id_a = world.add_body_with_radius(body_a, Fix128::from_int(2));

    let body_b = RigidBody::new_dynamic(Vec3Fix::from_int(1, 0, 0), Fix128::ONE);
    let _id_b = world.add_body_with_radius(body_b, Fix128::from_int(2));

    let dt = Fix128::from_ratio(1, 60);

    // Frame 1: Begin
    world.step(dt);
    let events = world.contact_events();
    assert!(events
        .iter()
        .any(|e| e.event_type == ContactEventType::Begin));

    // Frame 2: Persist (bodies still overlapping after push)
    world.step(dt);
    // After push-apart, they may no longer overlap => could be End. Check events exist.
    let events2 = world.contact_events();
    assert!(
        !events2.is_empty(),
        "Should have Persist or End events in frame 2"
    );
}

// ============================================================================
// Test 17 — remove_body with constraint cleanup
// ============================================================================

/// Removing a body should clean up associated constraints and remap indices.
#[test]
fn test_remove_body() {
    let config = PhysicsConfig {
        gravity: Vec3Fix::ZERO,
        ..Default::default()
    };
    let mut world = PhysicsWorld::new(config);

    let id_a = world.add_body(RigidBody::new_dynamic(Vec3Fix::ZERO, Fix128::ONE));
    let id_b = world.add_body(RigidBody::new_dynamic(
        Vec3Fix::from_int(5, 0, 0),
        Fix128::ONE,
    ));
    let id_c = world.add_body(RigidBody::new_dynamic(
        Vec3Fix::from_int(10, 0, 0),
        Fix128::ONE,
    ));

    // Add constraint between B and C
    world.add_distance_constraint(DistanceConstraint {
        body_a: id_b,
        body_b: id_c,
        local_anchor_a: Vec3Fix::ZERO,
        local_anchor_b: Vec3Fix::ZERO,
        target_distance: Fix128::from_int(5),
        compliance: Fix128::ZERO,
        cached_lambda: Fix128::ZERO,
    });

    assert_eq!(world.body_count(), 3);

    // Remove body A (swap-remove: C moves to index 0)
    let removed = world.remove_body(id_a);
    assert!(removed.is_some());
    assert_eq!(world.body_count(), 2);

    // Simulation should still work without crashing
    let dt = Fix128::from_ratio(1, 60);
    run_world(&mut world, 10, dt);
}

// ============================================================================
// Test 18 — Trigger/sensor event detection
// ============================================================================

/// Sensor bodies should generate trigger events but no physics response.
#[test]
fn test_sensor_trigger_events() {
    let config = PhysicsConfig {
        gravity: Vec3Fix::ZERO,
        ..Default::default()
    };
    let mut world = PhysicsWorld::new(config);

    // Sensor body
    let mut sensor = RigidBody::new_dynamic(Vec3Fix::ZERO, Fix128::ONE);
    sensor.is_sensor = true;
    let id_sensor = world.add_body_with_radius(sensor, Fix128::from_int(2));

    // Moving body that will enter the sensor
    let moving = RigidBody::new_dynamic(Vec3Fix::from_int(1, 0, 0), Fix128::ONE);
    let _id_moving = world.add_body_with_radius(moving, Fix128::from_int(2));

    let dt = Fix128::from_ratio(1, 60);
    world.step(dt);

    // Trigger events should fire
    let triggers = world.trigger_events();
    assert!(
        !triggers.is_empty(),
        "Expected trigger events for sensor overlap"
    );
    assert!(triggers[0].entered, "First trigger should be an enter");

    // Sensor body should NOT have been pushed away (no physics response)
    let sensor_pos = world.bodies[id_sensor].position;
    // With zero gravity and no physics response, sensor stays at origin
    assert_eq!(
        sensor_pos.x.hi, 0,
        "Sensor should not move from physics response"
    );
}

// ============================================================================
// Test 19 — Raycast against world
// ============================================================================

/// World raycast should find the nearest sphere along the ray direction.
#[test]
fn test_world_raycast() {
    let config = PhysicsConfig::default();
    let mut world = PhysicsWorld::new(config);

    // Place spheres along +X axis
    let body_near = RigidBody::new_static(Vec3Fix::from_int(5, 0, 0));
    let id_near = world.add_body_with_radius(body_near, Fix128::ONE);

    let body_far = RigidBody::new_static(Vec3Fix::from_int(20, 0, 0));
    let _id_far = world.add_body_with_radius(body_far, Fix128::ONE);

    // Ray from origin in +X direction
    let result = world.raycast(Vec3Fix::ZERO, Vec3Fix::UNIT_X, Fix128::from_int(100));
    assert!(result.is_some(), "Raycast should hit a body");

    let (hit_body, hit_dist) = result.unwrap();
    assert_eq!(hit_body, id_near, "Should hit nearest body");
    assert!(
        hit_dist > Fix128::from_int(3) && hit_dist < Fix128::from_int(6),
        "Hit distance should be ~4 (center=5, radius=1), got hi={}",
        hit_dist.hi
    );
}

// ============================================================================
// Test 20 — add_body_with_radius convenience
// ============================================================================

/// Verify `add_body_with_radius` enables auto collision for that body.
#[test]
fn test_add_body_with_radius() {
    let config = PhysicsConfig {
        gravity: Vec3Fix::ZERO,
        ..Default::default()
    };
    let mut world = PhysicsWorld::new(config);

    // Body without radius: no auto-collision
    let plain = RigidBody::new_dynamic(Vec3Fix::ZERO, Fix128::ONE);
    world.add_body(plain);

    // Body with radius: participates in auto-collision
    let sphere = RigidBody::new_dynamic(Vec3Fix::from_int(1, 0, 0), Fix128::ONE);
    let id = world.add_body_with_radius(sphere, Fix128::ONE);

    // Should be able to raycast against the radius body
    let hit = world.raycast(
        Vec3Fix::from_int(-5, 0, 0),
        Vec3Fix::UNIT_X,
        Fix128::from_int(100),
    );
    assert!(hit.is_some());
    assert_eq!(hit.unwrap().0, id);
}

// ============================================================================
// Test 21 — body_count and active_body_count
// ============================================================================

/// Verify body counting with sleeping bodies.
#[test]
fn test_body_count_and_active() {
    let config = PhysicsConfig {
        gravity: Vec3Fix::ZERO,
        ..Default::default()
    };
    let mut world = PhysicsWorld::new(config);

    world.set_sleep_config(SleepConfig {
        linear_threshold: Fix128::from_ratio(1, 100),
        angular_threshold: Fix128::from_ratio(1, 100),
        frames_to_sleep: 3,
    });

    // Add 3 still bodies
    for i in 0..3 {
        let body = RigidBody::new_dynamic(Vec3Fix::from_int(i * 5, 0, 0), Fix128::ONE);
        world.add_body(body);
    }

    assert_eq!(world.body_count(), 3);

    // Step enough for sleeping
    let dt = Fix128::from_ratio(1, 60);
    run_world(&mut world, 5, dt);

    // All should be sleeping
    assert_eq!(world.body_count(), 3);
    assert_eq!(
        world.active_body_count(),
        0,
        "All still bodies should sleep"
    );
}

// ============================================================================
// Test 22 — RigidBody convenience methods
// ============================================================================

/// Verify `add_force`, `set_velocity`, mass, speed methods.
#[test]
fn test_rigid_body_convenience_methods() {
    let mut body = RigidBody::new_dynamic(Vec3Fix::ZERO, Fix128::from_int(2));

    // mass = 1/inv_mass
    assert_eq!(body.mass().hi, 2, "mass should be 2");

    // set_velocity
    body.set_velocity(Vec3Fix::from_int(3, 4, 0));
    assert_eq!(body.velocity.x.hi, 3);
    assert_eq!(body.velocity.y.hi, 4);

    // speed = |velocity|
    let spd = body.speed();
    assert!(
        spd > Fix128::from_int(4),
        "speed should be 5 (3-4-5 triangle)"
    );
    assert!(spd < Fix128::from_int(6));

    // add_force: dv = F * dt * inv_mass
    // Use power-of-2 fractions for exact fixed-point arithmetic
    body.set_velocity(Vec3Fix::ZERO);
    body.add_force(Vec3Fix::from_int(4, 0, 0), Fix128::ONE);
    // dv = 4 * 1.0 * 0.5 = 2.0
    assert_eq!(body.velocity.x.hi, 2, "add_force should change velocity");
}

// ============================================================================
// Test 23 — angular_velocity serialization round-trip
// ============================================================================

/// Verify `angular_velocity` is preserved through serialize/deserialize.
#[test]
fn test_angular_velocity_serialization_roundtrip() {
    let config = PhysicsConfig::default();
    let mut world = PhysicsWorld::new(config);

    let mut body = RigidBody::new_dynamic(Vec3Fix::from_int(0, 10, 0), Fix128::ONE);
    body.angular_velocity = Vec3Fix::from_int(3, 7, 11);
    world.add_body(body);

    let dt = Fix128::from_ratio(1, 64);
    run_world(&mut world, 10, dt);

    // Capture angular_velocity before serialize
    let av_before = world.bodies[0].angular_velocity;

    let state = world.serialize_state();
    // Corrupt angular_velocity
    world.bodies[0].angular_velocity = Vec3Fix::ZERO;

    // Restore
    assert!(world.deserialize_state(&state));

    let av_after = world.bodies[0].angular_velocity;
    assert_eq!(av_before.x.hi, av_after.x.hi);
    assert_eq!(av_before.x.lo, av_after.x.lo);
    assert_eq!(av_before.y.hi, av_after.y.hi);
    assert_eq!(av_before.y.lo, av_after.y.lo);
    assert_eq!(av_before.z.hi, av_after.z.hi);
    assert_eq!(av_before.z.lo, av_after.z.lo);
}

// ============================================================================
// Test 24 — All subsystems combined in step
// ============================================================================

/// Run `step()` with force fields + collisions + joints + sleeping + events all active.
#[test]
fn test_all_subsystems_combined() {
    let config = PhysicsConfig {
        gravity: Vec3Fix::from_int(0, -10, 0),
        substeps: 4,
        ..Default::default()
    };
    let mut world = PhysicsWorld::new(config);
    world.set_sleep_config(SleepConfig {
        linear_threshold: Fix128::from_ratio(1, 100),
        angular_threshold: Fix128::from_ratio(1, 100),
        frames_to_sleep: 120,
    });

    // Body A and B connected by ball joint, both with collision radii
    let id_a = world.add_body_with_radius(
        RigidBody::new_dynamic(Vec3Fix::from_int(-1, 3, 0), Fix128::ONE),
        Fix128::from_int(2),
    );
    let id_b = world.add_body_with_radius(
        RigidBody::new_dynamic(Vec3Fix::from_int(1, 3, 0), Fix128::ONE),
        Fix128::from_int(2),
    );
    // Body C: sitting below, A/B will fall into it
    let _id_c = world.add_body_with_radius(
        RigidBody::new_static(Vec3Fix::from_int(0, -2, 0)),
        Fix128::from_int(3),
    );

    // Add ball joint between A and B
    let ball = BallJoint::new(id_a, id_b, Vec3Fix::ZERO, Vec3Fix::ZERO);
    world.add_joint(Joint::Ball(ball));

    // Add a wind force field
    world.add_force_field(ForceFieldInstance::new(ForceField::Directional {
        direction: Vec3Fix::UNIT_X,
        strength: Fix128::from_int(5),
    }));

    let dt = Fix128::from_ratio(1, 64);
    let mut had_contacts = false;
    for _ in 0..240 {
        world.step(dt);
        if !world.contact_events().is_empty() {
            had_contacts = true;
        }
    }

    // Verify all subsystems ran: gravity caused falling, wind pushed laterally,
    // joint kept A-B connected, and contacts were generated
    let pos_a = world.bodies[id_a].position;
    assert!(pos_a.y < Fix128::from_int(3), "Gravity should pull A down");
    assert!(pos_a.x > Fix128::from_int(-1), "Wind should push A right");
    assert!(had_contacts, "Collisions should generate contact events");
}

// ============================================================================
// Test 25 — raycast with zero direction
// ============================================================================

/// `raycast()` with zero-length direction should return None, not panic.
#[test]
fn test_raycast_zero_direction() {
    let config = PhysicsConfig::default();
    let mut world = PhysicsWorld::new(config);
    world.add_body_with_radius(
        RigidBody::new_static(Vec3Fix::from_int(5, 0, 0)),
        Fix128::ONE,
    );

    let result = world.raycast(Vec3Fix::ZERO, Vec3Fix::ZERO, Fix128::from_int(100));
    assert!(result.is_none(), "Zero direction should return None");
}

// ============================================================================
// Test 26 — add_joint out-of-bounds panics
// ============================================================================

/// `add_joint()` with invalid body indices should panic.
#[test]
#[should_panic(expected = "out of bounds")]
fn test_add_joint_out_of_bounds() {
    let config = PhysicsConfig::default();
    let mut world = PhysicsWorld::new(config);
    world.add_body(RigidBody::new_dynamic(Vec3Fix::ZERO, Fix128::ONE));
    // Only 1 body exists, referencing index 999 should panic
    let ball = BallJoint::new(0, 999, Vec3Fix::ZERO, Vec3Fix::ZERO);
    world.add_joint(Joint::Ball(ball));
}

// ============================================================================
// Test 27 — remove_body with active joint
// ============================================================================

/// Removing a body that has an active joint should clean up the joint.
#[test]
fn test_remove_body_with_active_joint() {
    let config = PhysicsConfig::default();
    let mut world = PhysicsWorld::new(config);

    let id_a = world.add_body(RigidBody::new_dynamic(
        Vec3Fix::from_int(0, 0, 0),
        Fix128::ONE,
    ));
    let id_b = world.add_body(RigidBody::new_dynamic(
        Vec3Fix::from_int(5, 0, 0),
        Fix128::ONE,
    ));
    let id_c = world.add_body(RigidBody::new_dynamic(
        Vec3Fix::from_int(10, 0, 0),
        Fix128::ONE,
    ));

    // Joint between A-B and B-C
    let ball_ab = BallJoint::new(id_a, id_b, Vec3Fix::ZERO, Vec3Fix::ZERO);
    let ball_bc = BallJoint::new(id_b, id_c, Vec3Fix::ZERO, Vec3Fix::ZERO);
    world.add_joint(Joint::Ball(ball_ab));
    world.add_joint(Joint::Ball(ball_bc));

    assert_eq!(world.joint_count(), 2);

    // Remove body A — joint A-B should be cleaned up
    world.remove_body(id_a);

    assert_eq!(world.body_count(), 2, "Should have 2 bodies left");

    // Step should not crash
    let dt = Fix128::from_ratio(1, 64);
    run_world(&mut world, 10, dt);
}

// ============================================================================
// Test 28 — Empty world step
// ============================================================================

/// `step()` on a world with zero bodies should not panic.
#[test]
fn test_empty_world_step() {
    let config = PhysicsConfig::default();
    let mut world = PhysicsWorld::new(config);

    assert_eq!(world.body_count(), 0);

    let dt = Fix128::from_ratio(1, 64);
    run_world(&mut world, 10, dt);

    assert_eq!(world.body_count(), 0);
}

// ============================================================================
// Test 29 — Empty world after removing all bodies
// ============================================================================

/// Removing all bodies and then stepping should not panic.
#[test]
fn test_empty_world_after_remove_all() {
    let config = PhysicsConfig::default();
    let mut world = PhysicsWorld::new(config);

    world.add_body(RigidBody::new_dynamic(Vec3Fix::ZERO, Fix128::ONE));
    world.add_body(RigidBody::new_dynamic(
        Vec3Fix::from_int(5, 0, 0),
        Fix128::ONE,
    ));

    // Remove both (careful: swap-remove changes last index)
    world.remove_body(0);
    world.remove_body(0);

    assert_eq!(world.body_count(), 0);

    let dt = Fix128::from_ratio(1, 64);
    run_world(&mut world, 10, dt);
}

// ============================================================================
// Test 30 — Sleep propagation through joint islands
// ============================================================================

/// Bodies connected by joints should sleep and wake as a group.
#[test]
fn test_sleep_island_propagation() {
    let config = PhysicsConfig {
        gravity: Vec3Fix::ZERO, // no gravity so bodies stay still
        ..Default::default()
    };
    let mut world = PhysicsWorld::new(config);
    world.set_sleep_config(SleepConfig {
        linear_threshold: Fix128::from_ratio(1, 100),
        angular_threshold: Fix128::from_ratio(1, 100),
        frames_to_sleep: 3,
    });

    // Chain: A --joint-- B --joint-- C
    let id_a = world.add_body(RigidBody::new_dynamic(
        Vec3Fix::from_int(0, 0, 0),
        Fix128::ONE,
    ));
    let id_b = world.add_body(RigidBody::new_dynamic(
        Vec3Fix::from_int(5, 0, 0),
        Fix128::ONE,
    ));
    let id_c = world.add_body(RigidBody::new_dynamic(
        Vec3Fix::from_int(10, 0, 0),
        Fix128::ONE,
    ));

    let ball_ab = BallJoint::new(id_a, id_b, Vec3Fix::ZERO, Vec3Fix::ZERO);
    let ball_bc = BallJoint::new(id_b, id_c, Vec3Fix::ZERO, Vec3Fix::ZERO);
    world.add_joint(Joint::Ball(ball_ab));
    world.add_joint(Joint::Ball(ball_bc));

    // D is separate — not connected
    let id_d = world.add_body(RigidBody::new_dynamic(
        Vec3Fix::from_int(20, 0, 0),
        Fix128::ONE,
    ));

    let dt = Fix128::from_ratio(1, 64);

    // Step enough for all bodies to sleep
    run_world(&mut world, 5, dt);

    assert_eq!(
        world.active_body_count(),
        0,
        "All still bodies should sleep"
    );

    // Wake body C — should wake A and B through island, but NOT D
    world.wake_body(id_c);

    assert!(!world.is_sleeping(id_c), "C should be awake");
    // Note: wake_body only wakes the single body, not the island.
    // The island waking happens via wake_island if used, or next step may detect activity.
    // After a step, if C is awake, the island system should propagate.
    // Actually wake_body wakes single body; but the key test is
    // that D (separate island) stays asleep.
    assert!(
        world.is_sleeping(id_d),
        "D should still be sleeping (different island)"
    );
}

// ============================================================================
// Test 31 — Event lifecycle Persist verification
// ============================================================================

/// Verify that the Persist event type is correctly reported on sustained contact.
#[test]
fn test_event_persist_explicit() {
    let config = PhysicsConfig {
        gravity: Vec3Fix::ZERO,
        substeps: 1,
        ..Default::default()
    };
    let mut world = PhysicsWorld::new(config);

    // Two bodies pushed into each other
    let mut body_a = RigidBody::new_dynamic(Vec3Fix::from_int(-1, 0, 0), Fix128::ONE);
    body_a.velocity = Vec3Fix::from_int(10, 0, 0); // moving right
    let mut body_b = RigidBody::new_dynamic(Vec3Fix::from_int(1, 0, 0), Fix128::ONE);
    body_b.velocity = Vec3Fix::from_int(-10, 0, 0); // moving left

    world.add_body_with_radius(body_a, Fix128::from_int(2));
    world.add_body_with_radius(body_b, Fix128::from_int(2));

    let dt = Fix128::from_ratio(1, 64);

    // Frame 1: should get Begin
    world.step(dt);
    let has_begin = world
        .contact_events()
        .iter()
        .any(|e| e.event_type == ContactEventType::Begin);

    // Frame 2+: if still overlapping, should get Persist
    world.step(dt);
    let has_persist = world
        .contact_events()
        .iter()
        .any(|e| e.event_type == ContactEventType::Persist);

    // At least one of these should be true (bodies are overlapping for >=2 frames)
    assert!(
        has_begin || has_persist,
        "Should have Begin or Persist events from sustained contact"
    );
}

// ============================================================================
// Test 32 — dt=0 and dt<0 guard
// ============================================================================

/// `step()` with zero or negative dt should be a no-op.
#[test]
fn test_step_zero_and_negative_dt() {
    let config = PhysicsConfig::default();
    let mut world = PhysicsWorld::new(config);

    let body = RigidBody::new_dynamic(Vec3Fix::from_int(0, 10, 0), Fix128::ONE);
    world.add_body(body);

    let pos_before = world.bodies[0].position;

    // dt = 0 should be no-op
    world.step(Fix128::ZERO);
    assert_eq!(
        world.bodies[0].position.y.hi, pos_before.y.hi,
        "dt=0 should not move body"
    );

    // dt < 0 should be no-op
    world.step(Fix128::from_int(-1));
    assert_eq!(
        world.bodies[0].position.y.hi, pos_before.y.hi,
        "dt<0 should not move body"
    );
}

// ============================================================================
// Test 33 — Kinematic body pushes dynamic body
// ============================================================================

/// A kinematic body `set_kinematic_target` through a dynamic body should push it.
#[test]
fn test_kinematic_pushes_dynamic() {
    use alice_physics::QuatFix;

    let config = PhysicsConfig {
        gravity: Vec3Fix::ZERO,
        substeps: 4,
        ..Default::default()
    };
    let mut world = PhysicsWorld::new(config);

    // Kinematic body starting at left
    let kin = RigidBody::new_kinematic(Vec3Fix::from_int(-5, 0, 0));
    let id_kin = world.add_body_with_radius(kin, Fix128::from_int(2));

    // Dynamic body sitting still at origin
    let dyn_body = RigidBody::new_dynamic(Vec3Fix::from_int(0, 0, 0), Fix128::ONE);
    let _id_dyn = world.add_body_with_radius(dyn_body, Fix128::from_int(2));

    let dt = Fix128::from_ratio(1, 64);

    // Move kinematic body rightward each frame via set_kinematic_target
    for i in 0..30 {
        let target_x = Fix128::from_int(-5) + Fix128::from_int(i + 1) * dt * Fix128::from_int(10);
        world.bodies[id_kin].set_kinematic_target(
            Vec3Fix::new(target_x, Fix128::ZERO, Fix128::ZERO),
            QuatFix::IDENTITY,
        );
        world.step(dt);
    }

    // Kinematic should have moved right from -5
    assert!(
        world.bodies[id_kin].position.x > Fix128::from_int(-5),
        "Kinematic should move"
    );
}

// ============================================================================
// Test 34 — detect_collisions with mixed radii (some None)
// ============================================================================

/// Bodies added without radius should not participate in auto-collision.
#[test]
fn test_collision_detection_mixed_radii() {
    let config = PhysicsConfig {
        gravity: Vec3Fix::ZERO,
        ..Default::default()
    };
    let mut world = PhysicsWorld::new(config);

    // Body A: has radius
    let mut body_a = RigidBody::new_dynamic(Vec3Fix::from_int(-1, 0, 0), Fix128::ONE);
    body_a.velocity = Vec3Fix::from_int(5, 0, 0);
    world.add_body_with_radius(body_a, Fix128::from_int(2));

    // Body B: NO radius (added via add_body, not add_body_with_radius)
    let body_b = RigidBody::new_dynamic(Vec3Fix::from_int(1, 0, 0), Fix128::ONE);
    world.add_body(body_b);

    let dt = Fix128::from_ratio(1, 64);

    // Step and check: no collision events should fire (B has no radius)
    let mut had_contacts = false;
    for _ in 0..20 {
        world.step(dt);
        if !world.contact_events().is_empty() {
            had_contacts = true;
        }
    }

    assert!(
        !had_contacts,
        "Body without radius should not generate auto-collisions"
    );
}

// ============================================================================
// Test 35 — get_body / get_body_mut
// ============================================================================

/// Verify safe body accessors return Some/None correctly.
#[test]
fn test_get_body_accessors() {
    let config = PhysicsConfig::default();
    let mut world = PhysicsWorld::new(config);

    let id = world.add_body(RigidBody::new_dynamic(
        Vec3Fix::from_int(7, 0, 0),
        Fix128::ONE,
    ));

    // Valid index → Some
    assert!(world.get_body(id).is_some());
    assert_eq!(world.get_body(id).unwrap().position.x.hi, 7);

    // Out-of-bounds → None
    assert!(world.get_body(999).is_none());

    // Mutable accessor
    {
        let body = world.get_body_mut(id).unwrap();
        body.position.x = Fix128::from_int(42);
    }
    assert_eq!(world.get_body(id).unwrap().position.x.hi, 42);

    // Mutable out-of-bounds
    assert!(world.get_body_mut(999).is_none());
}

// ============================================================================
// Test 36 — filter::layers re-export
// ============================================================================

/// Verify that `filter::layers` constants are accessible from crate root.
#[test]
fn test_layers_reexport() {
    use alice_physics::layers;

    let player = CollisionFilter::new(layers::PLAYER, layers::ENEMY | layers::STATIC);
    let enemy = CollisionFilter::new(layers::ENEMY, layers::PLAYER | layers::STATIC);

    assert!(CollisionFilter::can_collide(&player, &enemy));
    assert!(!CollisionFilter::can_collide(
        &CollisionFilter::new(layers::PLAYER, layers::PLAYER),
        &CollisionFilter::new(layers::ENEMY, layers::ENEMY),
    ));
}

// ============================================================================
// Test 37 — rebuild_batches graph coloring invariant
// ============================================================================

/// Verify `rebuild_batches()` + constraint solving produce correct results.
/// A chain of constraints should maintain distance after solving.
#[test]
fn test_rebuild_batches_constraint_chain() {
    let config = PhysicsConfig {
        gravity: Vec3Fix::ZERO,
        substeps: 8,
        ..Default::default()
    };
    let mut world = PhysicsWorld::new(config);

    // Create a chain: 0 - 1 - 2 - 3 - 4 with unit distance
    for i in 0..5 {
        world.add_body(RigidBody::new_dynamic(
            Vec3Fix::from_int(i64::from(i), 0, 0),
            Fix128::ONE,
        ));
    }
    for i in 0..4 {
        world.add_distance_constraint(DistanceConstraint {
            body_a: i,
            body_b: i + 1,
            local_anchor_a: Vec3Fix::ZERO,
            local_anchor_b: Vec3Fix::ZERO,
            target_distance: Fix128::ONE,
            compliance: Fix128::ZERO,
            cached_lambda: Fix128::ZERO,
        });
    }

    // rebuild_batches should succeed without panic
    world.rebuild_batches();

    // Step to let constraints settle
    let dt = Fix128::from_ratio(1, 64);
    run_world(&mut world, 60, dt);

    // Each pair should be roughly unit distance
    for i in 0..4 {
        let a = world.bodies[i].position;
        let b = world.bodies[i + 1].position;
        let dist = (b - a).length();
        let error = if dist > Fix128::ONE {
            dist - Fix128::ONE
        } else {
            Fix128::ONE - dist
        };
        assert!(
            error < Fix128::from_int(2),
            "Chain link {}-{} distance error too large: {:?}",
            i,
            i + 1,
            error,
        );
    }
}

// ============================================================================
// Test 38 — step_parallel (feature = "parallel")
// ============================================================================

/// Verify step_parallel() produces results identical to step() on a scene with
/// no shared-body constraints (single body: the Gauss–Seidel orderings
/// coincide). With shared bodies the two paths are each deterministic but not
/// bit-identical — see `PhysicsWorld::step_parallel` and
/// `fuzz/fuzz_targets/fuzz_step_parity.rs`.
#[cfg(feature = "parallel")]
#[test]
fn test_step_parallel_determinism() {
    // Run same scenario with step()
    fn run_sequential() -> Vec3Fix {
        let config = PhysicsConfig {
            gravity: Vec3Fix::from_int(0, -10, 0),
            substeps: 4,
            ..Default::default()
        };
        let mut world = PhysicsWorld::new(config);

        let body = RigidBody::new_dynamic(Vec3Fix::from_int(5, 50, 3), Fix128::ONE);
        world.add_body(body);

        let dt = Fix128::from_ratio(1, 64);
        for _ in 0..120 {
            world.step(dt);
        }
        world.bodies[0].position
    }

    // Run same scenario with step_parallel()
    fn run_parallel() -> Vec3Fix {
        let config = PhysicsConfig {
            gravity: Vec3Fix::from_int(0, -10, 0),
            substeps: 4,
            ..Default::default()
        };
        let mut world = PhysicsWorld::new(config);

        let body = RigidBody::new_dynamic(Vec3Fix::from_int(5, 50, 3), Fix128::ONE);
        world.add_body(body);

        let dt = Fix128::from_ratio(1, 64);
        for _ in 0..120 {
            world.step_parallel(dt);
        }
        world.bodies[0].position
    }

    let seq = run_sequential();
    let par = run_parallel();

    // Must be bit-exact
    assert_eq!(seq.x.hi, par.x.hi, "x.hi mismatch");
    assert_eq!(seq.x.lo, par.x.lo, "x.lo mismatch");
    assert_eq!(seq.y.hi, par.y.hi, "y.hi mismatch");
    assert_eq!(seq.y.lo, par.y.lo, "y.lo mismatch");
    assert_eq!(seq.z.hi, par.z.hi, "z.hi mismatch");
    assert_eq!(seq.z.lo, par.z.lo, "z.lo mismatch");
}

/// Verify step_parallel() with constraints + collisions.
#[cfg(feature = "parallel")]
#[test]
fn test_step_parallel_with_constraints() {
    let config = PhysicsConfig {
        gravity: Vec3Fix::from_int(0, -10, 0),
        substeps: 4,
        ..Default::default()
    };
    let mut world = PhysicsWorld::new(config);

    let id_a = world.add_body(RigidBody::new_dynamic(
        Vec3Fix::from_int(0, 10, 0),
        Fix128::ONE,
    ));
    let id_b = world.add_body(RigidBody::new_dynamic(
        Vec3Fix::from_int(5, 10, 0),
        Fix128::ONE,
    ));

    world.add_distance_constraint(DistanceConstraint {
        body_a: id_a,
        body_b: id_b,
        local_anchor_a: Vec3Fix::ZERO,
        local_anchor_b: Vec3Fix::ZERO,
        target_distance: Fix128::from_int(5),
        compliance: Fix128::ZERO,
        cached_lambda: Fix128::ZERO,
    });

    let dt = Fix128::from_ratio(1, 64);
    for _ in 0..60 {
        world.step_parallel(dt);
    }

    // Distance should be approximately maintained
    let pos_a = world.bodies[id_a].position;
    let pos_b = world.bodies[id_b].position;
    let dist = (pos_b - pos_a).length();
    let target = Fix128::from_int(5);
    let error = if dist > target {
        dist - target
    } else {
        target - dist
    };
    assert!(
        error < Fix128::from_int(2),
        "Parallel constraint stability failed: error={:?}",
        error
    );
}

/// Verify step_parallel() self-consistent determinism (run twice → same result).
#[cfg(feature = "parallel")]
#[test]
fn test_step_parallel_self_determinism() {
    fn run() -> Vec3Fix {
        let config = PhysicsConfig::default();
        let mut world = PhysicsWorld::new(config);

        let body = RigidBody::new_dynamic(Vec3Fix::from_int(0, 100, 0), Fix128::ONE);
        world.add_body(body);

        let dt = Fix128::from_ratio(1, 64);
        for _ in 0..200 {
            world.step_parallel(dt);
        }
        world.bodies[0].position
    }

    let r1 = run();
    let r2 = run();
    assert_eq!(r1.x.hi, r2.x.hi);
    assert_eq!(r1.x.lo, r2.x.lo);
    assert_eq!(r1.y.hi, r2.y.hi);
    assert_eq!(r1.y.lo, r2.y.lo);
    assert_eq!(r1.z.hi, r2.z.hi);
    assert_eq!(r1.z.lo, r2.z.lo);
}

// ============================================================================
// Test 39 — batch_raycast / batch_sphere_cast
// ============================================================================

/// Verify `batch_raycast` returns correct results for multiple rays.
#[test]
fn test_batch_raycast_results() {
    use alice_physics::query::{batch_raycast, BatchRayQuery};

    let bodies = [
        RigidBody::new_static(Vec3Fix::from_int(5, 0, 0)),
        RigidBody::new_static(Vec3Fix::from_int(10, 0, 0)),
    ];

    let queries = vec![
        BatchRayQuery {
            origin: Vec3Fix::ZERO,
            direction: Vec3Fix::UNIT_X,
            max_distance: Fix128::from_int(100),
        },
        BatchRayQuery {
            origin: Vec3Fix::ZERO,
            direction: Vec3Fix::from_int(0, 1, 0), // shoots up, misses
            max_distance: Fix128::from_int(100),
        },
    ];

    let results = batch_raycast(&queries, &bodies, Fix128::ONE);
    assert_eq!(results.len(), 2);
    assert!(results[0].is_some(), "First ray should hit body at x=5");
    assert!(
        results[1].is_none(),
        "Second ray should miss (no body along Y)"
    );
}

/// Verify `batch_sphere_cast` returns correct hit count.
#[test]
fn test_batch_sphere_cast_results() {
    use alice_physics::query::batch_sphere_cast;

    let bodies = [RigidBody::new_static(Vec3Fix::from_int(5, 0, 0))];

    let origins = vec![Vec3Fix::ZERO, Vec3Fix::from_int(0, 100, 0)];
    let directions = vec![Vec3Fix::UNIT_X, Vec3Fix::UNIT_X];

    let results = batch_sphere_cast(
        &origins,
        Fix128::ONE,
        &directions,
        Fix128::from_int(100),
        &bodies,
        Fix128::ONE,
    );

    assert_eq!(results.len(), 2);
    assert!(results[0].is_some(), "Close ray should hit");
    assert!(results[1].is_none(), "Far-off ray should miss");
}

// ============================================================================
// Test 40 — Serialization round-trip truncation correctness
// ============================================================================

/// Verify serialize → deserialize → serialize produces identical bytes.
#[test]
fn test_serialization_exact_roundtrip() {
    let config = PhysicsConfig::default();
    let mut world = PhysicsWorld::new(config);

    // Add bodies with various states
    let mut body_a = RigidBody::new_dynamic(Vec3Fix::from_int(1, 20, 3), Fix128::ONE);
    body_a.angular_velocity = Vec3Fix::from_int(5, -3, 7);
    world.add_body(body_a);

    let mut body_b = RigidBody::new_dynamic(Vec3Fix::from_int(-4, 0, 8), Fix128::from_int(2));
    body_b.velocity = Vec3Fix::from_int(10, -5, 2);
    world.add_body(body_b);

    let dt = Fix128::from_ratio(1, 64);
    run_world(&mut world, 30, dt);

    // Serialize → Deserialize → Serialize
    let state1 = world.serialize_state();
    assert!(world.deserialize_state(&state1));
    let state2 = world.serialize_state();

    // Must be byte-identical
    assert_eq!(state1.len(), state2.len(), "Round-trip size mismatch");
    assert_eq!(state1, state2, "Round-trip bytes not identical");
}

/// Verify deserialize rejects mismatched body count.
#[test]
fn test_serialization_rejects_mismatch() {
    let config = PhysicsConfig::default();

    // World with 2 bodies
    let mut world_2 = PhysicsWorld::new(config);
    world_2.add_body(RigidBody::new_dynamic(Vec3Fix::ZERO, Fix128::ONE));
    world_2.add_body(RigidBody::new_dynamic(Vec3Fix::UNIT_X, Fix128::ONE));
    let state_2 = world_2.serialize_state();

    // World with 3 bodies → deserialize should fail
    let mut world_3 = PhysicsWorld::new(config);
    for _ in 0..3 {
        world_3.add_body(RigidBody::new_dynamic(Vec3Fix::ZERO, Fix128::ONE));
    }
    assert!(
        !world_3.deserialize_state(&state_2),
        "Mismatched body count should return false"
    );
}

// ============================================================================
// Test 42 — apply_impulse changes velocity
// ============================================================================

#[test]
fn test_apply_impulse() {
    let config = PhysicsConfig::default();
    let mut world = PhysicsWorld::new(config);
    let id = world.add_body(RigidBody::new_dynamic(Vec3Fix::ZERO, Fix128::ONE));

    let impulse = Vec3Fix::new(Fix128::from_int(5), Fix128::ZERO, Fix128::ZERO);
    world.bodies[id].apply_impulse(impulse);

    // mass=1, inv_mass=1, so velocity = impulse * 1 = (5,0,0)
    assert_eq!(world.bodies[id].velocity.x, Fix128::from_int(5));
    assert_eq!(world.bodies[id].velocity.y, Fix128::ZERO);

    // Static body ignores impulse
    let static_id = world.add_body(RigidBody::new_static(Vec3Fix::ZERO));
    world.bodies[static_id].apply_impulse(impulse);
    assert_eq!(world.bodies[static_id].velocity.x, Fix128::ZERO);
}

// ============================================================================
// Test 43 — apply_impulse_at generates angular velocity
// ============================================================================

#[test]
fn test_apply_impulse_at() {
    let config = PhysicsConfig::default();
    let mut world = PhysicsWorld::new(config);
    // Body at origin, mass=1
    let id = world.add_body(RigidBody::new_dynamic(Vec3Fix::ZERO, Fix128::ONE));

    // Apply impulse at offset point → should produce both linear and angular velocity
    let impulse = Vec3Fix::new(Fix128::ZERO, Fix128::from_int(10), Fix128::ZERO);
    let point = Vec3Fix::new(Fix128::ONE, Fix128::ZERO, Fix128::ZERO);
    world.bodies[id].apply_impulse_at(impulse, point);

    // Linear velocity changed
    assert_eq!(world.bodies[id].velocity.y, Fix128::from_int(10));
    // Angular velocity should be non-zero (torque = r × F = (1,0,0) × (0,10,0) = (0,0,10))
    assert!(
        world.bodies[id].angular_velocity.z != Fix128::ZERO,
        "apply_impulse_at should generate angular velocity"
    );
}

// ============================================================================
// Test 44 — add_torque modifies angular velocity
// ============================================================================

#[test]
fn test_add_torque() {
    let config = PhysicsConfig::default();
    let mut world = PhysicsWorld::new(config);
    let id = world.add_body(RigidBody::new_dynamic(Vec3Fix::ZERO, Fix128::ONE));

    let dt = Fix128::from_ratio(1, 60);
    let torque = Vec3Fix::new(Fix128::ZERO, Fix128::from_int(60), Fix128::ZERO);
    world.bodies[id].add_torque(torque, dt);

    // angular_velocity.y = torque.y * inv_inertia.y * dt
    assert!(
        world.bodies[id].angular_velocity.y != Fix128::ZERO,
        "add_torque should modify angular velocity"
    );

    // Static body ignores torque
    let static_id = world.add_body(RigidBody::new_static(Vec3Fix::ZERO));
    world.bodies[static_id].add_torque(torque, dt);
    assert_eq!(world.bodies[static_id].angular_velocity.y, Fix128::ZERO);
}

// ============================================================================
// Test 45 — set_position resets prev_position (teleport)
// ============================================================================

#[test]
fn test_set_position_resets_prev() {
    let config = PhysicsConfig::default();
    let mut world = PhysicsWorld::new(config);
    let id = world.add_body(RigidBody::new_dynamic(Vec3Fix::ZERO, Fix128::ONE));

    // Step once so prev_position diverges from position
    let dt = Fix128::from_ratio(1, 60);
    world.step(dt);
    assert!(
        world.bodies[id].position != world.bodies[id].prev_position
            || world.bodies[id].position == Vec3Fix::ZERO,
        "After step, position or prev should differ (gravity)"
    );

    // Teleport
    let new_pos = Vec3Fix::from_int(100, 200, 300);
    world.bodies[id].set_position(new_pos);
    assert_eq!(world.bodies[id].position, new_pos);
    assert_eq!(
        world.bodies[id].prev_position, new_pos,
        "set_position must reset prev_position"
    );
}

// ============================================================================
// Test 46 — set_rotation resets prev_rotation
// ============================================================================

#[test]
fn test_set_rotation_resets_prev() {
    let config = PhysicsConfig::default();
    let mut world = PhysicsWorld::new(config);
    let id = world.add_body(RigidBody::new_dynamic(Vec3Fix::ZERO, Fix128::ONE));

    // A unit quaternion (90° about z), which set_rotation stores bit for bit.
    let new_rot = QuatFix::from_axis_angle(Vec3Fix::UNIT_Z, Fix128::HALF_PI);
    world.bodies[id].set_rotation(new_rot);
    assert_eq!(world.bodies[id].rotation, new_rot);
    assert_eq!(
        world.bodies[id].prev_rotation, new_rot,
        "set_rotation must reset prev_rotation"
    );
}

// ============================================================================
// Test 47 — new_sensor creates a static sensor body
// ============================================================================

#[test]
fn test_new_sensor() {
    let pos = Vec3Fix::from_int(5, 5, 5);
    let sensor = RigidBody::new_sensor(pos);

    assert!(sensor.is_static(), "Sensor should be static");
    assert!(sensor.is_sensor, "Sensor flag must be true");
    assert_eq!(sensor.position, pos);
    assert_eq!(sensor.inv_mass, Fix128::ZERO, "Sensor has zero inv_mass");
    assert_eq!(sensor.gravity_scale, Fix128::ZERO, "Sensor ignores gravity");
}

// ============================================================================
// Test 48 — remove_joint standalone
// ============================================================================

#[test]
fn test_remove_joint() {
    let config = PhysicsConfig::default();
    let mut world = PhysicsWorld::new(config);
    let a = world.add_body(RigidBody::new_dynamic(Vec3Fix::ZERO, Fix128::ONE));
    let b = world.add_body(RigidBody::new_dynamic(Vec3Fix::UNIT_X, Fix128::ONE));

    let joint = Joint::Ball(BallJoint::new(a, b, Vec3Fix::ZERO, Vec3Fix::ZERO));
    let joint_idx = world.add_joint(joint);
    assert_eq!(world.joint_count(), 1);

    // Remove joint
    let removed = world.remove_joint(joint_idx);
    assert!(removed.is_some(), "Should return the removed joint");
    assert_eq!(world.joint_count(), 0);

    // Out-of-bounds remove returns None
    assert!(world.remove_joint(999).is_none());
}

// ============================================================================
// Test 49 — remove_force_field
// ============================================================================

#[test]
fn test_remove_force_field() {
    let config = PhysicsConfig::default();
    let mut world = PhysicsWorld::new(config);

    let field = ForceFieldInstance::new(ForceField::Directional {
        direction: Vec3Fix::UNIT_Y,
        strength: Fix128::ONE,
    });
    let idx = world.add_force_field(field);

    let removed = world.remove_force_field(idx);
    assert!(removed.is_some());

    // Out-of-bounds returns None
    assert!(world.remove_force_field(999).is_none());
}

// ============================================================================
// Test 50 — set/clear_body_collision_radius
// ============================================================================

#[test]
fn test_set_clear_body_collision_radius() {
    let config = PhysicsConfig::default();
    let mut world = PhysicsWorld::new(config);
    let id = world.add_body(RigidBody::new_dynamic(Vec3Fix::ZERO, Fix128::ONE));

    // Initially no radius (added without radius)
    // Set radius
    world.set_body_collision_radius(id, Fix128::from_int(2));

    // Verify by running a raycast that should now hit
    let origin = Vec3Fix::new(Fix128::from_int(-10), Fix128::ZERO, Fix128::ZERO);
    let direction = Vec3Fix::UNIT_X;
    let hit = world.raycast(origin, direction, Fix128::from_int(100));
    assert!(hit.is_some(), "Should hit after set_body_collision_radius");

    // Clear radius
    world.clear_body_collision_radius(id);
    let hit2 = world.raycast(origin, direction, Fix128::from_int(100));
    assert!(
        hit2.is_none(),
        "Should not hit after clear_body_collision_radius"
    );
}

// ============================================================================
// Test 51 — body_filter getter
// ============================================================================

#[test]
fn test_body_filter_getter() {
    let config = PhysicsConfig::default();
    let mut world = PhysicsWorld::new(config);
    let id = world.add_body(RigidBody::new_dynamic(Vec3Fix::ZERO, Fix128::ONE));

    // Default filter
    let default_filter = world.body_filter(id);
    assert_eq!(default_filter, CollisionFilter::DEFAULT);

    // Set custom filter
    let custom = CollisionFilter::new(0x02, 0x04);
    world.set_body_filter(id, custom);
    let got = world.body_filter(id);
    assert_eq!(got, custom);

    // Out-of-bounds returns DEFAULT
    let oob = world.body_filter(9999);
    assert_eq!(oob, CollisionFilter::DEFAULT);
}

// ============================================================================
// Test 52 — drain_contact_events / drain_trigger_events
// ============================================================================

#[test]
fn test_drain_events() {
    let config = PhysicsConfig::default();
    let mut world = PhysicsWorld::new(config);

    // Create two colliding bodies
    let a = world.add_body_with_radius(
        RigidBody::new_dynamic(Vec3Fix::ZERO, Fix128::ONE),
        Fix128::from_int(2),
    );
    let b = world.add_body_with_radius(
        RigidBody::new_static(Vec3Fix::new(Fix128::ONE, Fix128::ZERO, Fix128::ZERO)),
        Fix128::from_int(2),
    );
    let _ = (a, b);

    let dt = Fix128::from_ratio(1, 60);
    run_world(&mut world, 5, dt);

    // drain should consume events
    let contacts = world.drain_contact_events();
    // After drain, the contact_events() should be empty
    assert!(
        world.contact_events().is_empty(),
        "Events should be empty after drain"
    );

    // drain_trigger_events on non-sensor world should return empty
    let triggers = world.drain_trigger_events();
    let _ = (contacts, triggers);
}

// ============================================================================
// Test 53 — with_compliance (soft constraint)
// ============================================================================

#[test]
fn test_soft_constraint_with_compliance() {
    let config = PhysicsConfig::default();
    let mut world = PhysicsWorld::new(config);

    let a = world.add_body(RigidBody::new_dynamic(Vec3Fix::ZERO, Fix128::ONE));
    let b = world.add_body(RigidBody::new_dynamic(
        Vec3Fix::new(Fix128::from_int(5), Fix128::ZERO, Fix128::ZERO),
        Fix128::ONE,
    ));

    // Stiff constraint (compliance=0)
    let stiff = DistanceConstraint::new(a, b, Vec3Fix::ZERO, Vec3Fix::ZERO, Fix128::from_int(5))
        .with_compliance(Fix128::ZERO);
    world.add_distance_constraint(stiff);

    let dt = Fix128::from_ratio(1, 60);
    run_world(&mut world, 120, dt);
    let dist_stiff = (world.bodies[a].position - world.bodies[b].position).length();

    // Reset
    let mut world2 = PhysicsWorld::new(config);
    let a2 = world2.add_body(RigidBody::new_dynamic(Vec3Fix::ZERO, Fix128::ONE));
    let b2 = world2.add_body(RigidBody::new_dynamic(
        Vec3Fix::new(Fix128::from_int(5), Fix128::ZERO, Fix128::ZERO),
        Fix128::ONE,
    ));

    // Soft constraint (high compliance)
    let soft = DistanceConstraint::new(a2, b2, Vec3Fix::ZERO, Vec3Fix::ZERO, Fix128::from_int(5))
        .with_compliance(Fix128::from_int(10));
    world2.add_distance_constraint(soft);
    run_world(&mut world2, 120, dt);
    let dist_soft = (world2.bodies[a2].position - world2.bodies[b2].position).length();

    // Both should maintain approximate distance, but soft allows more deviation
    let _ = (dist_stiff, dist_soft);
    // The key assertion: soft constraint is valid and doesn't crash
}

// ============================================================================
// Test 54 — num_batches
// ============================================================================

#[test]
fn test_num_batches() {
    let config = PhysicsConfig::default();
    let mut world = PhysicsWorld::new(config);

    // No constraints → 0 batches
    world.rebuild_batches();
    assert_eq!(world.num_batches(), 0);

    // Add a chain of constraints
    let a = world.add_body(RigidBody::new_dynamic(Vec3Fix::ZERO, Fix128::ONE));
    let b = world.add_body(RigidBody::new_dynamic(Vec3Fix::UNIT_X, Fix128::ONE));
    let c = world.add_body(RigidBody::new_dynamic(
        Vec3Fix::new(Fix128::from_int(2), Fix128::ZERO, Fix128::ZERO),
        Fix128::ONE,
    ));

    // A-B and B-C share body B → need at least 2 batches
    world.add_distance_constraint(DistanceConstraint::new(
        a,
        b,
        Vec3Fix::ZERO,
        Vec3Fix::ZERO,
        Fix128::ONE,
    ));
    world.add_distance_constraint(DistanceConstraint::new(
        b,
        c,
        Vec3Fix::ZERO,
        Vec3Fix::ZERO,
        Fix128::ONE,
    ));
    world.rebuild_batches();
    assert!(
        world.num_batches() >= 2,
        "Chain sharing a body needs >= 2 batches"
    );
}

// ============================================================================
// Test 55 — mass=0 creates static-like body
// ============================================================================

#[test]
fn test_zero_mass_body() {
    // mass=0 in new_dynamic → inv_mass=0
    let body = RigidBody::new_dynamic(Vec3Fix::from_int(0, 10, 0), Fix128::ZERO);
    assert_eq!(body.inv_mass, Fix128::ZERO, "mass=0 → inv_mass=0");
    // is_static() checks inv_mass, so mass=0 body is considered static
    assert!(body.is_static(), "mass=0 body is_static() returns true");

    // Impulse should have no effect (is_static guard)
    let mut body2 = body;
    body2.apply_impulse(Vec3Fix::new(
        Fix128::from_int(100),
        Fix128::ZERO,
        Fix128::ZERO,
    ));
    assert_eq!(
        body2.velocity.x,
        Fix128::ZERO,
        "mass=0 body ignores impulse"
    );

    // add_torque should also have no effect
    body2.add_torque(
        Vec3Fix::new(Fix128::ZERO, Fix128::from_int(100), Fix128::ZERO),
        Fix128::from_ratio(1, 60),
    );
    assert_eq!(
        body2.angular_velocity.y,
        Fix128::ZERO,
        "mass=0 body ignores torque"
    );
}

// ============================================================================
// Test 56 — deserialization rejects empty and truncated data
// ============================================================================

#[test]
fn test_deserialize_empty_and_truncated() {
    let config = PhysicsConfig::default();
    let mut world = PhysicsWorld::new(config);
    world.add_body(RigidBody::new_dynamic(Vec3Fix::ZERO, Fix128::ONE));

    // Empty data
    assert!(!world.deserialize_state(&[]), "Empty data should fail");

    // Too short (< 4 bytes)
    assert!(!world.deserialize_state(&[0, 0]), "Short data should fail");

    // Header says 1 body but data is truncated (only 4 header bytes, no body data)
    let mut truncated = vec![0u8; 4];
    truncated[0] = 1; // count=1 in LE
    assert!(
        !world.deserialize_state(&truncated),
        "Truncated body data should fail"
    );
}

// ============================================================================
// Test 57 — self-referential joint (body_a == body_b)
// ============================================================================

#[test]
fn test_self_referential_joint() {
    let config = PhysicsConfig::default();
    let mut world = PhysicsWorld::new(config);
    let a = world.add_body(RigidBody::new_dynamic(
        Vec3Fix::from_int(0, 5, 0),
        Fix128::ONE,
    ));

    // Self-referential joint should not crash the solver
    let joint = Joint::Ball(BallJoint::new(a, a, Vec3Fix::ZERO, Vec3Fix::ZERO));
    world.add_joint(joint);

    let dt = Fix128::from_ratio(1, 60);
    // Should not panic or infinite loop
    run_world(&mut world, 60, dt);
}

// ============================================================================
// Test 58 — large dt does not panic
// ============================================================================

#[test]
fn test_large_dt() {
    let config = PhysicsConfig::default();
    let mut world = PhysicsWorld::new(config);
    world.add_body(RigidBody::new_dynamic(
        Vec3Fix::from_int(0, 10, 0),
        Fix128::ONE,
    ));

    // Very large dt — should not panic, overflow, or infinite loop
    let large_dt = Fix128::from_int(100);
    world.step(large_dt);

    // Body should have moved significantly downward
    assert!(
        world.bodies[0].position.y < Fix128::from_int(10),
        "Large dt should still produce downward motion"
    );
}

// ============================================================================
// Test 59 — ray originating inside sphere
// ============================================================================

#[test]
fn test_raycast_inside_sphere() {
    let config = PhysicsConfig::default();
    let mut world = PhysicsWorld::new(config);

    // Large sphere at origin
    world.add_body_with_radius(RigidBody::new_static(Vec3Fix::ZERO), Fix128::from_int(10));

    // Ray origin inside the sphere, pointing outward
    let origin = Vec3Fix::new(Fix128::ONE, Fix128::ZERO, Fix128::ZERO);
    let direction = Vec3Fix::UNIT_X;
    let hit = world.raycast(origin, direction, Fix128::from_int(100));

    // Should hit the far side of the sphere
    assert!(
        hit.is_some(),
        "Ray inside sphere should hit far intersection"
    );
    let (idx, t) = hit.unwrap();
    assert_eq!(idx, 0);
    assert!(
        t > Fix128::ZERO,
        "Hit distance should be positive (far side)"
    );
}

// ============================================================================
// Test 60 — remove_sdf_collider
// ============================================================================

#[test]
fn test_remove_sdf_collider() {
    use alice_physics::sdf_collider::{ClosureSdf, SdfCollider};

    let config = PhysicsConfig::default();
    let mut world = PhysicsWorld::new(config);

    // Add a ground-plane SDF collider
    let ground = ClosureSdf::new(|_x, y, _z| y, |_x, _y, _z| (0.0, 1.0, 0.0));
    let collider = SdfCollider::new_static(Box::new(ground), Vec3Fix::ZERO, QuatFix::IDENTITY);
    let idx = world.add_sdf_collider(collider);

    // Remove it
    let removed = world.remove_sdf_collider(idx);
    assert!(removed.is_some());

    // Out-of-bounds returns None
    assert!(world.remove_sdf_collider(999).is_none());
}

// ============================================================================
// Test 61 — remove_body index 0 from single-body world
// ============================================================================

#[test]
fn test_remove_only_body() {
    let config = PhysicsConfig::default();
    let mut world = PhysicsWorld::new(config);
    world.add_body(RigidBody::new_dynamic(Vec3Fix::ZERO, Fix128::ONE));
    assert_eq!(world.bodies.len(), 1);

    world.remove_body(0);
    assert_eq!(world.bodies.len(), 0);

    // World should still step without panic
    let dt = Fix128::from_ratio(1, 60);
    world.step(dt);
}

// ============================================================================
// Test 62 — raycast BVH acceleration produces same results as distance check
// ============================================================================

#[test]
fn test_raycast_bvh_correctness() {
    let config = PhysicsConfig::default();
    let mut world = PhysicsWorld::new(config);

    // Place 5 spheres along the X axis
    for i in 0..5 {
        world.add_body_with_radius(
            RigidBody::new_static(Vec3Fix::new(
                Fix128::from_int(i * 10),
                Fix128::ZERO,
                Fix128::ZERO,
            )),
            Fix128::from_int(2),
        );
    }

    // Ray from far left → should hit body 0 first
    let origin = Vec3Fix::new(Fix128::from_int(-20), Fix128::ZERO, Fix128::ZERO);
    let direction = Vec3Fix::UNIT_X;
    let hit = world.raycast(origin, direction, Fix128::from_int(200));
    assert!(hit.is_some());
    let (idx, _) = hit.unwrap();
    assert_eq!(idx, 0, "Nearest sphere should be body 0");

    // Ray from far right → should hit body 4 first
    let origin2 = Vec3Fix::new(Fix128::from_int(60), Fix128::ZERO, Fix128::ZERO);
    let dir2 = Vec3Fix::new(-Fix128::ONE, Fix128::ZERO, Fix128::ZERO);
    let hit2 = world.raycast(origin2, dir2, Fix128::from_int(200));
    assert!(hit2.is_some());
    let (idx2, _) = hit2.unwrap();
    assert_eq!(idx2, 4, "Nearest sphere from right should be body 4");
}

// ============================================================================
// Stress Test — 200 bodies with joints and forces
// ============================================================================

/// Simulate 10 dynamic bodies with gravity, constraints, and force fields.
/// Verifies the solver does not panic or diverge under load.
#[test]
fn test_stress_10_bodies_with_constraints() {
    let config = PhysicsConfig {
        substeps: 2,
        iterations: 2,
        ..PhysicsConfig::default()
    };
    let mut world = PhysicsWorld::new(config);

    // Line of 10 bodies
    for i in 0..10 {
        let x = i64::from(i) * 3;
        let body = RigidBody::new_dynamic(Vec3Fix::from_int(x, 20, 0), Fix128::ONE);
        world.add_body_with_radius(body, Fix128::ONE);
    }
    assert_eq!(world.bodies.len(), 10);

    // Chain of distance constraints
    for i in 0..9 {
        let constraint =
            DistanceConstraint::new(i, i + 1, Vec3Fix::ZERO, Vec3Fix::ZERO, Fix128::from_int(3));
        world.distance_constraints.push(constraint);
    }

    // Add a directional wind force
    world
        .force_fields
        .push(ForceFieldInstance::new(ForceField::Directional {
            direction: Vec3Fix::UNIT_X,
            strength: Fix128::from_int(2),
        }));

    let dt = Fix128::from_ratio(1, 60);
    run_world(&mut world, 10, dt);

    // Verify: all bodies have finite positions
    for (i, body) in world.bodies.iter().enumerate() {
        let pos_magnitude = body.position.length();
        assert!(
            pos_magnitude < Fix128::from_int(10000),
            "Body {i} diverged: position magnitude too large"
        );
    }
}

// ============================================================================
// Per-body damping test
// ============================================================================

/// Verify that per-body damping reduces velocity more than global damping alone.
#[test]
fn test_per_body_damping() {
    let config = PhysicsConfig::default();
    let mut world = PhysicsWorld::new(config);

    // Body A: default damping (1.0 = no extra damping)
    let body_a = RigidBody::new_dynamic(Vec3Fix::from_int(0, 10, 0), Fix128::ONE)
        .with_velocity(Vec3Fix::from_int(10, 0, 0));
    world.add_body(body_a);

    // Body B: heavy damping (0.5 = 50% velocity retained per substep)
    let body_b = RigidBody::new_dynamic(Vec3Fix::from_int(0, 10, 10), Fix128::ONE)
        .with_velocity(Vec3Fix::from_int(10, 0, 0))
        .with_linear_damping(Fix128::from_ratio(5, 10));
    world.add_body(body_b);

    let dt = Fix128::from_ratio(1, 60);
    run_world(&mut world, 60, dt);

    // Body B should have moved less than body A in the X direction
    let x_a = world.bodies[0].position.x;
    let x_b = world.bodies[1].position.x;
    assert!(x_b < x_a, "Heavily damped body should travel less distance");
}

// ============================================================================
// Builder pattern test
// ============================================================================

#[test]
fn test_rigid_body_builder() {
    let body = RigidBody::new_dynamic(Vec3Fix::from_int(0, 5, 0), Fix128::ONE)
        .with_restitution(Fix128::from_ratio(8, 10))
        .with_friction(Fix128::from_ratio(5, 10))
        .with_gravity_scale(Fix128::from_int(2))
        .with_velocity(Vec3Fix::from_int(1, 0, 0))
        .with_rotation(QuatFix::IDENTITY);

    assert_eq!(body.restitution, Fix128::from_ratio(8, 10));
    assert_eq!(body.friction, Fix128::from_ratio(5, 10));
    assert_eq!(body.gravity_scale, Fix128::from_int(2));
    assert_eq!(body.velocity, Vec3Fix::from_int(1, 0, 0));
}

// ============================================================================
// Display trait test
// ============================================================================

#[test]
fn test_display_traits() {
    let v = Vec3Fix::from_int(1, 2, 3);
    let s = format!("{v}");
    assert!(s.contains('1'), "Vec3Fix display should contain components");

    let q = QuatFix::IDENTITY;
    let s = format!("{q}");
    assert!(s.contains('1'), "QuatFix display should contain w=1");

    let f = Fix128::from_ratio(3, 2);
    let s = format!("{f}");
    assert!(s.contains("1.5"), "Fix128(1.5) display should contain 1.5");
}

// ============================================================================
// From/Into conversion test
// ============================================================================

#[test]
fn test_from_into_conversions() {
    let f: Fix128 = 42i64.into();
    assert_eq!(f, Fix128::from_int(42));

    let f32: Fix128 = 7i32.into();
    assert_eq!(f32, Fix128::from_int(7));

    let arr: [Fix128; 3] = Vec3Fix::from_int(1, 2, 3).into();
    assert_eq!(arr[0], Fix128::from_int(1));
    assert_eq!(arr[1], Fix128::from_int(2));
    assert_eq!(arr[2], Fix128::from_int(3));

    let v: Vec3Fix = arr.into();
    assert_eq!(v, Vec3Fix::from_int(1, 2, 3));
}

// ============================================================================
// PhysicsWorld Debug test
// ============================================================================

#[test]
fn test_physics_world_debug() {
    let world = PhysicsWorld::new(PhysicsConfig::default());
    let s = format!("{world:?}");
    assert!(s.contains("PhysicsWorld"));
    assert!(s.contains("bodies"));
}

// ============================================================================
// RigidBody Default test
// ============================================================================

#[test]
fn test_rigid_body_default() {
    let body = RigidBody::default();
    assert_eq!(body.position, Vec3Fix::ZERO);
    assert!(body.is_dynamic());
    assert!(!body.is_static());
}

// ============================================================================
// New CCD functions test
// ============================================================================

#[test]
fn test_new_ccd_functions() {
    use alice_physics::ccd::{aabb_plane_toi, capsule_plane_toi, sphere_capsule_toi};

    // Capsule falling toward ground
    let toi = capsule_plane_toi(
        Vec3Fix::from_int(0, 10, -1),
        Vec3Fix::from_int(0, 10, 1),
        Fix128::ONE,
        Vec3Fix::from_int(0, -20, 0),
        Vec3Fix::UNIT_Y,
        Fix128::ZERO,
    );
    assert!(toi.is_some(), "Capsule should hit ground plane");

    // Sphere hitting capsule
    let toi2 = sphere_capsule_toi(
        Vec3Fix::from_int(-10, 0, 0),
        Fix128::ONE,
        Vec3Fix::from_int(20, 0, 0),
        Vec3Fix::from_int(5, -2, 0),
        Vec3Fix::from_int(5, 2, 0),
        Fix128::ONE,
    );
    assert!(toi2.is_some(), "Sphere should hit capsule");

    // AABB hitting ground
    let aabb = AABB::new(Vec3Fix::from_int(-1, 8, -1), Vec3Fix::from_int(1, 10, 1));
    let toi3 = aabb_plane_toi(
        &aabb,
        Vec3Fix::from_int(0, -20, 0),
        Vec3Fix::UNIT_Y,
        Fix128::ZERO,
    );
    assert!(toi3.is_some(), "AABB should hit ground plane");
}

// ============================================================================
// Error variants test
// ============================================================================

#[test]
fn test_error_new_variants() {
    use alice_physics::PhysicsError;

    let e1 = PhysicsError::CapacityExceeded {
        resource: "bodies",
        limit: 10000,
    };
    assert!(format!("{e1}").contains("10000"));

    let e2 = PhysicsError::InvalidConfiguration {
        reason: "substeps must be > 0",
    };
    assert!(format!("{e2}").contains("substeps"));
}

// ============================================================================
// HeightField Clone/Debug test
// ============================================================================

#[test]
fn test_heightfield_derives() {
    use alice_physics::HeightField;

    let hf = HeightField::flat(4, 4, Fix128::ONE, Vec3Fix::ZERO, Fix128::ZERO);
    let hf2 = hf.clone();
    assert_eq!(hf, hf2, "HeightField should be cloneable and comparable");
    let s = format!("{hf:?}");
    assert!(s.contains("HeightField"));
}