euv-engine 0.9.1

A high-performance 2D game engine built on the euv framework, featuring ECS, fixed-timestep game loop, canvas rendering, physics, collision detection, sprite animation, and audio.
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
use crate::*;

/// Implements static math utility methods on the `Numeric` namespace struct.
impl Numeric {
    /// Clamps a value between a minimum and maximum bound.
    ///
    /// # Arguments
    ///
    /// - `f64` - The value to clamp.
    /// - `f64` - The minimum allowed value.
    /// - `f64` - The maximum allowed value.
    ///
    /// # Returns
    ///
    /// - `f64` - The clamped value.
    pub fn clamp(value: f64, min: f64, max: f64) -> f64 {
        value.max(min).min(max)
    }

    /// Performs linear interpolation between two values.
    ///
    /// # Arguments
    ///
    /// - `f64` - The start value.
    /// - `f64` - The end value.
    /// - `f64` - The interpolation factor, typically in the range 0.0 to 1.0.
    ///
    /// # Returns
    ///
    /// - `f64` - The interpolated value.
    pub fn lerp(start: f64, end: f64, t: f64) -> f64 {
        start + (end - start) * t
    }

    /// Converts an angle from degrees to radians.
    ///
    /// # Arguments
    ///
    /// - `f64` - The angle in degrees.
    ///
    /// # Returns
    ///
    /// - `f64` - The angle in radians.
    pub fn deg_to_rad(degrees: f64) -> f64 {
        degrees * DEG_TO_RAD
    }

    /// Converts an angle from radians to degrees.
    ///
    /// # Arguments
    ///
    /// - `f64` - The angle in radians.
    ///
    /// # Returns
    ///
    /// - `f64` - The angle in degrees.
    pub fn rad_to_deg(radians: f64) -> f64 {
        radians * RAD_TO_DEG
    }

    /// Normalizes an angle to the range -PI to PI.
    ///
    /// # Arguments
    ///
    /// - `f64` - The angle in radians.
    ///
    /// # Returns
    ///
    /// - `f64` - The normalized angle in the range -PI to PI.
    pub fn normalize_angle(radians: f64) -> f64 {
        let mut angle: f64 = radians % TWO_PI;
        if angle < -PI {
            angle += TWO_PI;
        }
        if angle > PI {
            angle -= TWO_PI;
        }
        angle
    }

    /// Computes the shortest angular difference between two angles.
    ///
    /// # Arguments
    ///
    /// - `f64` - The source angle in radians.
    /// - `f64` - The target angle in radians.
    ///
    /// # Returns
    ///
    /// - `f64` - The signed angular delta in the range -PI to PI.
    pub fn angle_delta(from: f64, to: f64) -> f64 {
        Self::normalize_angle(to - from)
    }

    /// Performs angular interpolation taking the shortest path around the circle.
    ///
    /// # Arguments
    ///
    /// - `f64` - The source angle in radians.
    /// - `f64` - The target angle in radians.
    /// - `f64` - The interpolation factor, typically in the range 0.0 to 1.0.
    ///
    /// # Returns
    ///
    /// - `f64` - The interpolated angle in radians.
    pub fn lerp_angle(from: f64, to: f64, t: f64) -> f64 {
        from + Self::angle_delta(from, to) * t
    }

    /// Computes the Euclidean distance between two 2D points.
    ///
    /// # Arguments
    ///
    /// - `Vector2D` - The first point.
    /// - `Vector2D` - The second point.
    ///
    /// # Returns
    ///
    /// - `f64` - The distance between the two points.
    pub fn distance(a: Vector2D, b: Vector2D) -> f64 {
        (b - a).magnitude()
    }

    /// Computes the squared Euclidean distance between two 2D points.
    ///
    /// Avoids a square root, making it faster for comparison-only use cases.
    ///
    /// # Arguments
    ///
    /// - `Vector2D` - The first point.
    /// - `Vector2D` - The second point.
    ///
    /// # Returns
    ///
    /// - `f64` - The squared distance between the two points.
    pub fn distance_squared(a: Vector2D, b: Vector2D) -> f64 {
        (b - a).magnitude_squared()
    }

    /// Computes a smoothstep interpolation factor using a cubic Hermite polynomial.
    ///
    /// # Arguments
    ///
    /// - `f64` - The edge minimum.
    /// - `f64` - The edge maximum.
    /// - `f64` - The input value.
    ///
    /// # Returns
    ///
    /// - `f64` - The smoothstep result in the range 0.0 to 1.0.
    pub fn smoothstep(edge_min: f64, edge_max: f64, value: f64) -> f64 {
        let clamped: f64 = Self::clamp((value - edge_min) / (edge_max - edge_min), 0.0, 1.0);
        clamped * clamped * (3.0 - 2.0 * clamped)
    }

    /// Moves `current` towards `target` by at most `max_delta`.
    ///
    /// # Arguments
    ///
    /// - `f64` - The current value.
    /// - `f64` - The target value.
    /// - `f64` - The maximum allowed change.
    ///
    /// # Returns
    ///
    /// - `f64` - The new value moved towards target.
    pub fn approach(current: f64, target: f64, max_delta: f64) -> f64 {
        if (target - current).abs() <= max_delta {
            return target;
        }
        current + max_delta.signum() * max_delta
    }

    /// Returns the sign of a value as -1.0, 0.0, or 1.0.
    ///
    /// # Arguments
    ///
    /// - `f64` - The input value.
    ///
    /// # Returns
    ///
    /// - `f64` - -1.0 if negative, 0.0 if zero, 1.0 if positive.
    pub fn sign(value: f64) -> f64 {
        if value > 0.0 {
            1.0
        } else if value < 0.0 {
            -1.0
        } else {
            0.0
        }
    }

    /// Wraps a value into the range 0.0 to `max`.
    ///
    /// # Arguments
    ///
    /// - `f64` - The value to wrap.
    /// - `f64` - The upper bound of the range.
    ///
    /// # Returns
    ///
    /// - `f64` - The wrapped value in the range 0.0 to `max`.
    pub fn wrap(value: f64, max: f64) -> f64 {
        let result: f64 = value % max;
        if result < 0.0 { result + max } else { result }
    }

    /// Returns 1.0 if the value is positive, -1.0 otherwise.
    ///
    /// # Arguments
    ///
    /// - `f64` - The input value.
    ///
    /// # Returns
    ///
    /// - `f64` - 1.0 if the value is non-negative, -1.0 otherwise.
    pub fn sign_or_positive(value: f64) -> f64 {
        if value < 0.0 { -1.0 } else { 1.0 }
    }

    /// Computes the Euclidean distance between two 3D points.
    ///
    /// # Arguments
    ///
    /// - `Vector3D` - The first point.
    /// - `Vector3D` - The second point.
    ///
    /// # Returns
    ///
    /// - `f64` - The distance between the two points.
    pub fn distance_3d(a: Vector3D, b: Vector3D) -> f64 {
        (b - a).magnitude()
    }

    /// Computes the squared Euclidean distance between two 3D points.
    ///
    /// Avoids a square root, making it faster for comparison-only use cases.
    ///
    /// # Arguments
    ///
    /// - `Vector3D` - The first point.
    /// - `Vector3D` - The second point.
    ///
    /// # Returns
    ///
    /// - `f64` - The squared distance between the two points.
    pub fn distance_squared_3d(a: Vector3D, b: Vector3D) -> f64 {
        (b - a).magnitude_squared()
    }
}

/// Implements the `Interpolable` trait for `f64`.
impl Interpolable for f64 {
    fn lerp(&self, other: f64, t: f64) -> f64 {
        *self + (other - *self) * t
    }
}

/// Implements methods and operator overloading for `Vector2D`.
impl Vector2D {
    /// Returns the zero vector (0.0, 0.0).
    ///
    /// # Returns
    ///
    /// - `Vector2D` - The zero vector.
    pub fn zero() -> Vector2D {
        Vector2D::new(0.0, 0.0)
    }

    /// Returns the unit vector pointing right (1.0, 0.0).
    ///
    /// # Returns
    ///
    /// - `Vector2D` - The right unit vector.
    pub fn right() -> Vector2D {
        Vector2D::new(1.0, 0.0)
    }

    /// Returns the unit vector pointing up (0.0, -1.0).
    ///
    /// In screen coordinates where y increases downward.
    ///
    /// # Returns
    ///
    /// - `Vector2D` - The up unit vector.
    pub fn up() -> Vector2D {
        Vector2D::new(0.0, -1.0)
    }

    /// Creates a unit vector from an angle in radians.
    ///
    /// # Arguments
    ///
    /// - `f64` - The angle in radians.
    ///
    /// # Returns
    ///
    /// - `Vector2D` - The unit vector pointing in the given direction.
    pub fn from_angle(radians: f64) -> Vector2D {
        Vector2D::new(radians.cos(), radians.sin())
    }

    /// Returns the magnitude (length) of the vector.
    ///
    /// # Returns
    ///
    /// - `f64` - The magnitude of the vector.
    pub fn magnitude(&self) -> f64 {
        (self.get_x() * self.get_x() + self.get_y() * self.get_y()).sqrt()
    }

    /// Returns the squared magnitude of the vector.
    ///
    /// Avoids a square root, making it faster for comparison-only use cases.
    ///
    /// # Returns
    ///
    /// - `f64` - The squared magnitude of the vector.
    pub fn magnitude_squared(&self) -> f64 {
        self.get_x() * self.get_x() + self.get_y() * self.get_y()
    }

    /// Returns a normalized (unit length) copy of this vector.
    ///
    /// Returns the zero vector if the magnitude is zero.
    ///
    /// # Returns
    ///
    /// - `Vector2D` - The normalized vector.
    pub fn normalized(&self) -> Vector2D {
        let mag: f64 = self.magnitude();
        if mag < EPSILON {
            return Vector2D::zero();
        }
        Vector2D::new(self.get_x() / mag, self.get_y() / mag)
    }

    /// Normalizes this vector in place.
    pub fn normalize(&mut self) {
        let mag: f64 = self.magnitude();
        if mag < EPSILON {
            self.set_x(0.0);
            self.set_y(0.0);
            return;
        }
        self.set_x(self.get_x() / mag);
        self.set_y(self.get_y() / mag);
    }

    /// Computes the dot product with another vector.
    ///
    /// # Arguments
    ///
    /// - `Vector2D` - The other vector.
    ///
    /// # Returns
    ///
    /// - `f64` - The dot product.
    pub fn dot(&self, other: Vector2D) -> f64 {
        self.get_x() * other.get_x() + self.get_y() * other.get_y()
    }

    /// Computes the 2D cross product (scalar) with another vector.
    ///
    /// # Arguments
    ///
    /// - `Vector2D` - The other vector.
    ///
    /// # Returns
    ///
    /// - `f64` - The cross product scalar.
    pub fn cross(&self, other: Vector2D) -> f64 {
        self.get_x() * other.get_y() - self.get_y() * other.get_x()
    }

    /// Returns the perpendicular vector (rotated 90 degrees counter-clockwise).
    ///
    /// # Returns
    ///
    /// - `Vector2D` - The perpendicular vector.
    pub fn perp(&self) -> Vector2D {
        Vector2D::new(-self.get_y(), self.get_x())
    }

    /// Returns the angle of this vector in radians.
    ///
    /// # Returns
    ///
    /// - `f64` - The angle in radians.
    pub fn angle(&self) -> f64 {
        self.get_y().atan2(self.get_x())
    }

    /// Returns the angle from this vector to another.
    ///
    /// # Arguments
    ///
    /// - `Vector2D` - The target vector.
    ///
    /// # Returns
    ///
    /// - `f64` - The signed angle in radians.
    pub fn angle_to(&self, other: Vector2D) -> f64 {
        (other - *self).angle()
    }

    /// Returns a rotated copy of this vector.
    ///
    /// # Arguments
    ///
    /// - `f64` - The rotation angle in radians.
    ///
    /// # Returns
    ///
    /// - `Vector2D` - The rotated vector.
    pub fn rotated(&self, radians: f64) -> Vector2D {
        let cos: f64 = radians.cos();
        let sin: f64 = radians.sin();
        Vector2D::new(
            self.get_x() * cos - self.get_y() * sin,
            self.get_x() * sin + self.get_y() * cos,
        )
    }

    /// Rotates this vector in place.
    ///
    /// # Arguments
    ///
    /// - `f64` - The rotation angle in radians.
    pub fn rotate(&mut self, radians: f64) {
        let cos: f64 = radians.cos();
        let sin: f64 = radians.sin();
        let new_x: f64 = self.get_x() * cos - self.get_y() * sin;
        let new_y: f64 = self.get_x() * sin + self.get_y() * cos;
        self.set_x(new_x);
        self.set_y(new_y);
    }

    /// Returns the distance from this point to another.
    ///
    /// # Arguments
    ///
    /// - `Vector2D` - The target point.
    ///
    /// # Returns
    ///
    /// - `f64` - The Euclidean distance.
    pub fn distance_to(&self, other: Vector2D) -> f64 {
        (other - *self).magnitude()
    }

    /// Returns the squared distance from this point to another.
    ///
    /// # Arguments
    ///
    /// - `Vector2D` - The target point.
    ///
    /// # Returns
    ///
    /// - `f64` - The squared Euclidean distance.
    pub fn distance_squared_to(&self, other: Vector2D) -> f64 {
        (other - *self).magnitude_squared()
    }

    /// Returns a unit vector pointing from this point to another.
    ///
    /// # Arguments
    ///
    /// - `Vector2D` - The target point.
    ///
    /// # Returns
    ///
    /// - `Vector2D` - The direction unit vector.
    pub fn direction_to(&self, other: Vector2D) -> Vector2D {
        (other - *self).normalized()
    }

    /// Returns a linearly interpolated vector between this and another.
    ///
    /// # Arguments
    ///
    /// - `Vector2D` - The target vector.
    /// - `f64` - The interpolation factor.
    ///
    /// # Returns
    ///
    /// - `Vector2D` - The interpolated vector.
    pub fn lerp(&self, other: Vector2D, t: f64) -> Vector2D {
        Vector2D::new(
            self.get_x() + (other.get_x() - self.get_x()) * t,
            self.get_y() + (other.get_y() - self.get_y()) * t,
        )
    }

    /// Scales this vector by a scalar factor.
    ///
    /// # Arguments
    ///
    /// - `f64` - The scalar factor.
    pub fn scale(&mut self, scalar: f64) {
        self.set_x(self.get_x() * scalar);
        self.set_y(self.get_y() * scalar);
    }

    /// Returns a scaled copy of this vector.
    ///
    /// # Arguments
    ///
    /// - `f64` - The scalar factor.
    ///
    /// # Returns
    ///
    /// - `Vector2D` - The scaled vector.
    pub fn scaled(&self, scalar: f64) -> Vector2D {
        Vector2D::new(self.get_x() * scalar, self.get_y() * scalar)
    }
}

/// Implements `Interpolable` for `Vector2D`.
impl Interpolable for Vector2D {
    fn lerp(&self, other: Vector2D, t: f64) -> Vector2D {
        Vector2D::lerp(self, other, t)
    }
}

/// Implements vector addition.
impl Add for Vector2D {
    type Output = Vector2D;
    fn add(self, other: Vector2D) -> Vector2D {
        Vector2D::new(self.get_x() + other.get_x(), self.get_y() + other.get_y())
    }
}

/// Implements vector subtraction.
impl Sub for Vector2D {
    type Output = Vector2D;
    fn sub(self, other: Vector2D) -> Vector2D {
        Vector2D::new(self.get_x() - other.get_x(), self.get_y() - other.get_y())
    }
}

/// Implements scalar multiplication.
impl Mul<f64> for Vector2D {
    type Output = Vector2D;
    fn mul(self, scalar: f64) -> Vector2D {
        Vector2D::new(self.get_x() * scalar, self.get_y() * scalar)
    }
}

/// Implements vector negation.
impl Neg for Vector2D {
    type Output = Vector2D;
    fn neg(self) -> Vector2D {
        Vector2D::new(-self.get_x(), -self.get_y())
    }
}

/// Implements in-place vector addition.
impl AddAssign for Vector2D {
    fn add_assign(&mut self, other: Vector2D) {
        self.set_x(self.get_x() + other.get_x());
        self.set_y(self.get_y() + other.get_y());
    }
}

/// Implements in-place vector subtraction.
impl SubAssign for Vector2D {
    fn sub_assign(&mut self, other: Vector2D) {
        self.set_x(self.get_x() - other.get_x());
        self.set_y(self.get_y() - other.get_y());
    }
}

/// Implements in-place scalar multiplication.
impl MulAssign<f64> for Vector2D {
    fn mul_assign(&mut self, scalar: f64) {
        self.set_x(self.get_x() * scalar);
        self.set_y(self.get_y() * scalar);
    }
}

/// Implements methods for `Rect`.
impl Rect {
    /// Creates a rectangle from a center point and dimensions.
    ///
    /// # Arguments
    ///
    /// - `Vector2D` - The center point.
    /// - `f64` - The width.
    /// - `f64` - The height.
    ///
    /// # Returns
    ///
    /// - `Rect` - The new rectangle.
    pub fn from_center(center: Vector2D, width: f64, height: f64) -> Rect {
        Rect::new(
            center.get_x() - width * 0.5,
            center.get_y() - height * 0.5,
            width,
            height,
        )
    }

    /// Returns the center point of the rectangle.
    ///
    /// # Returns
    ///
    /// - `Vector2D` - The center point.
    pub fn center(&self) -> Vector2D {
        Vector2D::new(
            self.get_x() + self.get_width() * 0.5,
            self.get_y() + self.get_height() * 0.5,
        )
    }

    /// Returns the minimum corner (top-left).
    ///
    /// # Returns
    ///
    /// - `Vector2D` - The minimum corner.
    pub fn min(&self) -> Vector2D {
        Vector2D::new(self.get_x(), self.get_y())
    }

    /// Returns the maximum corner (bottom-right).
    ///
    /// # Returns
    ///
    /// - `Vector2D` - The maximum corner.
    pub fn max(&self) -> Vector2D {
        Vector2D::new(
            self.get_x() + self.get_width(),
            self.get_y() + self.get_height(),
        )
    }

    /// Returns the size as a vector.
    ///
    /// # Returns
    ///
    /// - `Vector2D` - The size vector.
    pub fn size(&self) -> Vector2D {
        Vector2D::new(self.get_width(), self.get_height())
    }

    /// Tests whether a point is inside this rectangle.
    ///
    /// # Arguments
    ///
    /// - `Vector2D` - The point to test.
    ///
    /// # Returns
    ///
    /// - `bool` - True if the point is inside.
    pub fn contains(&self, point: Vector2D) -> bool {
        point.get_x() >= self.get_x()
            && point.get_x() <= self.get_x() + self.get_width()
            && point.get_y() >= self.get_y()
            && point.get_y() <= self.get_y() + self.get_height()
    }

    /// Tests whether this rectangle intersects another.
    ///
    /// # Arguments
    ///
    /// - `Rect` - The other rectangle.
    ///
    /// # Returns
    ///
    /// - `bool` - True if they intersect.
    pub fn intersects(&self, other: Rect) -> bool {
        self.get_x() < other.get_x() + other.get_width()
            && self.get_x() + self.get_width() > other.get_x()
            && self.get_y() < other.get_y() + other.get_height()
            && self.get_y() + self.get_height() > other.get_y()
    }

    /// Returns the intersection of two rectangles, or `None` if they do not overlap.
    ///
    /// # Arguments
    ///
    /// - `Rect` - The other rectangle.
    ///
    /// # Returns
    ///
    /// - `Option<Rect>` - The intersection rectangle, or `None`.
    pub fn intersection(&self, other: Rect) -> Option<Rect> {
        if !self.intersects(other) {
            return None;
        }
        let max_x: f64 = self.get_x().max(other.get_x());
        let max_y: f64 = self.get_y().max(other.get_y());
        let min_right: f64 =
            (self.get_x() + self.get_width()).min(other.get_x() + other.get_width());
        let min_bottom: f64 =
            (self.get_y() + self.get_height()).min(other.get_y() + other.get_height());
        Some(Rect::new(
            max_x,
            max_y,
            min_right - max_x,
            min_bottom - max_y,
        ))
    }
}

/// Implements methods for `Circle`.
impl Circle {
    /// Tests whether a point is inside this circle.
    ///
    /// # Arguments
    ///
    /// - `Vector2D` - The point to test.
    ///
    /// # Returns
    ///
    /// - `bool` - True if the point is inside.
    pub fn contains(&self, point: Vector2D) -> bool {
        self.get_center().distance_squared_to(point) <= self.get_radius() * self.get_radius()
    }

    /// Tests whether this circle intersects another.
    ///
    /// # Arguments
    ///
    /// - `Circle` - The other circle.
    ///
    /// # Returns
    ///
    /// - `bool` - True if they intersect.
    pub fn intersects(&self, other: Circle) -> bool {
        let distance_sq: f64 = self.get_center().distance_squared_to(other.get_center());
        let radius_sum: f64 = self.get_radius() + other.get_radius();
        distance_sq <= radius_sum * radius_sum
    }

    /// Returns the circumference of the circle.
    ///
    /// # Returns
    ///
    /// - `f64` - The circumference.
    pub fn circumference(&self) -> f64 {
        TWO_PI * self.get_radius()
    }

    /// Returns the area of the circle.
    ///
    /// # Returns
    ///
    /// - `f64` - The area.
    pub fn area(&self) -> f64 {
        PI * self.get_radius() * self.get_radius()
    }
}

/// Implements methods for `Transform2D`.
impl Transform2D {
    /// Creates a new transform at the origin with no rotation and unit scale.
    ///
    /// # Returns
    ///
    /// - `Transform2D` - The identity transform.
    pub fn identity() -> Transform2D {
        Transform2D::new(Vector2D::zero(), 0.0, Vector2D::new(1.0, 1.0))
    }

    /// Translates the position by the given offset.
    ///
    /// # Arguments
    ///
    /// - `Vector2D` - The translation offset.
    pub fn translate(&mut self, offset: Vector2D) {
        self.set_position(self.get_position() + offset);
    }

    /// Rotates by the given angle in radians.
    ///
    /// # Arguments
    ///
    /// - `f64` - The rotation delta in radians.
    pub fn rotate(&mut self, radians: f64) {
        self.set_rotation(self.get_rotation() + radians);
    }

    /// Scales by the given factors.
    ///
    /// # Arguments
    ///
    /// - `Vector2D` - The scale factors.
    pub fn scale_by(&mut self, factors: Vector2D) {
        let mut scale: Vector2D = self.get_scale();
        scale.set_x(scale.get_x() * factors.get_x());
        scale.set_y(scale.get_y() * factors.get_y());
        self.set_scale(scale);
    }

    /// Applies this transform to a local-space point, returning world-space coordinates.
    ///
    /// # Arguments
    ///
    /// - `Vector2D` - The local-space point.
    ///
    /// # Returns
    ///
    /// - `Vector2D` - The transformed world-space point.
    pub fn apply_to_point(&self, point: Vector2D) -> Vector2D {
        let scaled: Vector2D = Vector2D::new(
            point.get_x() * self.get_scale().get_x(),
            point.get_y() * self.get_scale().get_y(),
        );
        scaled.rotated(self.get_rotation()) + self.get_position()
    }
}

/// Implements `Default` for `Transform2D` as the identity transform.
impl Default for Transform2D {
    fn default() -> Transform2D {
        Transform2D::identity()
    }
}

/// Implements methods for `Color`.
impl Color {
    /// Creates a color from RGB hex values (0-255), with full opacity.
    ///
    /// # Arguments
    ///
    /// - `u8` - The red channel (0-255).
    /// - `u8` - The green channel (0-255).
    /// - `u8` - The blue channel (0-255).
    ///
    /// # Returns
    ///
    /// - `Color` - The new color.
    pub fn from_rgb(red: u8, green: u8, blue: u8) -> Color {
        Color::new(
            red as f64 / 255.0,
            green as f64 / 255.0,
            blue as f64 / 255.0,
            1.0,
        )
    }

    /// Converts the color to a CSS `rgba()` string.
    ///
    /// # Returns
    ///
    /// - `String` - The CSS color string.
    pub fn to_css_rgba(&self) -> String {
        format!(
            "rgba({}, {}, {}, {})",
            (self.get_red() * 255.0).round() as i32,
            (self.get_green() * 255.0).round() as i32,
            (self.get_blue() * 255.0).round() as i32,
            self.get_alpha()
        )
    }

    /// Returns black (0, 0, 0, 1).
    ///
    /// # Returns
    ///
    /// - `Color` - The black color.
    pub fn black() -> Color {
        Color::new(0.0, 0.0, 0.0, 1.0)
    }

    /// Returns white (1, 1, 1, 1).
    ///
    /// # Returns
    ///
    /// - `Color` - The white color.
    pub fn white() -> Color {
        Color::new(1.0, 1.0, 1.0, 1.0)
    }

    /// Returns transparent (0, 0, 0, 0).
    ///
    /// # Returns
    ///
    /// - `Color` - The transparent color.
    pub fn transparent() -> Color {
        Color::new(0.0, 0.0, 0.0, 0.0)
    }
}

/// Implements `Default` for `Color` as opaque black.
impl Default for Color {
    fn default() -> Color {
        Color::black()
    }
}

/// Implements methods and operator overloading for `Vector3D`.
impl Vector3D {
    /// Returns the zero vector (0.0, 0.0, 0.0).
    ///
    /// # Returns
    ///
    /// - `Vector3D` - The zero vector.
    pub fn zero() -> Vector3D {
        Vector3D::new(0.0, 0.0, 0.0)
    }

    /// Returns the unit vector pointing right (1.0, 0.0, 0.0).
    ///
    /// # Returns
    ///
    /// - `Vector3D` - The right unit vector.
    pub fn right() -> Vector3D {
        Vector3D::new(1.0, 0.0, 0.0)
    }

    /// Returns the unit vector pointing up (0.0, 1.0, 0.0).
    ///
    /// # Returns
    ///
    /// - `Vector3D` - The up unit vector.
    pub fn up() -> Vector3D {
        Vector3D::new(0.0, 1.0, 0.0)
    }

    /// Returns the unit vector pointing forward (0.0, 0.0, -1.0).
    ///
    /// In a right-handed coordinate system where -z is forward.
    ///
    /// # Returns
    ///
    /// - `Vector3D` - The forward unit vector.
    pub fn forward() -> Vector3D {
        Vector3D::new(0.0, 0.0, -1.0)
    }

    /// Returns the magnitude (length) of the vector.
    ///
    /// # Returns
    ///
    /// - `f64` - The magnitude of the vector.
    pub fn magnitude(&self) -> f64 {
        (self.get_x() * self.get_x() + self.get_y() * self.get_y() + self.get_z() * self.get_z())
            .sqrt()
    }

    /// Returns the squared magnitude of the vector.
    ///
    /// Avoids a square root, making it faster for comparison-only use cases.
    ///
    /// # Returns
    ///
    /// - `f64` - The squared magnitude of the vector.
    pub fn magnitude_squared(&self) -> f64 {
        self.get_x() * self.get_x() + self.get_y() * self.get_y() + self.get_z() * self.get_z()
    }

    /// Returns a normalized (unit length) copy of this vector.
    ///
    /// Returns the zero vector if the magnitude is zero.
    ///
    /// # Returns
    ///
    /// - `Vector3D` - The normalized vector.
    pub fn normalized(&self) -> Vector3D {
        let mag: f64 = self.magnitude();
        if mag < EPSILON {
            return Vector3D::zero();
        }
        Vector3D::new(self.get_x() / mag, self.get_y() / mag, self.get_z() / mag)
    }

    /// Normalizes this vector in place.
    pub fn normalize(&mut self) {
        let mag: f64 = self.magnitude();
        if mag < EPSILON {
            self.set_x(0.0);
            self.set_y(0.0);
            self.set_z(0.0);
            return;
        }
        self.set_x(self.get_x() / mag);
        self.set_y(self.get_y() / mag);
        self.set_z(self.get_z() / mag);
    }

    /// Computes the dot product with another vector.
    ///
    /// # Arguments
    ///
    /// - `Vector3D` - The other vector.
    ///
    /// # Returns
    ///
    /// - `f64` - The dot product.
    pub fn dot(&self, other: Vector3D) -> f64 {
        self.get_x() * other.get_x() + self.get_y() * other.get_y() + self.get_z() * other.get_z()
    }

    /// Computes the 3D cross product with another vector.
    ///
    /// # Arguments
    ///
    /// - `Vector3D` - The other vector.
    ///
    /// # Returns
    ///
    /// - `Vector3D` - The cross product vector.
    pub fn cross(&self, other: Vector3D) -> Vector3D {
        Vector3D::new(
            self.get_y() * other.get_z() - self.get_z() * other.get_y(),
            self.get_z() * other.get_x() - self.get_x() * other.get_z(),
            self.get_x() * other.get_y() - self.get_y() * other.get_x(),
        )
    }

    /// Returns the distance from this point to another.
    ///
    /// # Arguments
    ///
    /// - `Vector3D` - The target point.
    ///
    /// # Returns
    ///
    /// - `f64` - The Euclidean distance.
    pub fn distance_to(&self, other: Vector3D) -> f64 {
        (other - *self).magnitude()
    }

    /// Returns the squared distance from this point to another.
    ///
    /// # Arguments
    ///
    /// - `Vector3D` - The target point.
    ///
    /// # Returns
    ///
    /// - `f64` - The squared Euclidean distance.
    pub fn distance_squared_to(&self, other: Vector3D) -> f64 {
        (other - *self).magnitude_squared()
    }

    /// Returns a unit vector pointing from this point to another.
    ///
    /// # Arguments
    ///
    /// - `Vector3D` - The target point.
    ///
    /// # Returns
    ///
    /// - `Vector3D` - The direction unit vector.
    pub fn direction_to(&self, other: Vector3D) -> Vector3D {
        (other - *self).normalized()
    }

    /// Returns a linearly interpolated vector between this and another.
    ///
    /// # Arguments
    ///
    /// - `Vector3D` - The target vector.
    /// - `f64` - The interpolation factor.
    ///
    /// # Returns
    ///
    /// - `Vector3D` - The interpolated vector.
    pub fn lerp(&self, other: Vector3D, t: f64) -> Vector3D {
        Vector3D::new(
            self.get_x() + (other.get_x() - self.get_x()) * t,
            self.get_y() + (other.get_y() - self.get_y()) * t,
            self.get_z() + (other.get_z() - self.get_z()) * t,
        )
    }

    /// Scales this vector by a scalar factor.
    ///
    /// # Arguments
    ///
    /// - `f64` - The scalar factor.
    pub fn scale(&mut self, scalar: f64) {
        self.set_x(self.get_x() * scalar);
        self.set_y(self.get_y() * scalar);
        self.set_z(self.get_z() * scalar);
    }

    /// Returns a scaled copy of this vector.
    ///
    /// # Arguments
    ///
    /// - `f64` - The scalar factor.
    ///
    /// # Returns
    ///
    /// - `Vector3D` - The scaled vector.
    pub fn scaled(&self, scalar: f64) -> Vector3D {
        Vector3D::new(
            self.get_x() * scalar,
            self.get_y() * scalar,
            self.get_z() * scalar,
        )
    }

    /// Rotates this vector by a quaternion.
    ///
    /// # Arguments
    ///
    /// - `Quaternion` - The rotation quaternion.
    ///
    /// # Returns
    ///
    /// - `Vector3D` - The rotated vector.
    pub fn rotated_by(&self, quaternion: Quaternion) -> Vector3D {
        let pure: Quaternion = Quaternion::new(self.get_x(), self.get_y(), self.get_z(), 0.0);
        let result: Quaternion = quaternion * pure * quaternion.conjugate();
        Vector3D::new(result.get_x(), result.get_y(), result.get_z())
    }
}

/// Implements `Interpolable` for `Vector3D`.
impl Interpolable for Vector3D {
    fn lerp(&self, other: Vector3D, t: f64) -> Vector3D {
        Vector3D::lerp(self, other, t)
    }
}

/// Implements vector addition.
impl Add for Vector3D {
    type Output = Vector3D;
    fn add(self, other: Vector3D) -> Vector3D {
        Vector3D::new(
            self.get_x() + other.get_x(),
            self.get_y() + other.get_y(),
            self.get_z() + other.get_z(),
        )
    }
}

/// Implements vector subtraction.
impl Sub for Vector3D {
    type Output = Vector3D;
    fn sub(self, other: Vector3D) -> Vector3D {
        Vector3D::new(
            self.get_x() - other.get_x(),
            self.get_y() - other.get_y(),
            self.get_z() - other.get_z(),
        )
    }
}

/// Implements scalar multiplication.
impl Mul<f64> for Vector3D {
    type Output = Vector3D;
    fn mul(self, scalar: f64) -> Vector3D {
        Vector3D::new(
            self.get_x() * scalar,
            self.get_y() * scalar,
            self.get_z() * scalar,
        )
    }
}

/// Implements vector negation.
impl Neg for Vector3D {
    type Output = Vector3D;
    fn neg(self) -> Vector3D {
        Vector3D::new(-self.get_x(), -self.get_y(), -self.get_z())
    }
}

/// Implements in-place vector addition.
impl AddAssign for Vector3D {
    fn add_assign(&mut self, other: Vector3D) {
        self.set_x(self.get_x() + other.get_x());
        self.set_y(self.get_y() + other.get_y());
        self.set_z(self.get_z() + other.get_z());
    }
}

/// Implements in-place vector subtraction.
impl SubAssign for Vector3D {
    fn sub_assign(&mut self, other: Vector3D) {
        self.set_x(self.get_x() - other.get_x());
        self.set_y(self.get_y() - other.get_y());
        self.set_z(self.get_z() - other.get_z());
    }
}

/// Implements in-place scalar multiplication.
impl MulAssign<f64> for Vector3D {
    fn mul_assign(&mut self, scalar: f64) {
        self.set_x(self.get_x() * scalar);
        self.set_y(self.get_y() * scalar);
        self.set_z(self.get_z() * scalar);
    }
}

/// Implements quaternion operations for `Quaternion`.
impl Quaternion {
    /// Returns the identity quaternion (0, 0, 0, 1) representing no rotation.
    ///
    /// # Returns
    ///
    /// - `Quaternion` - The identity quaternion.
    pub fn identity() -> Quaternion {
        Quaternion::new(0.0, 0.0, 0.0, 1.0)
    }

    /// Creates a quaternion from a rotation around an axis.
    ///
    /// # Arguments
    ///
    /// - `Vector3D` - The rotation axis (should be normalized).
    /// - `f64` - The rotation angle in radians.
    ///
    /// # Returns
    ///
    /// - `Quaternion` - The rotation quaternion.
    pub fn from_axis_angle(axis: Vector3D, angle: f64) -> Quaternion {
        let half: f64 = angle * 0.5;
        let sin_half: f64 = half.sin();
        let cos_half: f64 = half.cos();
        let normalized_axis: Vector3D = axis.normalized();
        Quaternion::new(
            normalized_axis.get_x() * sin_half,
            normalized_axis.get_y() * sin_half,
            normalized_axis.get_z() * sin_half,
            cos_half,
        )
    }

    /// Creates a quaternion from Euler angles (yaw, pitch, roll) in radians.
    ///
    /// # Arguments
    ///
    /// - `f64` - The yaw (rotation around y axis) in radians.
    /// - `f64` - The pitch (rotation around x axis) in radians.
    /// - `f64` - The roll (rotation around z axis) in radians.
    ///
    /// # Returns
    ///
    /// - `Quaternion` - The rotation quaternion.
    pub fn from_euler(yaw: f64, pitch: f64, roll: f64) -> Quaternion {
        let half_yaw: f64 = yaw * 0.5;
        let half_pitch: f64 = pitch * 0.5;
        let half_roll: f64 = roll * 0.5;
        let cy: f64 = half_yaw.cos();
        let sy: f64 = half_yaw.sin();
        let cp: f64 = half_pitch.cos();
        let sp: f64 = half_pitch.sin();
        let cr: f64 = half_roll.cos();
        let sr: f64 = half_roll.sin();
        Quaternion::new(
            sp * cy * cr + cp * sy * sr,
            cp * sy * cr - sp * cy * sr,
            cp * cy * sr - sp * sy * cr,
            sp * sy * sr + cp * cy * cr,
        )
    }

    /// Returns the magnitude of the quaternion.
    ///
    /// # Returns
    ///
    /// - `f64` - The magnitude.
    pub fn magnitude(&self) -> f64 {
        (self.get_x() * self.get_x()
            + self.get_y() * self.get_y()
            + self.get_z() * self.get_z()
            + self.get_w() * self.get_w())
        .sqrt()
    }

    /// Returns a normalized copy of this quaternion.
    ///
    /// # Returns
    ///
    /// - `Quaternion` - The normalized quaternion.
    pub fn normalized(&self) -> Quaternion {
        let mag: f64 = self.magnitude();
        if mag < EPSILON {
            return Quaternion::identity();
        }
        let inv: f64 = 1.0 / mag;
        Quaternion::new(
            self.get_x() * inv,
            self.get_y() * inv,
            self.get_z() * inv,
            self.get_w() * inv,
        )
    }

    /// Returns the conjugate of this quaternion.
    ///
    /// # Returns
    ///
    /// - `Quaternion` - The conjugate quaternion.
    pub fn conjugate(&self) -> Quaternion {
        Quaternion::new(-self.get_x(), -self.get_y(), -self.get_z(), self.get_w())
    }

    /// Computes the dot product with another quaternion.
    ///
    /// # Arguments
    ///
    /// - `Quaternion` - The other quaternion.
    ///
    /// # Returns
    ///
    /// - `f64` - The dot product.
    pub fn dot(&self, other: Quaternion) -> f64 {
        self.get_x() * other.get_x()
            + self.get_y() * other.get_y()
            + self.get_z() * other.get_z()
            + self.get_w() * other.get_w()
    }

    /// Performs spherical linear interpolation between this and another quaternion.
    ///
    /// # Arguments
    ///
    /// - `Quaternion` - The target quaternion.
    /// - `f64` - The interpolation factor in the range 0.0 to 1.0.
    ///
    /// # Returns
    ///
    /// - `Quaternion` - The interpolated quaternion.
    pub fn slerp(&self, other: Quaternion, t: f64) -> Quaternion {
        let mut cos_theta: f64 = self.dot(other);
        let target: Quaternion = if cos_theta < 0.0 {
            cos_theta = -cos_theta;
            Quaternion::new(
                -other.get_x(),
                -other.get_y(),
                -other.get_z(),
                -other.get_w(),
            )
        } else {
            other
        };
        if cos_theta > 1.0 - EPSILON {
            return Quaternion::new(
                self.get_x() + (target.get_x() - self.get_x()) * t,
                self.get_y() + (target.get_y() - self.get_y()) * t,
                self.get_z() + (target.get_z() - self.get_z()) * t,
                self.get_w() + (target.get_w() - self.get_w()) * t,
            )
            .normalized();
        }
        let theta: f64 = cos_theta.acos();
        let sin_theta: f64 = theta.sin();
        let factor_a: f64 = ((1.0 - t) * theta).sin() / sin_theta;
        let factor_b: f64 = (t * theta).sin() / sin_theta;
        Quaternion::new(
            self.get_x() * factor_a + target.get_x() * factor_b,
            self.get_y() * factor_a + target.get_y() * factor_b,
            self.get_z() * factor_a + target.get_z() * factor_b,
            self.get_w() * factor_a + target.get_w() * factor_b,
        )
    }
}

/// Implements quaternion multiplication.
impl Mul for Quaternion {
    type Output = Quaternion;
    fn mul(self, other: Quaternion) -> Quaternion {
        Quaternion::new(
            self.get_w() * other.get_x()
                + self.get_x() * other.get_w()
                + self.get_y() * other.get_z()
                - self.get_z() * other.get_y(),
            self.get_w() * other.get_y() - self.get_x() * other.get_z()
                + self.get_y() * other.get_w()
                + self.get_z() * other.get_x(),
            self.get_w() * other.get_z() + self.get_x() * other.get_y()
                - self.get_y() * other.get_x()
                + self.get_z() * other.get_w(),
            self.get_w() * other.get_w()
                - self.get_x() * other.get_x()
                - self.get_y() * other.get_y()
                - self.get_z() * other.get_z(),
        )
    }
}

/// Implements matrix operations for `Matrix4x4`.
impl Matrix4x4 {
    /// Returns the identity matrix.
    ///
    /// # Returns
    ///
    /// - `Matrix4x4` - The identity matrix.
    pub fn identity() -> Matrix4x4 {
        Matrix4x4::new([
            1.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 0.0, 1.0,
        ])
    }

    /// Creates a translation matrix.
    ///
    /// # Arguments
    ///
    /// - `Vector3D` - The translation vector.
    ///
    /// # Returns
    ///
    /// - `Matrix4x4` - The translation matrix.
    pub fn translation(translation: Vector3D) -> Matrix4x4 {
        let mut elements: [f64; 16] = Self::identity().get_elements();
        elements[12] = translation.get_x();
        elements[13] = translation.get_y();
        elements[14] = translation.get_z();
        Matrix4x4::new(elements)
    }

    /// Creates a scaling matrix.
    ///
    /// # Arguments
    ///
    /// - `Vector3D` - The scale factors.
    ///
    /// # Returns
    ///
    /// - `Matrix4x4` - The scaling matrix.
    pub fn scaling(scale: Vector3D) -> Matrix4x4 {
        Matrix4x4::new([
            scale.get_x(),
            0.0,
            0.0,
            0.0,
            0.0,
            scale.get_y(),
            0.0,
            0.0,
            0.0,
            0.0,
            scale.get_z(),
            0.0,
            0.0,
            0.0,
            0.0,
            1.0,
        ])
    }

    /// Creates a rotation matrix from a quaternion.
    ///
    /// # Arguments
    ///
    /// - `Quaternion` - The rotation quaternion.
    ///
    /// # Returns
    ///
    /// - `Matrix4x4` - The rotation matrix.
    pub fn rotation(quaternion: Quaternion) -> Matrix4x4 {
        let xx: f64 = quaternion.get_x() * quaternion.get_x();
        let yy: f64 = quaternion.get_y() * quaternion.get_y();
        let zz: f64 = quaternion.get_z() * quaternion.get_z();
        let xy: f64 = quaternion.get_x() * quaternion.get_y();
        let xz: f64 = quaternion.get_x() * quaternion.get_z();
        let yz: f64 = quaternion.get_y() * quaternion.get_z();
        let wx: f64 = quaternion.get_w() * quaternion.get_x();
        let wy: f64 = quaternion.get_w() * quaternion.get_y();
        let wz: f64 = quaternion.get_w() * quaternion.get_z();
        Matrix4x4::new([
            1.0 - 2.0 * (yy + zz),
            2.0 * (xy + wz),
            2.0 * (xz - wy),
            0.0,
            2.0 * (xy - wz),
            1.0 - 2.0 * (xx + zz),
            2.0 * (yz + wx),
            0.0,
            2.0 * (xz + wy),
            2.0 * (yz - wx),
            1.0 - 2.0 * (xx + yy),
            0.0,
            0.0,
            0.0,
            0.0,
            1.0,
        ])
    }

    /// Creates a perspective projection matrix.
    ///
    /// # Arguments
    ///
    /// - `f64` - The vertical field of view in radians.
    /// - `f64` - The aspect ratio (width / height).
    /// - `f64` - The near clipping plane distance.
    /// - `f64` - The far clipping plane distance.
    ///
    /// # Returns
    ///
    /// - `Matrix4x4` - The perspective projection matrix.
    pub fn perspective(fov: f64, aspect: f64, near: f64, far: f64) -> Matrix4x4 {
        let f: f64 = 1.0 / (fov * 0.5).tan();
        let range: f64 = far - near;
        Matrix4x4::new([
            f / aspect,
            0.0,
            0.0,
            0.0,
            0.0,
            f,
            0.0,
            0.0,
            0.0,
            0.0,
            -(far + near) / range,
            -1.0,
            0.0,
            0.0,
            -(2.0 * far * near) / range,
            0.0,
        ])
    }

    /// Creates an orthographic projection matrix.
    ///
    /// # Arguments
    ///
    /// - `f64` - The left boundary.
    /// - `f64` - The right boundary.
    /// - `f64` - The bottom boundary.
    /// - `f64` - The top boundary.
    /// - `f64` - The near clipping plane distance.
    /// - `f64` - The far clipping plane distance.
    ///
    /// # Returns
    ///
    /// - `Matrix4x4` - The orthographic projection matrix.
    pub fn orthographic(
        left: f64,
        right: f64,
        bottom: f64,
        top: f64,
        near: f64,
        far: f64,
    ) -> Matrix4x4 {
        let rml: f64 = right - left;
        let tmb: f64 = top - bottom;
        let fmn: f64 = far - near;
        Matrix4x4::new([
            2.0 / rml,
            0.0,
            0.0,
            0.0,
            0.0,
            2.0 / tmb,
            0.0,
            0.0,
            0.0,
            0.0,
            -2.0 / fmn,
            0.0,
            -(right + left) / rml,
            -(top + bottom) / tmb,
            -(far + near) / fmn,
            1.0,
        ])
    }

    /// Creates a view matrix using the "look at" convention.
    ///
    /// # Arguments
    ///
    /// - `Vector3D` - The eye position.
    /// - `Vector3D` - The target position to look at.
    /// - `Vector3D` - The up direction.
    ///
    /// # Returns
    ///
    /// - `Matrix4x4` - The view matrix.
    pub fn look_at(eye: Vector3D, target: Vector3D, up: Vector3D) -> Matrix4x4 {
        let forward: Vector3D = (target - eye).normalized();
        let right: Vector3D = forward.cross(up).normalized();
        let up_orthogonal: Vector3D = right.cross(forward);
        Matrix4x4::new([
            right.get_x(),
            up_orthogonal.get_x(),
            -forward.get_x(),
            0.0,
            right.get_y(),
            up_orthogonal.get_y(),
            -forward.get_y(),
            0.0,
            right.get_z(),
            up_orthogonal.get_z(),
            -forward.get_z(),
            0.0,
            -right.dot(eye),
            -up_orthogonal.dot(eye),
            forward.dot(eye),
            1.0,
        ])
    }

    /// Multiplies this matrix by another.
    ///
    /// # Arguments
    ///
    /// - `Matrix4x4` - The other matrix.
    ///
    /// # Returns
    ///
    /// - `Matrix4x4` - The product matrix.
    pub fn multiply(&self, other: Matrix4x4) -> Matrix4x4 {
        let self_elements: [f64; 16] = self.get_elements();
        let other_elements: [f64; 16] = other.get_elements();
        let mut result: [f64; 16] = [0.0; 16];
        for col in 0..4usize {
            for row in 0..4usize {
                let mut sum: f64 = 0.0;
                for k in 0..4usize {
                    sum += self_elements[k * 4 + row] * other_elements[col * 4 + k];
                }
                result[col * 4 + row] = sum;
            }
        }
        Matrix4x4::new(result)
    }

    /// Transforms a 3D point by this matrix, applying the perspective divide.
    ///
    /// # Arguments
    ///
    /// - `Vector3D` - The point to transform.
    ///
    /// # Returns
    ///
    /// - `Vector3D` - The transformed point.
    pub fn transform_point(&self, point: Vector3D) -> Vector3D {
        let elements: [f64; 16] = self.get_elements();
        let x: f64 = elements[0] * point.get_x()
            + elements[4] * point.get_y()
            + elements[8] * point.get_z()
            + elements[12];
        let y: f64 = elements[1] * point.get_x()
            + elements[5] * point.get_y()
            + elements[9] * point.get_z()
            + elements[13];
        let z: f64 = elements[2] * point.get_x()
            + elements[6] * point.get_y()
            + elements[10] * point.get_z()
            + elements[14];
        let w: f64 = elements[3] * point.get_x()
            + elements[7] * point.get_y()
            + elements[11] * point.get_z()
            + elements[15];
        if w.abs() < EPSILON {
            return Vector3D::new(x, y, z);
        }
        Vector3D::new(x / w, y / w, z / w)
    }
}

/// Implements `Default` for `Quaternion` as the identity quaternion.
impl Default for Quaternion {
    fn default() -> Quaternion {
        Quaternion::identity()
    }
}

/// Implements `Default` for `Matrix4x4` as the identity matrix.
impl Default for Matrix4x4 {
    fn default() -> Matrix4x4 {
        Matrix4x4::identity()
    }
}

/// Implements methods for `Transform3D`.
impl Transform3D {
    /// Creates a new transform at the origin with no rotation and unit scale.
    ///
    /// # Returns
    ///
    /// - `Transform3D` - The identity transform.
    pub fn identity() -> Transform3D {
        Transform3D::new(
            Vector3D::zero(),
            Quaternion::identity(),
            Vector3D::new(1.0, 1.0, 1.0),
        )
    }

    /// Translates the position by the given offset.
    ///
    /// # Arguments
    ///
    /// - `Vector3D` - The translation offset.
    pub fn translate(&mut self, offset: Vector3D) {
        self.set_position(self.get_position() + offset);
    }

    /// Rotates by the given quaternion (post-multiplies).
    ///
    /// # Arguments
    ///
    /// - `Quaternion` - The rotation to apply.
    pub fn rotate(&mut self, rotation: Quaternion) {
        self.set_rotation(rotation * self.get_rotation());
    }

    /// Scales by the given factors.
    ///
    /// # Arguments
    ///
    /// - `Vector3D` - The scale factors.
    pub fn scale_by(&mut self, factors: Vector3D) {
        let mut scale: Vector3D = self.get_scale();
        scale.set_x(scale.get_x() * factors.get_x());
        scale.set_y(scale.get_y() * factors.get_y());
        scale.set_z(scale.get_z() * factors.get_z());
        self.set_scale(scale);
    }

    /// Applies this transform to a local-space point, returning world-space coordinates.
    ///
    /// # Arguments
    ///
    /// - `Vector3D` - The local-space point.
    ///
    /// # Returns
    ///
    /// - `Vector3D` - The transformed world-space point.
    pub fn apply_to_point(&self, point: Vector3D) -> Vector3D {
        let scale: Vector3D = self.get_scale();
        let scaled: Vector3D = Vector3D::new(
            point.get_x() * scale.get_x(),
            point.get_y() * scale.get_y(),
            point.get_z() * scale.get_z(),
        );
        scaled.rotated_by(self.get_rotation()) + self.get_position()
    }

    /// Converts this transform to a `Matrix4x4`.
    ///
    /// # Returns
    ///
    /// - `Matrix4x4` - The composed transformation matrix.
    pub fn to_matrix(&self) -> Matrix4x4 {
        let translation: Matrix4x4 = Matrix4x4::translation(self.get_position());
        let rotation: Matrix4x4 = Matrix4x4::rotation(self.get_rotation());
        let scaling: Matrix4x4 = Matrix4x4::scaling(self.get_scale());
        translation.multiply(rotation).multiply(scaling)
    }
}

/// Implements `Default` for `Transform3D` as the identity transform.
impl Default for Transform3D {
    fn default() -> Transform3D {
        Transform3D::identity()
    }
}

/// Implements methods for `AABB3D`.
impl AABB3D {
    /// Creates an AABB from a center point and dimensions.
    ///
    /// # Arguments
    ///
    /// - `Vector3D` - The center point.
    /// - `f64` - The width.
    /// - `f64` - The height.
    /// - `f64` - The depth.
    ///
    /// # Returns
    ///
    /// - `AABB3D` - The new bounding box.
    pub fn from_center(center: Vector3D, width: f64, height: f64, depth: f64) -> AABB3D {
        AABB3D::new(
            Vector3D::new(
                center.get_x() - width * 0.5,
                center.get_y() - height * 0.5,
                center.get_z() - depth * 0.5,
            ),
            Vector3D::new(
                center.get_x() + width * 0.5,
                center.get_y() + height * 0.5,
                center.get_z() + depth * 0.5,
            ),
        )
    }

    /// Returns the center point of the bounding box.
    ///
    /// # Returns
    ///
    /// - `Vector3D` - The center point.
    pub fn center(&self) -> Vector3D {
        Vector3D::new(
            (self.get_min().get_x() + self.get_max().get_x()) * 0.5,
            (self.get_min().get_y() + self.get_max().get_y()) * 0.5,
            (self.get_min().get_z() + self.get_max().get_z()) * 0.5,
        )
    }

    /// Returns the dimensions of the bounding box as a vector.
    ///
    /// # Returns
    ///
    /// - `Vector3D` - The size vector (width, height, depth).
    pub fn size(&self) -> Vector3D {
        Vector3D::new(
            self.get_max().get_x() - self.get_min().get_x(),
            self.get_max().get_y() - self.get_min().get_y(),
            self.get_max().get_z() - self.get_min().get_z(),
        )
    }

    /// Tests whether a point is inside this bounding box.
    ///
    /// # Arguments
    ///
    /// - `Vector3D` - The point to test.
    ///
    /// # Returns
    ///
    /// - `bool` - True if the point is inside.
    pub fn contains(&self, point: Vector3D) -> bool {
        point.get_x() >= self.get_min().get_x()
            && point.get_x() <= self.get_max().get_x()
            && point.get_y() >= self.get_min().get_y()
            && point.get_y() <= self.get_max().get_y()
            && point.get_z() >= self.get_min().get_z()
            && point.get_z() <= self.get_max().get_z()
    }

    /// Tests whether this bounding box intersects another.
    ///
    /// # Arguments
    ///
    /// - `AABB3D` - The other bounding box.
    ///
    /// # Returns
    ///
    /// - `bool` - True if they intersect.
    pub fn intersects(&self, other: AABB3D) -> bool {
        self.get_min().get_x() <= other.get_max().get_x()
            && self.get_max().get_x() >= other.get_min().get_x()
            && self.get_min().get_y() <= other.get_max().get_y()
            && self.get_max().get_y() >= other.get_min().get_y()
            && self.get_min().get_z() <= other.get_max().get_z()
            && self.get_max().get_z() >= other.get_min().get_z()
    }
}

/// Implements methods for `Sphere`.
impl Sphere {
    /// Tests whether a point is inside this sphere.
    ///
    /// # Arguments
    ///
    /// - `Vector3D` - The point to test.
    ///
    /// # Returns
    ///
    /// - `bool` - True if the point is inside.
    pub fn contains(&self, point: Vector3D) -> bool {
        self.get_center().distance_squared_to(point) <= self.get_radius() * self.get_radius()
    }

    /// Tests whether this sphere intersects another.
    ///
    /// # Arguments
    ///
    /// - `Sphere` - The other sphere.
    ///
    /// # Returns
    ///
    /// - `bool` - True if they intersect.
    pub fn intersects(&self, other: Sphere) -> bool {
        let distance_sq: f64 = self.get_center().distance_squared_to(other.get_center());
        let radius_sum: f64 = self.get_radius() + other.get_radius();
        distance_sq <= radius_sum * radius_sum
    }

    /// Returns the volume of the sphere.
    ///
    /// # Returns
    ///
    /// - `f64` - The volume.
    pub fn volume(&self) -> f64 {
        (4.0 / 3.0) * PI * self.get_radius() * self.get_radius() * self.get_radius()
    }

    /// Returns the surface area of the sphere.
    ///
    /// # Returns
    ///
    /// - `f64` - The surface area.
    pub fn surface_area(&self) -> f64 {
        4.0 * PI * self.get_radius() * self.get_radius()
    }
}

/// Implements methods for `Plane`.
impl Plane {
    /// Creates a plane from a normal and a point on the plane.
    ///
    /// # Arguments
    ///
    /// - `Vector3D` - The normal vector.
    /// - `Vector3D` - A point on the plane.
    ///
    /// # Returns
    ///
    /// - `Plane` - The new plane.
    pub fn from_normal_and_point(normal: Vector3D, point: Vector3D) -> Plane {
        let normalized_normal: Vector3D = normal.normalized();
        Plane::new(normalized_normal, -normalized_normal.dot(point))
    }

    /// Returns the signed distance from a point to this plane.
    ///
    /// # Arguments
    ///
    /// - `Vector3D` - The point to test.
    ///
    /// # Returns
    ///
    /// - `f64` - The signed distance (positive on the normal side).
    pub fn distance_to_point(&self, point: Vector3D) -> f64 {
        self.get_normal().dot(point) + self.get_distance()
    }

    /// Normalizes the plane normal and adjusts the distance accordingly.
    pub fn normalize(&mut self) {
        let mut normal: Vector3D = self.get_normal();
        let mag: f64 = normal.magnitude();
        if mag < EPSILON {
            return;
        }
        normal.set_x(normal.get_x() / mag);
        normal.set_y(normal.get_y() / mag);
        normal.set_z(normal.get_z() / mag);
        self.set_normal(normal);
        self.set_distance(self.get_distance() / mag);
    }
}

/// Implements methods for `Ray3D`.
impl Ray3D {
    /// Returns the point on the ray at the given parameter value.
    ///
    /// # Arguments
    ///
    /// - `f64` - The parameter value (distance along the ray).
    ///
    /// # Returns
    ///
    /// - `Vector3D` - The point at the given distance.
    pub fn point_at(&self, t: f64) -> Vector3D {
        self.get_origin() + self.get_direction().scaled(t)
    }

    /// Tests for intersection with a sphere, returning the nearest distance if hit.
    ///
    /// # Arguments
    ///
    /// - `Sphere` - The sphere to test.
    ///
    /// # Returns
    ///
    /// - `Option<f64>` - The distance to the intersection, or `None`.
    pub fn intersect_sphere(&self, sphere: Sphere) -> Option<f64> {
        let oc: Vector3D = self.get_origin() - sphere.get_center();
        let direction: Vector3D = self.get_direction();
        let a: f64 = direction.dot(direction);
        let b: f64 = 2.0 * oc.dot(direction);
        let c: f64 = oc.dot(oc) - sphere.get_radius() * sphere.get_radius();
        let discriminant: f64 = b * b - 4.0 * a * c;
        if discriminant < 0.0 {
            return None;
        }
        let sqrt_d: f64 = discriminant.sqrt();
        let t1: f64 = (-b - sqrt_d) / (2.0 * a);
        if t1 >= 0.0 {
            return Some(t1);
        }
        let t2: f64 = (-b + sqrt_d) / (2.0 * a);
        if t2 >= 0.0 {
            return Some(t2);
        }
        None
    }

    /// Tests for intersection with a plane, returning the distance if hit.
    ///
    /// # Arguments
    ///
    /// - `Plane` - The plane to test.
    ///
    /// # Returns
    ///
    /// - `Option<f64>` - The distance to the intersection, or `None`.
    pub fn intersect_plane(&self, plane: Plane) -> Option<f64> {
        let direction: Vector3D = self.get_direction();
        let normal: Vector3D = plane.get_normal();
        let denom: f64 = direction.dot(normal);
        if denom.abs() < EPSILON {
            return None;
        }
        let t: f64 = -(normal.dot(self.get_origin()) + plane.get_distance()) / denom;
        if t >= 0.0 { Some(t) } else { None }
    }

    /// Tests for intersection with an AABB, returning the nearest distance if hit.
    ///
    /// # Arguments
    ///
    /// - `AABB3D` - The bounding box to test.
    ///
    /// # Returns
    ///
    /// - `Option<f64>` - The distance to the intersection, or `None`.
    pub fn intersect_aabb(&self, aabb: AABB3D) -> Option<f64> {
        let mut t_min: f64 = f64::MIN;
        let mut t_max: f64 = f64::MAX;
        let direction: Vector3D = self.get_direction();
        let origin: Vector3D = self.get_origin();
        let aabb_min: Vector3D = aabb.get_min();
        let aabb_max: Vector3D = aabb.get_max();
        for axis in 0..3usize {
            let (dir_component, origin_component, min_component, max_component) = match axis {
                0 => (
                    direction.get_x(),
                    origin.get_x(),
                    aabb_min.get_x(),
                    aabb_max.get_x(),
                ),
                1 => (
                    direction.get_y(),
                    origin.get_y(),
                    aabb_min.get_y(),
                    aabb_max.get_y(),
                ),
                _ => (
                    direction.get_z(),
                    origin.get_z(),
                    aabb_min.get_z(),
                    aabb_max.get_z(),
                ),
            };
            if dir_component.abs() < EPSILON {
                if origin_component < min_component || origin_component > max_component {
                    return None;
                }
            } else {
                let inv_dir: f64 = 1.0 / dir_component;
                let t1: f64 = (min_component - origin_component) * inv_dir;
                let t2: f64 = (max_component - origin_component) * inv_dir;
                let t_near: f64 = t1.min(t2);
                let t_far: f64 = t1.max(t2);
                t_min = t_min.max(t_near);
                t_max = t_max.min(t_far);
                if t_min > t_max {
                    return None;
                }
            }
        }
        if t_min >= 0.0 {
            Some(t_min)
        } else if t_max >= 0.0 {
            Some(t_max)
        } else {
            None
        }
    }
}