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
|
#include "sys_math3d.h"
#include "printf.h"
#include "terminal.h"
#include "translation.h"
#include "ultra64.h"
#include "z_lib.h"
#include "z_math.h"
#pragma increment_block_number "gc-eu:76 gc-eu-mq:76 gc-jp:76 gc-jp-ce:76 gc-jp-mq:76 gc-us:76 gc-us-mq:76 ique-cn:66" \
"ntsc-1.0:64 ntsc-1.1:64 ntsc-1.2:64 pal-1.0:64 pal-1.1:64"
s32 Math3D_LineVsLineClosestTwoPoints(Vec3f* lineAPointA, Vec3f* lineAPointB, Vec3f* lineBPointA, Vec3f* lineBPointB,
Vec3f* lineAClosestToB, Vec3f* lineBClosestToA);
s32 Math3D_TriLineIntersect(Vec3f* v0, Vec3f* v1, Vec3f* v2, f32 nx, f32 ny, f32 nz, f32 originDist, Vec3f* linePointA,
Vec3f* linePointB, Vec3f* intersect, s32 fromFront);
s32 Math3D_PlaneVsPlaneNewLine(f32 planeAA, f32 planeAB, f32 planeAC, f32 planeADist, f32 planeBA, f32 planeBB,
f32 planeBC, f32 planeBDist, InfiniteLine* intersect);
s32 Math3D_CirSquareVsTriSquare(f32 x0, f32 y0, f32 x1, f32 y1, f32 x2, f32 y2, f32 centerX, f32 centerY, f32 radius);
s32 Math3D_SphCubeVsTriCube(Vec3f* v0, Vec3f* v1, Vec3f* v2, Vec3f* center, f32 radius);
/**
* Creates an infinite line along the intersection of the plane defined from `planeAA`x + `planeAB`y + `planeAB`z +
* `planeADist` = 0 and `planeBA`x + `planeBB`y + `planeBC`z + `planeBDist` = 0, and finds the closest point on that
* intersection to the line segment `linePointA and linePointB`, outputs the intersection to `closestPoint`
*/
s32 Math3D_PlaneVsLineSegClosestPoint(f32 planeAA, f32 planeAB, f32 planeAC, f32 planeADist, f32 planeBA, f32 planeBB,
f32 planeBC, f32 planeBDist, Vec3f* linePointA, Vec3f* linePointB,
Vec3f* closestPoint) {
static InfiniteLine planeIntersectLine;
static Linef planeIntersectSeg;
Vec3f sp34; // unused
if (!Math3D_PlaneVsPlaneNewLine(planeAA, planeAB, planeAC, planeADist, planeBA, planeBB, planeBC, planeBDist,
&planeIntersectLine)) {
// The planes are parallel
return false;
}
// create a line segment on the plane.
Math_Vec3f_Copy(&planeIntersectSeg.a, &planeIntersectLine.point);
planeIntersectSeg.b.x = (planeIntersectLine.dir.x * 100.0f) + planeIntersectLine.point.x;
planeIntersectSeg.b.y = (planeIntersectLine.dir.y * 100.0f) + planeIntersectLine.point.y;
planeIntersectSeg.b.z = (planeIntersectLine.dir.z * 100.0f) + planeIntersectLine.point.z;
// closestPoint is a point on planeIntersect, sp34 is a point on linePointA, linePointB
if (!Math3D_LineVsLineClosestTwoPoints(&planeIntersectSeg.a, &planeIntersectSeg.b, linePointA, linePointB,
closestPoint, &sp34)) {
return false;
}
return true;
}
/**
* Finds the two points on lines A and B where the lines are closest together.
*/
s32 Math3D_LineVsLineClosestTwoPoints(Vec3f* lineAPointA, Vec3f* lineAPointB, Vec3f* lineBPointA, Vec3f* lineBPointB,
Vec3f* lineAClosestToB, Vec3f* lineBClosestToA) {
f32 sqMag;
f32 scaleB;
f32 lineAx;
f32 lineAy;
f32 lineAz;
f32 lineBx;
f32 lineBy;
f32 lineBz;
f32 compAAlongB;
f32 compBAAlongB;
Vec3f lineAPerpB;
Vec3f lineBAPerpB;
f32 tA;
f32 tB;
lineAx = lineAPointB->x - lineAPointA->x;
lineAy = lineAPointB->y - lineAPointA->y;
lineAz = lineAPointB->z - lineAPointA->z;
lineBx = lineBPointB->x - lineBPointA->x;
lineBy = lineBPointB->y - lineBPointA->y;
lineBz = lineBPointB->z - lineBPointA->z;
sqMag = SQ(lineBx) + SQ(lineBy) + SQ(lineBz);
if (IS_ZERO(sqMag)) {
return false;
}
scaleB = 1.0f / sqMag;
compAAlongB = ((lineAx * lineBx) + (lineAy * lineBy) + (lineAz * lineBz)) * scaleB;
compBAAlongB = ((lineBx * (lineAPointA->x - lineBPointA->x)) + (lineBy * (lineAPointA->y - lineBPointA->y)) +
(lineBz * (lineAPointA->z - lineBPointA->z))) *
scaleB;
lineAPerpB.x = lineAx - (lineBx * compAAlongB);
lineAPerpB.y = lineAy - (lineBy * compAAlongB);
lineAPerpB.z = lineAz - (lineBz * compAAlongB);
sqMag = SQXYZ(lineAPerpB);
if (IS_ZERO(sqMag)) {
return false;
}
lineBAPerpB.x = (lineAPointA->x - lineBPointA->x) - (lineBx * compBAAlongB);
lineBAPerpB.y = (lineAPointA->y - lineBPointA->y) - (lineBy * compBAAlongB);
lineBAPerpB.z = (lineAPointA->z - lineBPointA->z) - (lineBz * compBAAlongB);
tA = -DOTXYZ(lineAPerpB, lineBAPerpB) / sqMag;
lineAClosestToB->x = (lineAx * tA) + lineAPointA->x;
lineAClosestToB->y = (lineAy * tA) + lineAPointA->y;
lineAClosestToB->z = (lineAz * tA) + lineAPointA->z;
tB = (compAAlongB * tA) + compBAAlongB;
lineBClosestToA->x = (lineBx * tB) + lineBPointA->x;
lineBClosestToA->y = (lineBy * tB) + lineBPointA->y;
lineBClosestToA->z = (lineBz * tB) + lineBPointA->z;
return true;
}
/**
* Determines the closest point on the line `line` to `pos`, by forming a line perpendicular from
* `point` to `line` closest point is placed in `closestPoint`
*/
void Math3D_LineClosestToPoint(InfiniteLine* line, Vec3f* pos, Vec3f* closestPoint) {
f32 dirVectorLengthSq;
f32 t;
dirVectorLengthSq = Math3D_Vec3fMagnitudeSq(&line->dir);
if (IS_ZERO(dirVectorLengthSq)) {
PRINTF_COLOR_WARNING();
PRINTF(T("Math3D_lineVsPosSuisenCross():直線の長さがありません\n",
"Math3D_lineVsPosSuisenCross(): No straight line length\n"));
PRINTF(T("cross = pos を返します。\n", "Returns cross = pos.\n"));
PRINTF_RST();
Math_Vec3f_Copy(closestPoint, pos);
//! @bug Missing early return
}
t = (((pos->x - line->point.x) * line->dir.x) + ((pos->y - line->point.y) * line->dir.y) +
((pos->z - line->point.z) * line->dir.z)) /
dirVectorLengthSq;
closestPoint->x = (line->dir.x * t) + line->point.x;
closestPoint->y = (line->dir.y * t) + line->point.y;
closestPoint->z = (line->dir.z * t) + line->point.z;
}
void Math3D_FindPointOnPlaneIntersect(f32 planeAAxis1Norm, f32 planeAAxis2Norm, f32 planeBAxis1Norm,
f32 planeBAxis2Norm, f32 axis3Direction, f32 planeADist, f32 planeBDist,
f32* axis1Point, f32* axis2Point) {
*axis1Point = ((planeAAxis2Norm * planeBDist) - (planeBAxis2Norm * planeADist)) / axis3Direction;
*axis2Point = ((planeBAxis1Norm * planeADist) - (planeAAxis1Norm * planeBDist)) / axis3Direction;
}
/**
* Creates a line between the intersections of two planes defined from `planeAA`x + `planeAB`y + `planeAC`z +
* `planeADist` = 0 and `planeBA`x + `planeBB`y + `planeBC`z + `planeBDist` = 0, and outputs the line to `intersect`.
* Returns false if the planes are parallel.
*/
s32 Math3D_PlaneVsPlaneNewLine(f32 planeAA, f32 planeAB, f32 planeAC, f32 planeADist, f32 planeBA, f32 planeBB,
f32 planeBC, f32 planeBDist, InfiniteLine* intersect) {
char pad[4];
Vec3f planeANormal;
Vec3f planeBNormal;
f32 dirX;
f32 dirY;
f32 dirZ;
VEC_SET(planeANormal, planeAA, planeAB, planeAC);
VEC_SET(planeBNormal, planeBA, planeBB, planeBC);
Math3D_Vec3f_Cross(&planeANormal, &planeBNormal, &intersect->dir);
if (IS_ZERO(intersect->dir.x) && IS_ZERO(intersect->dir.y) && IS_ZERO(intersect->dir.z)) {
// planes are parallel
return false;
}
dirX = fabsf(intersect->dir.x);
dirY = fabsf(intersect->dir.y);
dirZ = fabsf(intersect->dir.z);
if ((dirX >= dirY) && (dirX >= dirZ)) {
Math3D_FindPointOnPlaneIntersect(planeAB, planeAC, planeBB, planeBC, intersect->dir.x, planeADist, planeBDist,
&intersect->point.y, &intersect->point.z);
intersect->point.x = 0.0f;
} else if ((dirY >= dirX) && (dirY >= dirZ)) {
Math3D_FindPointOnPlaneIntersect(planeAC, planeAA, planeBC, planeBA, intersect->dir.y, planeADist, planeBDist,
&intersect->point.z, &intersect->point.x);
intersect->point.y = 0.0f;
} else {
Math3D_FindPointOnPlaneIntersect(planeAA, planeAB, planeBA, planeBB, intersect->dir.z, planeADist, planeBDist,
&intersect->point.x, &intersect->point.y);
intersect->point.z = 0.0f;
}
return true;
}
/**
* Gets the closest point on the line formed from the intersection of of the planes defined from
* `planeAA`x + `planeAB`y + `planeAC`z + `planeADist` = 0 and
* `planeBA`x + `planeBB`y + `planeBC`z + `planeBDist` = 0
* the point on the intersection line closest to `point` is placed in `closestPoint`
* returns false if the planes are parallel.
*/
s32 Math3D_PlaneVsPlaneVsLineClosestPoint(f32 planeAA, f32 planeAB, f32 planeAC, f32 planeADist, f32 planeBA,
f32 planeBB, f32 planeBC, f32 planeBDist, Vec3f* point, Vec3f* closestPoint) {
static InfiniteLine planeIntersect;
if (!Math3D_PlaneVsPlaneNewLine(planeAA, planeAB, planeAC, planeADist, planeBA, planeBB, planeBC, planeBDist,
&planeIntersect)) {
return false;
}
Math3D_LineClosestToPoint(&planeIntersect, point, closestPoint);
return true;
}
/**
* Finds a point on the line from starting point `v0`, and directional vector `dir`
* which is `dist` length from the starting point. Result is placed in `ret`
*/
void Math3D_PointOnInfiniteLine(Vec3f* v0, Vec3f* dir, f32 dist, Vec3f* ret) {
ret->x = (dir->x * dist) + v0->x;
ret->y = (dir->y * dist) + v0->y;
ret->z = (dir->z * dist) + v0->z;
}
/**
* Splits the line segment from end points `v0` and `v1`, and splits that segment
* by `ratio` of `v0`:`v1`, places the resulting point on the line in `ret`
*/
void Math3D_LineSplitRatio(Vec3f* v0, Vec3f* v1, f32 ratio, Vec3f* ret) {
Vec3f diff;
Math_Vec3f_Diff(v1, v0, &diff);
Math3D_PointOnInfiniteLine(v0, &diff, ratio, ret);
}
/**
* Calculates the cosine between vectors `a` and `b`
*/
f32 Math3D_Cos(Vec3f* a, Vec3f* b) {
f32 ret;
Math3D_CosOut(a, b, &ret);
return ret;
}
/**
* Calculates the cosine between vectors `a` and `b` and places the result in `ret`
* returns true if the cosine cannot be calculated because the product of the magnitudes is zero
*/
s32 Math3D_CosOut(Vec3f* a, Vec3f* b, f32* dst) {
f32 magProduct;
magProduct = Math3D_Vec3fMagnitude(a) * Math3D_Vec3fMagnitude(b);
if (IS_ZERO(magProduct)) {
*dst = 0.0f;
return true;
}
*dst = ((a->x * b->x) + (a->y * b->y) + (a->z * b->z)) / magProduct;
return false;
}
/**
* Reflects vector `vec` across the normal vector `normal`, reflection vector is placed in
* `reflVec`
*/
void Math3D_Vec3fReflect(Vec3f* vec, Vec3f* normal, Vec3f* reflVec) {
f32 normScaleY;
Vec3f negVec;
f32 normScaleZ;
f32 normScaleX;
f32 vecDotNorm;
negVec.x = vec->x * -1.0f;
negVec.y = vec->y * -1.0f;
negVec.z = vec->z * -1.0f;
vecDotNorm = Math3D_Cos(&negVec, normal);
normScaleX = normal->x * vecDotNorm;
normScaleY = normal->y * vecDotNorm;
normScaleZ = normal->z * vecDotNorm;
reflVec->x = ((normScaleX + vec->x) + (normScaleX + vec->x)) + negVec.x;
reflVec->y = ((normScaleY + vec->y) + (normScaleY + vec->y)) + negVec.y;
reflVec->z = ((normScaleZ + vec->z) + (normScaleZ + vec->z)) + negVec.z;
}
/**
* Checks if the point (`x`,`y`) is contained within the square formed from (`upperLeftX`,`upperLeftY`) to
* (`lowerRightX`,`lowerRightY`)
*/
s32 Math3D_PointInSquare2D(f32 upperLeftX, f32 lowerRightX, f32 upperLeftY, f32 lowerRightY, f32 x, f32 y) {
if (x >= upperLeftX && x <= lowerRightX && y >= upperLeftY && y <= lowerRightY) {
return true;
}
return false;
}
/**
* Checks if the square formed around the circle with center (`centerX`,`centerY`) with radius `radius`
* touches any portion of the square formed around the triangle with vertices (`x0`,`y0`), (`x1`,`y1`),
* and (`x2`,`y2`)
*/
s32 Math3D_CirSquareVsTriSquare(f32 x0, f32 y0, f32 x1, f32 y1, f32 x2, f32 y2, f32 centerX, f32 centerY, f32 radius) {
f32 minX;
f32 maxX;
f32 minY;
f32 maxY;
minX = maxX = x0;
minY = maxY = y0;
if (x1 < minX) {
minX = x1;
} else if (maxX < x1) {
maxX = x1;
}
if (y1 < minY) {
minY = y1;
} else if (maxY < y1) {
maxY = y1;
}
if (x2 < minX) {
minX = x2;
} else if (maxX < x2) {
maxX = x2;
}
if (y2 < minY) {
minY = y2;
} else if (maxY < y2) {
maxY = y2;
}
if ((minX - radius) <= centerX && (maxX + radius) >= centerX && (minY - radius) <= centerY &&
(maxY + radius) >= centerY) {
return true;
}
return false;
}
/**
* Checks if the cube formed around the triangle formed from `v0`, `v1`, and `v2`
* has any portion touching the cube formed around the sphere with center `center`
* and radius of `radius`
*/
s32 Math3D_SphCubeVsTriCube(Vec3f* v0, Vec3f* v1, Vec3f* v2, Vec3f* center, f32 radius) {
f32 minX;
f32 maxX;
f32 minY;
f32 maxY;
f32 minZ;
f32 maxZ;
minX = maxX = v0->x;
minY = maxY = v0->y;
minZ = maxZ = v0->z;
if (v1->x < minX) {
minX = v1->x;
} else if (maxX < v1->x) {
maxX = v1->x;
}
if (v1->y < minY) {
minY = v1->y;
} else if (maxY < v1->y) {
maxY = v1->y;
}
if (v1->z < minZ) {
minZ = v1->z;
} else if (maxZ < v1->z) {
maxZ = v1->z;
}
if (v2->x < minX) {
minX = v2->x;
} else if (maxX < v2->x) {
maxX = v2->x;
}
if (v2->y < minY) {
minY = v2->y;
} else if (maxY < v2->y) {
maxY = v2->y;
}
if (v2->z < minZ) {
minZ = v2->z;
} else if (maxZ < v2->z) {
maxZ = v2->z;
}
if ((center->x >= (minX - radius)) && (center->x <= (maxX + radius)) && (center->y >= (minY - radius)) &&
(center->y <= (maxY + radius)) && (center->z >= (minZ - radius)) && (center->z <= (maxZ + radius))) {
return true;
}
return false;
}
/**
* Returns the distance squared between `a` and `b` on a single axis
*/
f32 Math3D_Dist1DSq(f32 a, f32 b) {
return SQ(a) + SQ(b);
}
/**
* Returns the distance between `a` and `b` on a single axis
*/
f32 Math3D_Dist1D(f32 a, f32 b) {
return sqrtf(Math3D_Dist1DSq(a, b));
}
/**
* Returns the distance squared between (`x0`,`y0`) and (`x1`,`x2`)
*/
f32 Math3D_Dist2DSq(f32 x0, f32 y0, f32 x1, f32 y1) {
return Math3D_Dist1DSq(x0 - x1, y0 - y1);
}
/**
* Returns the distance between points (`x0`,`y0`) and (`x1`,`y1`)
*/
f32 Math3D_Dist2D(f32 x0, f32 y0, f32 x1, f32 y1) {
return sqrtf(Math3D_Dist2DSq(x0, y0, x1, y1));
}
/**
* Returns the magnitude (length) squared of `vec`
*/
f32 Math3D_Vec3fMagnitudeSq(Vec3f* vec) {
return SQ(vec->x) + SQ(vec->y) + SQ(vec->z);
}
/**
* Returns the magnitude (length) of `vec`
*/
f32 Math3D_Vec3fMagnitude(Vec3f* vec) {
return sqrt(Math3D_Vec3fMagnitudeSq(vec));
}
/**
* Returns the distance between `a` and `b` squared.
*/
f32 Math3D_Vec3fDistSq(Vec3f* a, Vec3f* b) {
Vec3f diff;
Math_Vec3f_Diff(a, b, &diff);
return Math3D_Vec3fMagnitudeSq(&diff);
}
/*
* Calculates the distance between points `a` and `b`
*/
f32 Math3D_Vec3f_DistXYZ(Vec3f* a, Vec3f* b) {
return Math_Vec3f_DistXYZ(a, b);
}
/*
* Calculates the distance between `a` and `b`.
*/
f32 Math3D_DistXYZ16toF(Vec3s* a, Vec3f* b) {
Vec3f diff;
diff.x = a->x - b->x;
diff.y = a->y - b->y;
diff.z = a->z - b->z;
return Math3D_Vec3fMagnitude(&diff);
}
/**
* Gets the Z portion of the cross product of vectors `a - (`dx`,`dy`,z) and `b` - (`dx`,`dy`,z)
*/
f32 Math3D_Vec3fDiff_CrossZ(Vec3f* a, Vec3f* b, f32 dx, f32 dy) {
return ((a->x - dx) * (b->y - dy)) - ((a->y - dy) * (b->x - dx));
}
/**
* Gets the X portion of the cross product of vectors `a - (x,`dy`,`dz`) and `b` - (x,`dy`,`dz`)
*/
f32 Math3D_Vec3fDiff_CrossX(Vec3f* a, Vec3f* b, f32 dy, f32 dz) {
return ((a->y - dy) * (b->z - dz)) - ((a->z - dz) * (b->y - dy));
}
/**
* Gets the Y portion of the cross product of vectors `a - (`dx`,y,`dz`) and `b` - (`dx`,y,`dz`)
*/
f32 Math3D_Vec3fDiff_CrossY(Vec3f* a, Vec3f* b, f32 dz, f32 dx) {
return ((a->z - dz) * (b->x - dx)) - ((a->x - dx) * (b->z - dz));
}
/**
* Gets the Cross Product of vectors `a` and `b` and places the result in `ret`
*/
void Math3D_Vec3f_Cross(Vec3f* a, Vec3f* b, Vec3f* ret) {
ret->x = (a->y * b->z) - (a->z * b->y);
ret->y = (a->z * b->x) - (a->x * b->z);
ret->z = (a->x * b->y) - (a->y * b->x);
}
/*
* Calculates the normal vector to a surface with sides `vb` - `va` and `vc` - `va`
* outputs the normal to `normal`
*/
void Math3D_SurfaceNorm(Vec3f* va, Vec3f* vb, Vec3f* vc, Vec3f* normal) {
static Vec3f abDiff;
static Vec3f acDiff;
Math_Vec3f_Diff(vb, va, &abDiff);
Math_Vec3f_Diff(vc, va, &acDiff);
Math3D_Vec3f_Cross(&abDiff, &acDiff, normal);
}
/**
* Creates flags relative to the faces of a cube.
*/
s32 Math3D_PointRelativeToCubeFaces(Vec3f* point, Vec3f* min, Vec3f* max) {
s32 ret = 0;
if (point->x > max->x) {
ret = 1;
}
if (point->x < min->x) {
ret |= 2;
}
if (point->y > max->y) {
ret |= 4;
}
if (point->y < min->y) {
ret |= 8;
}
if (point->z > max->z) {
ret |= 0x10;
}
if (point->z < min->z) {
ret |= 0x20;
}
return ret;
}
/**
* Creates flags of `point` relative to the edges of a cube
*/
s32 Math3D_PointRelativeToCubeEdges(Vec3f* point, Vec3f* min, Vec3f* max) {
s32 ret = 0;
if ((-min->x + max->y) < (-point->x + point->y)) {
ret |= 1;
}
if ((-point->x + point->y) < (-max->x + min->y)) {
ret |= 2;
}
if ((max->x + max->y) < (point->x + point->y)) {
ret |= 4;
}
if ((point->x + point->y) < (min->x + min->y)) {
ret |= 8;
}
if ((-min->z + max->y) < (-point->z + point->y)) {
ret |= 0x10;
}
if ((-point->z + point->y) < (-max->z + min->y)) {
ret |= 0x20;
}
if ((max->z + max->y) < (point->z + point->y)) {
ret |= 0x40;
}
if ((point->z + point->y) < (min->z + min->y)) {
ret |= 0x80;
}
if ((-min->z + max->x) < (-point->z + point->x)) {
ret |= 0x100;
}
if ((-point->z + point->x) < (-max->z + min->x)) {
ret |= 0x200;
}
if ((max->z + max->x) < (point->z + point->x)) {
ret |= 0x400;
}
if ((point->z + point->x) < (min->z + min->x)) {
ret |= 0x800;
}
return ret;
}
/**
* Creates flags for `point` relative to the vertices of a cube
*/
s32 Math3D_PointRelativeToCubeVertices(Vec3f* point, Vec3f* min, Vec3f* max) {
s32 ret = 0;
if ((max->x + max->y + max->z) < (point->x + point->y + point->z)) {
ret = 1;
}
if ((-min->x + max->y + max->z) < (-point->x + point->y + point->z)) {
ret |= 2;
}
if ((-min->x + max->y - min->z) < (-point->x + point->y - point->z)) {
ret |= 4;
}
if ((max->x + max->y - min->z) < (point->x + point->y - point->z)) {
ret |= 8;
}
if ((max->x - min->y + max->z) < (point->x - point->y + point->z)) {
ret |= 0x10;
}
//! @bug: The next 2 conditions are the same check.
if ((-min->x - min->y + max->z) < (-point->x - point->y + point->z)) {
ret |= 0x20;
}
if ((-min->x - min->y + max->z) < (-point->x - point->y + point->z)) {
ret |= 0x40;
}
if ((-min->x - min->y - min->z) < (-point->x - point->y - point->z)) {
ret |= 0x80;
}
return ret;
}
/**
* Checks if a line segment with endpoints `a` and `b` intersect a cube
*/
s32 Math3D_LineVsCube(Vec3f* min, Vec3f* max, Vec3f* a, Vec3f* b) {
static Vec3f triVtx0;
static Vec3f triVtx1;
static Vec3f triVtx2;
static Vec3f intersectPoint;
s32 flags[2];
flags[0] = flags[1] = 0;
flags[0] = Math3D_PointRelativeToCubeFaces(a, min, max);
if (!flags[0]) {
return true;
}
flags[1] = Math3D_PointRelativeToCubeFaces(b, min, max);
if (!flags[1]) {
return true;
}
if (flags[0] & flags[1]) {
return false;
}
flags[0] |= (Math3D_PointRelativeToCubeEdges(a, min, max) << 8);
flags[1] |= (Math3D_PointRelativeToCubeEdges(b, min, max) << 8);
if (flags[0] & flags[1]) {
return false;
}
flags[0] |= (Math3D_PointRelativeToCubeVertices(a, min, max) << 0x18);
flags[1] |= (Math3D_PointRelativeToCubeVertices(b, min, max) << 0x18);
if (flags[0] & flags[1]) {
return false;
}
// face 1
triVtx0.x = min->x;
triVtx0.y = min->y;
triVtx0.z = min->z;
triVtx1.x = min->x;
triVtx1.y = min->y;
triVtx1.z = max->z;
triVtx2.x = min->x;
triVtx2.y = max->y;
triVtx2.z = max->z;
if (Math3D_TriLineIntersect(&triVtx0, &triVtx1, &triVtx2, -1.0f, 0.0f, 0.0f, min->x, a, b, &intersectPoint, 0)) {
return true;
}
triVtx0.x = min->x;
triVtx0.y = min->y;
triVtx0.z = min->z;
triVtx1.x = min->x;
triVtx1.y = max->y;
triVtx1.z = max->z;
triVtx2.x = min->x;
triVtx2.y = max->y;
triVtx2.z = min->z;
if (Math3D_TriLineIntersect(&triVtx0, &triVtx1, &triVtx2, -1.0f, 0.0f, 0.0f, min->x, a, b, &intersectPoint, 0)) {
return true;
}
// face 2
triVtx0.x = min->x;
triVtx0.y = max->y;
triVtx0.z = max->z;
triVtx1.x = min->x;
triVtx1.y = min->y;
triVtx1.z = max->z;
triVtx2.x = max->x;
triVtx2.y = max->y;
triVtx2.z = max->z;
if (Math3D_TriLineIntersect(&triVtx0, &triVtx1, &triVtx2, 0.0f, 0.0f, 1.0f, -max->z, a, b, &intersectPoint, 0)) {
return true;
}
triVtx0.x = max->x;
triVtx0.y = max->y;
triVtx0.z = max->z;
triVtx1.x = min->x;
triVtx1.y = min->y;
triVtx1.z = max->z;
triVtx2.x = max->x;
//! @bug trVtx1.y should be triVtx2.y, prevents a tri on the cube from being checked.
triVtx1.y = min->y;
triVtx2.z = max->z;
if (Math3D_TriLineIntersect(&triVtx0, &triVtx1, &triVtx2, 0.0f, 0.0f, 1.0f, -max->z, a, b, &intersectPoint, 0)) {
return true;
}
// face 3
triVtx0.x = max->x;
triVtx0.y = max->y;
triVtx0.z = max->z;
triVtx1.x = min->x;
triVtx1.y = max->y;
triVtx1.z = min->z;
triVtx2.x = min->x;
triVtx2.y = max->y;
triVtx2.z = max->z;
if (Math3D_TriLineIntersect(&triVtx0, &triVtx1, &triVtx2, 0.0f, 1.0f, 0.0f, -max->y, a, b, &intersectPoint, 0)) {
return true;
}
triVtx0.x = max->x;
triVtx0.y = max->y;
triVtx0.z = max->z;
triVtx1.x = max->x;
triVtx1.y = max->y;
triVtx1.z = min->z;
triVtx2.x = min->x;
triVtx2.y = max->y;
triVtx2.z = min->z;
if (Math3D_TriLineIntersect(&triVtx0, &triVtx1, &triVtx2, 0.0f, 1.0f, 0.0f, -max->y, a, b, &intersectPoint, 0)) {
return true;
}
// face 4
triVtx0.x = min->x;
triVtx0.y = min->y;
triVtx0.z = min->z;
triVtx1.x = min->x;
triVtx1.y = max->y;
triVtx1.z = min->z;
triVtx2.x = max->x;
triVtx2.y = max->y;
triVtx2.z = min->z;
if (Math3D_TriLineIntersect(&triVtx0, &triVtx1, &triVtx2, 0.0f, 0.0f, -1.0f, min->z, a, b, &intersectPoint, 0)) {
return true;
}
triVtx0.x = min->x;
triVtx0.y = min->y;
triVtx0.z = min->z;
triVtx1.x = max->x;
triVtx1.y = max->y;
triVtx1.z = min->z;
triVtx2.x = max->x;
triVtx2.y = min->y;
triVtx2.z = min->z;
// face 5
if (Math3D_TriLineIntersect(&triVtx0, &triVtx1, &triVtx2, 0.0f, 0.0f, -1.0f, min->z, a, b, &intersectPoint, 0)) {
return true;
}
triVtx0.x = min->x;
triVtx0.y = min->y;
triVtx0.z = min->z;
triVtx1.x = max->x;
triVtx1.y = min->y;
triVtx1.z = min->z;
triVtx2.x = max->x;
triVtx2.y = min->y;
triVtx2.z = max->z;
if (Math3D_TriLineIntersect(&triVtx0, &triVtx1, &triVtx2, 0.0f, -1.0f, 0.0f, min->y, a, b, &intersectPoint, 0)) {
return true;
}
triVtx0.x = min->x;
triVtx0.y = min->y;
triVtx0.z = min->z;
triVtx1.x = max->x;
triVtx1.y = min->y;
triVtx1.z = max->z;
triVtx2.x = min->x;
triVtx2.y = min->y;
triVtx2.z = max->z;
// face 6
if (Math3D_TriLineIntersect(&triVtx0, &triVtx1, &triVtx2, 0.0f, -1.0f, 0.0f, min->y, a, b, &intersectPoint, 0)) {
return true;
}
triVtx0.x = max->x;
triVtx0.y = max->y;
triVtx0.z = max->z;
triVtx1.x = max->x;
triVtx1.y = min->y;
triVtx1.z = min->z;
triVtx2.x = max->x;
triVtx2.y = max->y;
triVtx2.z = min->z;
if (Math3D_TriLineIntersect(&triVtx0, &triVtx1, &triVtx2, 1.0f, 0.0f, 0.0f, -max->x, a, b, &intersectPoint, 0)) {
return true;
}
triVtx0.x = max->x;
triVtx0.y = max->y;
triVtx0.z = max->z;
triVtx1.x = max->x;
triVtx1.y = min->y;
triVtx1.z = max->z;
triVtx2.x = max->x;
triVtx2.y = min->y;
triVtx2.z = min->z;
if (Math3D_TriLineIntersect(&triVtx0, &triVtx1, &triVtx2, 1.0f, 0.0f, 0.0f, -max->x, a, b, &intersectPoint, 0)) {
return true;
}
return false;
}
/**
* Checks if a line segment with endpoints `a` and `b` intersect a cube
*/
s32 Math3D_LineVsCubeShort(Vec3s* min, Vec3s* max, Vec3s* a, Vec3s* b) {
static Vec3f minF;
static Vec3f maxF;
static Vec3f aF;
static Vec3f bF;
minF.x = min->x;
minF.y = min->y;
minF.z = min->z;
maxF.x = max->x;
maxF.y = max->y;
maxF.z = max->z;
aF.x = a->x;
aF.y = a->y;
aF.z = a->z;
bF.x = b->x;
bF.y = b->y;
bF.z = b->z;
return Math3D_LineVsCube(&minF, &maxF, &aF, &bF);
}
/**
* Rotates the xz plane around the y axis `angle` degrees.
* outputs the plane equation `a``pointOnPlane->x` + 0y + `c``pointOnPlane->z`+`d` = 0
*/
void Math3D_RotateXZPlane(Vec3f* pointOnPlane, s16 angle, f32* a, f32* c, f32* d) {
*a = Math_SinS(angle) * 32767.0f;
*c = Math_CosS(angle) * 32767.0f;
*d = -((*a * pointOnPlane->x) + (*c * pointOnPlane->z));
}
/*
* Defines a plane from vertices `va`, `vb`, and `vc`. Normal components are output to
* `nx`, `ny`, and `nz`. Distance from the origin is output to `originDist`
* Satisfies the plane equation NxVx + NyVy + NzVz + D = 0
*/
void Math3D_DefPlane(Vec3f* va, Vec3f* vb, Vec3f* vc, f32* nx, f32* ny, f32* nz, f32* originDist) {
static Vec3f normal;
f32 normMagnitude;
f32 normMagInv;
Math3D_SurfaceNorm(va, vb, vc, &normal);
normMagnitude = sqrtf(SQ(normal.x) + SQ(normal.y) + SQ(normal.z));
if (!IS_ZERO(normMagnitude)) {
normMagInv = 1.0f / normMagnitude;
*nx = normal.x * normMagInv;
*ny = normal.y * normMagInv;
*nz = normal.z * normMagInv;
*originDist = -((*nx * va->x) + (*ny * va->y) + (*nz * va->z));
} else {
*originDist = 0.0f;
*nz = 0.0f;
*ny = 0.0f;
*nx = 0.0f;
}
}
/*
* Returns the answer to the plane equation with elements specified by arguments.
*/
f32 Math3D_Planef(f32 nx, f32 ny, f32 nz, f32 originDist, Vec3f* pointOnPlane) {
return (nx * pointOnPlane->x) + (ny * pointOnPlane->y) + (nz * pointOnPlane->z) + originDist;
}
/*
* Returns the answer to the plane equation
*/
f32 Math3D_Plane(Plane* plane, Vec3f* pointOnPlane) {
return (plane->normal.x * pointOnPlane->x) + (plane->normal.y * pointOnPlane->y) +
(plane->normal.z * pointOnPlane->z) + plane->originDist;
}
/*
* Calculates the absolute distance from a point `p` to the plane defined as
* `nx`, `ny`, `nz`, and `originDist`
*/
f32 Math3D_UDistPlaneToPos(f32 nx, f32 ny, f32 nz, f32 originDist, Vec3f* p) {
if (DEBUG_FEATURES && IS_ZERO(sqrtf(SQ(nx) + SQ(ny) + SQ(nz)))) {
PRINTF_COLOR_WARNING();
PRINTF(T("Math3DLengthPlaneAndPos():法線size がゼロ近いです%f %f %f\n",
"Math3DLengthPlaneAndPos(): Normal size is near zero %f %f %f\n"),
nx, ny, nz);
PRINTF_RST();
return 0.0f;
}
return fabsf(Math3D_DistPlaneToPos(nx, ny, nz, originDist, p));
}
/*
* Calculates the signed distance from a point `p` to a plane defined as
* `nx`, `ny`, `nz`, and `originDist`
*/
f32 Math3D_DistPlaneToPos(f32 nx, f32 ny, f32 nz, f32 originDist, Vec3f* p) {
f32 normMagnitude;
normMagnitude = sqrtf(SQ(nx) + SQ(ny) + SQ(nz));
if (IS_ZERO(normMagnitude)) {
PRINTF_COLOR_WARNING();
PRINTF(T("Math3DSignedLengthPlaneAndPos():法線size がゼロ近いです%f %f %f\n",
"Math3DSignedLengthPlaneAndPos(): Normal size is close to zero %f %f %f\n"),
nx, ny, nz);
PRINTF_RST();
return 0.0f;
}
return Math3D_Planef(nx, ny, nz, originDist, p) / normMagnitude;
}
/**
* Checks if the point defined by (`z`,`x`) is within distance of the triangle defined from `v0`,`v1`, and `v2`
*/
s32 Math3D_TriChkPointParaYImpl(Vec3f* v0, Vec3f* v1, Vec3f* v2, f32 z, f32 x, f32 detMax, f32 chkDist, f32 ny) {
f32 detv0v1;
f32 detv1v2;
f32 detv2v0;
f32 distToEdgeSq;
f32 chkDistSq;
// first check if the point is within range of the triangle.
if (!Math3D_CirSquareVsTriSquare(v0->z, v0->x, v1->z, v1->x, v2->z, v2->x, z, x, chkDist)) {
return false;
}
// check if the point is within `chkDist` units of any vertex of the triangle.
chkDistSq = SQ(chkDist);
if (((SQ(v0->z - z) + SQ(v0->x - x)) < chkDistSq) || ((SQ(v1->z - z) + SQ(v1->x - x)) < chkDistSq) ||
((SQ(v2->z - z) + SQ(v2->x - x)) < chkDistSq)) {
return true;
}
// Calculate the determinant of each face of the triangle to the point.
// If all the of determinants are within abs(`detMax`), return true.
detv0v1 = ((v0->z - z) * (v1->x - x)) - ((v0->x - x) * (v1->z - z));
detv1v2 = ((v1->z - z) * (v2->x - x)) - ((v1->x - x) * (v2->z - z));
detv2v0 = ((v2->z - z) * (v0->x - x)) - ((v2->x - x) * (v0->z - z));
if (((detMax >= detv0v1) && (detMax >= detv1v2) && (detMax >= detv2v0)) ||
((-detMax <= detv0v1) && (-detMax <= detv1v2) && (-detMax <= detv2v0))) {
return true;
}
if (fabsf(ny) > 0.5f) {
// Do a check on each face of the triangle, if the point is within `chkDist` units return true.
if (Math3D_PointDistSqToLine2D(z, x, v0->z, v0->x, v1->z, v1->x, &distToEdgeSq) && (distToEdgeSq < chkDistSq)) {
return true;
}
if (Math3D_PointDistSqToLine2D(z, x, v1->z, v1->x, v2->z, v2->x, &distToEdgeSq) && (distToEdgeSq < chkDistSq)) {
return true;
}
if (Math3D_PointDistSqToLine2D(z, x, v2->z, v2->x, v0->z, v0->x, &distToEdgeSq) && (distToEdgeSq < chkDistSq)) {
return true;
}
}
return false;
}
s32 Math3D_TriChkPointParaYDeterminate(Vec3f* v0, Vec3f* v1, Vec3f* v2, f32 z, f32 x, f32 detMax, f32 ny) {
return Math3D_TriChkPointParaYImpl(v0, v1, v2, z, x, detMax, 1.0f, ny);
}
s32 Math3D_TriChkPointParaYSlopedY(Vec3f* v0, Vec3f* v1, Vec3f* v2, f32 z, f32 x) {
return Math3D_TriChkPointParaYImpl(v0, v1, v2, z, x, 300.0f, 1.0f, 0.6f);
}
/**
* Performs the triangle and point check parallel to the Y axis, outputs the y coordinate of the point to `yIntersect`
*/
s32 Math3D_TriChkPointParaYIntersectDist(Vec3f* v0, Vec3f* v1, Vec3f* v2, f32 nx, f32 ny, f32 nz, f32 originDist, f32 z,
f32 x, f32* yIntersect, f32 chkDist) {
if (IS_ZERO(ny)) {
return false;
}
if (Math3D_TriChkPointParaYImpl(v0, v1, v2, z, x, 300.0f, chkDist, ny)) {
*yIntersect = (f32)((((-nx * x) - (nz * z)) - originDist) / ny);
return true;
}
return false;
}
s32 Math3D_TriChkPointParaYIntersectInsideTri(Vec3f* v0, Vec3f* v1, Vec3f* v2, f32 nx, f32 ny, f32 nz, f32 originDist,
f32 z, f32 x, f32* yIntersect, f32 chkDist) {
if (IS_ZERO(ny)) {
return false;
}
if (Math3D_TriChkPointParaYImpl(v0, v1, v2, z, x, 0.0f, chkDist, ny)) {
*yIntersect = (f32)((((-nx * x) - (nz * z)) - originDist) / ny);
return true;
}
return false;
}
s32 Math3D_TriChkPointParaY(Vec3f* v0, Vec3f* v1, Vec3f* v2, f32 ny, f32 z, f32 x) {
if (IS_ZERO(ny)) {
return false;
}
if (Math3D_TriChkPointParaYImpl(v0, v1, v2, z, x, 300.0f, 1.0f, ny)) {
return true;
}
return false;
}
s32 Math3D_TriChkLineSegParaYIntersect(Vec3f* v0, Vec3f* v1, Vec3f* v2, f32 nx, f32 ny, f32 nz, f32 originDist, f32 z,
f32 x, f32* yIntersect, f32 y0, f32 y1) {
f32 pointADist;
f32 pointBDist;
Vec3f planePos;
if (IS_ZERO(ny)) {
return false;
}
planePos.x = x;
planePos.y = y0;
planePos.z = z;
pointADist = Math3D_Planef(nx, ny, nz, originDist, &planePos);
planePos.y = y1;
pointBDist = Math3D_Planef(nx, ny, nz, originDist, &planePos);
if (((pointADist > 0.0f) && (pointBDist > 0.0f)) || ((pointADist < 0.0f) && (pointBDist < 0.0f))) {
return false;
}
if (Math3D_TriChkPointParaYImpl(v0, v1, v2, z, x, 300.0f, 1.0f, ny)) {
*yIntersect = (((-nx * x) - (nz * z)) - originDist) / ny;
return true;
}
return false;
}
s32 Math3D_TriChkPointParaYDist(Vec3f* v0, Vec3f* v1, Vec3f* v2, Plane* plane, f32 z, f32 x, f32 chkDist) {
if (IS_ZERO(plane->normal.y)) {
return false;
}
if (Math3D_TriChkPointParaYImpl(v0, v1, v2, z, x, 0.0f, chkDist, plane->normal.y)) {
return true;
}
return false;
}
s32 Math3D_TriChkPointParaXImpl(Vec3f* v0, Vec3f* v1, Vec3f* v2, f32 y, f32 z, f32 detMax, f32 chkDist, f32 nx) {
f32 detv0v1;
f32 detv1v2;
f32 detv2v0;
f32 distToEdgeSq;
f32 chkDistSq;
if (!Math3D_CirSquareVsTriSquare(v0->y, v0->z, v1->y, v1->z, v2->y, v2->z, y, z, chkDist)) {
return false;
}
chkDistSq = SQ(chkDist);
if (((SQ(v0->y - y) + SQ(v0->z - z)) < chkDistSq) || ((SQ(v1->y - y) + SQ(v1->z - z)) < chkDistSq) ||
((SQ(v2->y - y) + SQ(v2->z - z)) < chkDistSq)) {
return true;
}
detv0v1 = ((v0->y - y) * (v1->z - z)) - ((v0->z - z) * (v1->y - y));
detv1v2 = ((v1->y - y) * (v2->z - z)) - ((v1->z - z) * (v2->y - y));
detv2v0 = ((v2->y - y) * (v0->z - z)) - ((v2->z - z) * (v0->y - y));
if (((detv0v1 <= detMax) && (detv1v2 <= detMax) && (detv2v0 <= detMax)) ||
((-detMax <= detv0v1) && (-detMax <= detv1v2) && (-detMax <= detv2v0))) {
return true;
}
if (fabsf(nx) > 0.5f) {
if (Math3D_PointDistSqToLine2D(y, z, v0->y, v0->z, v1->y, v1->z, &distToEdgeSq) && (distToEdgeSq < chkDistSq)) {
return true;
}
if (Math3D_PointDistSqToLine2D(y, z, v1->y, v1->z, v2->y, v2->z, &distToEdgeSq) && (distToEdgeSq < chkDistSq)) {
return true;
}
if (Math3D_PointDistSqToLine2D(y, z, v2->y, v2->z, v0->y, v0->z, &distToEdgeSq) && (distToEdgeSq < chkDistSq)) {
return true;
}
}
return false;
}
s32 Math3D_TriChkPointParaXDeterminate(Vec3f* v0, Vec3f* v1, Vec3f* v2, f32 y, f32 z, f32 detMax, f32 nx) {
return Math3D_TriChkPointParaXImpl(v0, v1, v2, y, z, detMax, 1.0f, nx);
}
s32 Math3D_TriChkPointParaXIntersect(Vec3f* v0, Vec3f* v1, Vec3f* v2, f32 nx, f32 ny, f32 nz, f32 originDist, f32 y,
f32 z, f32* xIntersect) {
if (IS_ZERO(nx)) {
return false;
}
if (Math3D_TriChkPointParaXImpl(v0, v1, v2, y, z, 300.0f, 1.0f, nx)) {
*xIntersect = (((-ny * y) - (nz * z)) - originDist) / nx;
return true;
}
return false;
}
s32 Math3D_TriChkPointParaX(Vec3f* v0, Vec3f* v1, Vec3f* v2, f32 nx, f32 y, f32 z) {
if (IS_ZERO(nx)) {
return false;
}
if (Math3D_TriChkPointParaXImpl(v0, v1, v2, y, z, 300.0f, 1.0f, nx)) {
return true;
}
return false;
}
s32 Math3D_TriChkLineSegParaXIntersect(Vec3f* v0, Vec3f* v1, Vec3f* v2, f32 nx, f32 ny, f32 nz, f32 originDist, f32 y,
f32 z, f32* xIntersect, f32 x0, f32 x1) {
static Vec3f planePos;
f32 pointADist;
f32 pointBDist;
if (IS_ZERO(nx)) {
return false;
}
planePos.x = x0;
planePos.y = y;
planePos.z = z;
pointADist = Math3D_Planef(nx, ny, nz, originDist, &planePos);
planePos.x = x1;
pointBDist = Math3D_Planef(nx, ny, nz, originDist, &planePos);
if (((pointADist > 0.0f) && (pointBDist > 0.0f)) || ((pointADist < 0.0f) && (pointBDist < 0.0f))) {
return false;
}
if (Math3D_TriChkPointParaXImpl(v0, v1, v2, y, z, 300.0f, 1.0f, nx)) {
*xIntersect = (((-ny * y) - (nz * z)) - originDist) / nx;
return true;
}
return false;
}
s32 Math3D_TriChkPointParaXDist(Vec3f* v0, Vec3f* v1, Vec3f* v2, Plane* plane, f32 y, f32 z, f32 chkDist) {
if (IS_ZERO(plane->normal.x)) {
return false;
}
if (Math3D_TriChkPointParaXImpl(v0, v1, v2, y, z, 0.0f, chkDist, plane->normal.x)) {
return true;
}
return false;
}
s32 Math3D_TriChkPointParaZImpl(Vec3f* v0, Vec3f* v1, Vec3f* v2, f32 x, f32 y, f32 detMax, f32 chkDist, f32 nz) {
f32 detv0v1;
f32 detv1v2;
f32 detv2v0;
f32 distToEdgeSq;
f32 chkDistSq;
if (!Math3D_CirSquareVsTriSquare(v0->x, v0->y, v1->x, v1->y, v2->x, v2->y, x, y, chkDist)) {
return false;
}
chkDistSq = SQ(chkDist);
if (((SQ(x - v0->x) + SQ(y - v0->y)) < chkDistSq) || ((SQ(x - v1->x) + SQ(y - v1->y)) < chkDistSq) ||
((SQ(x - v2->x) + SQ(y - v2->y)) < chkDistSq)) {
// Distance from any vertex to a point is less than chkDist
return true;
}
detv0v1 = ((v0->x - x) * (v1->y - y)) - ((v0->y - y) * (v1->x - x));
detv1v2 = ((v1->x - x) * (v2->y - y)) - ((v1->y - y) * (v2->x - x));
detv2v0 = ((v2->x - x) * (v0->y - y)) - ((v2->y - y) * (v0->x - x));
if (((detMax >= detv0v1) && (detMax >= detv1v2) && (detMax >= detv2v0)) ||
((-detMax <= detv0v1) && (-detMax <= detv1v2) && (-detMax <= detv2v0))) {
return true;
}
if (fabsf(nz) > 0.5f) {
if (Math3D_PointDistSqToLine2D(x, y, v0->x, v0->y, v1->x, v1->y, &distToEdgeSq) && (distToEdgeSq < chkDistSq)) {
return true;
}
if (Math3D_PointDistSqToLine2D(x, y, v1->x, v1->y, v2->x, v2->y, &distToEdgeSq) && (distToEdgeSq < chkDistSq)) {
return true;
}
if (Math3D_PointDistSqToLine2D(x, y, v2->x, v2->y, v0->x, v0->y, &distToEdgeSq) && (distToEdgeSq < chkDistSq)) {
return true;
}
}
return false;
}
s32 Math3D_TriChkPointParaZDeterminate(Vec3f* v0, Vec3f* v1, Vec3f* v2, f32 x, f32 y, f32 detMax, f32 nz) {
return Math3D_TriChkPointParaZImpl(v0, v1, v2, x, y, detMax, 1.0f, nz);
}
s32 Math3D_TriChkPointParaZIntersect(Vec3f* v0, Vec3f* v1, Vec3f* v2, f32 nx, f32 ny, f32 nz, f32 originDist, f32 x,
f32 y, f32* zIntersect) {
if (IS_ZERO(nz)) {
return false;
}
if (Math3D_TriChkPointParaZImpl(v0, v1, v2, x, y, 300.0f, 1.0f, nz)) {
*zIntersect = (f32)((((-nx * x) - (ny * y)) - originDist) / nz);
return true;
}
return false;
}
s32 Math3D_TriChkPointParaZ(Vec3f* v0, Vec3f* v1, Vec3f* v2, f32 nz, f32 x, f32 y) {
if (IS_ZERO(nz)) {
return false;
}
if (Math3D_TriChkPointParaZImpl(v0, v1, v2, x, y, 300.0f, 1.0f, nz)) {
return true;
}
return false;
}
s32 Math3D_TriChkLineSegParaZIntersect(Vec3f* v0, Vec3f* v1, Vec3f* v2, f32 nx, f32 ny, f32 nz, f32 originDist, f32 x,
f32 y, f32* zIntersect, f32 z0, f32 z1) {
static Vec3f planePos;
f32 pointADist;
f32 pointBDist;
if (IS_ZERO(nz)) {
return false;
}
planePos.x = x;
planePos.y = y;
planePos.z = z0;
pointADist = Math3D_Planef(nx, ny, nz, originDist, &planePos);
planePos.z = z1;
pointBDist = Math3D_Planef(nx, ny, nz, originDist, &planePos);
if (((pointADist > 0.0f) && (pointBDist > 0.0f)) || ((pointADist < 0.0f) && (pointBDist < 0.0f))) {
// points on the line segment are on the same side of the plane
return false;
}
if (Math3D_TriChkPointParaZImpl(v0, v1, v2, x, y, 300.0f, 1.0f, nz)) {
*zIntersect = (((-nx * x) - (ny * y)) - originDist) / nz;
return true;
}
return false;
}
s32 Math3D_TriChkLineSegParaZDist(Vec3f* v0, Vec3f* v1, Vec3f* v2, Plane* plane, f32 x, f32 y, f32 chkDist) {
if (IS_ZERO(plane->normal.z)) {
return false;
}
if (Math3D_TriChkPointParaZImpl(v0, v1, v2, x, y, 0.0f, chkDist, plane->normal.z)) {
return true;
}
return false;
}
s32 Math3D_LineSegFindPlaneIntersect(f32 pointADist, f32 pointBDist, Vec3f* pointA, Vec3f* pointB, Vec3f* intersect) {
f32 distDiff;
distDiff = pointADist - pointBDist;
if (IS_ZERO(distDiff)) {
// both points lie on the plane.
*intersect = *pointB;
return false;
}
if (pointADist == 0.0f) {
// pointA is on the plane
*intersect = *pointA;
} else if (pointBDist == 0.0f) {
// pointB is on the plane
*intersect = *pointB;
} else {
// place the point at the intersection point.
Math3D_LineSplitRatio(pointA, pointB, pointADist / distDiff, intersect);
}
return true;
}
/**
* Determines if the line segment from `linePointA` to `linePointB` crosses the plane
* from `nx` + `ny` + `nz` + `originDist` = 0. If fromFront is set, then detection will only
* be true if point A crosses from the front of the plane
*/
s32 Math3D_LineSegVsPlane(f32 nx, f32 ny, f32 nz, f32 originDist, Vec3f* linePointA, Vec3f* linePointB,
Vec3f* intersect, s32 fromFront) {
f32 pointADist;
f32 pointBDist;
pointADist = Math3D_Planef(nx, ny, nz, originDist, linePointA);
pointBDist = Math3D_Planef(nx, ny, nz, originDist, linePointB);
if ((pointADist * pointBDist) > 0.0f) {
*intersect = *linePointB;
return false;
}
if (fromFront && (pointADist < 0.0f) && (pointBDist > 0.0f)) {
*intersect = *linePointB;
return false;
}
return Math3D_LineSegFindPlaneIntersect(pointADist, pointBDist, linePointA, linePointB, intersect);
}
/*
* Determines if the line formed by `linePointA` and `linePointB` intersect with Triangle formed from
* vertices `v0`, `v1`, and `v2` with normal vector `nx`, `ny`, and `nz` with plane distance from origin
* `originDist` Outputs the intersection point at to `intersect`
* Returns 1 if the line intersects with the triangle, 0 otherwise
*/
s32 Math3D_TriLineIntersect(Vec3f* v0, Vec3f* v1, Vec3f* v2, f32 nx, f32 ny, f32 nz, f32 originDist, Vec3f* linePointA,
Vec3f* linePointB, Vec3f* intersect, s32 fromFront) {
if (!Math3D_LineSegVsPlane(nx, ny, nz, originDist, linePointA, linePointB, intersect, fromFront)) {
return false;
}
if (((nx == 0.0f) || (Math3D_TriChkPointParaX(v0, v1, v2, nx, intersect->y, intersect->z))) &&
((ny == 0.0f) || (Math3D_TriChkPointParaY(v0, v1, v2, ny, intersect->z, intersect->x))) &&
((nz == 0.0f) || (Math3D_TriChkPointParaZ(v0, v1, v2, nz, intersect->x, intersect->y)))) {
return true;
}
*intersect = *linePointB;
return false;
}
/*
* Creates a TriNorm output to `tri`, and calculates the normal vector and plane from vertices
* `va`, `vb`, and `vc`
*/
void Math3D_TriNorm(TriNorm* tri, Vec3f* va, Vec3f* vb, Vec3f* vc) {
tri->vtx[0] = *va;
tri->vtx[1] = *vb;
tri->vtx[2] = *vc;
Math3D_DefPlane(va, vb, vc, &tri->plane.normal.x, &tri->plane.normal.y, &tri->plane.normal.z,
&tri->plane.originDist);
}
/*
* Determines if point `point` lies within `sphere`
*/
s32 Math3D_PointInSph(Sphere16* sphere, Vec3f* point) {
if (Math3D_DistXYZ16toF(&sphere->center, point) < sphere->radius) {
return true;
}
return false;
}
/**
* Determines the distance from point (`x0`,`y0`) to the line formed from (`x1`,`y1`) and (`x2`,`y2`)
* Distance squared is output to `lineLenSq`, returns true if the point perpendicular from (`x0`,`y0`)
* is contained within the segment between (`x1`,`y1`) and (`x2`,`y2`)
*/
s32 Math3D_PointDistSqToLine2D(f32 x0, f32 y0, f32 x1, f32 y1, f32 x2, f32 y2, f32* lineLenSq) {
static Vec3f perpendicularPoint;
f32 perpendicularRatio;
f32 xDiff;
f32 distSq;
f32 yDiff;
s32 ret = false;
xDiff = x2 - x1;
yDiff = y2 - y1;
distSq = SQ(xDiff) + SQ(yDiff);
if (IS_ZERO(distSq)) {
*lineLenSq = 0.0f;
return false;
}
perpendicularRatio = (((x0 - x1) * xDiff) + (y0 - y1) * yDiff) / distSq;
if (perpendicularRatio >= 0.0f && perpendicularRatio <= 1.0f) {
ret = true;
}
perpendicularPoint.x = (xDiff * perpendicularRatio) + x1;
perpendicularPoint.y = (yDiff * perpendicularRatio) + y1;
*lineLenSq = SQ(perpendicularPoint.x - x0) + SQ(perpendicularPoint.y - y0);
return ret;
}
/**
* Determines if the line `line` is touching the sphere `sphere` at any point in the line.
*/
s32 Math3D_LineVsSph(Sphere16* sphere, Linef* line) {
static Vec3f sphLinePerpendicularPoint;
Vec3f lineDiff;
f32 temp_f0_2;
f32 lineLenSq;
if ((Math3D_PointInSph(sphere, &line->a)) || (Math3D_PointInSph(sphere, &line->b))) {
// either point of the line is in the sphere.
return true;
}
lineDiff.x = line->b.x - line->a.x;
lineDiff.y = line->b.y - line->a.y;
lineDiff.z = line->b.z - line->a.z;
lineLenSq = SQ(lineDiff.x) + SQ(lineDiff.y) + SQ(lineDiff.z);
if (IS_ZERO(lineLenSq)) {
// line length is "0"
return false;
}
temp_f0_2 = ((((sphere->center.x - line->a.x) * lineDiff.x) + ((sphere->center.y - line->a.y) * lineDiff.y)) +
((sphere->center.z - line->a.z) * lineDiff.z)) /
lineLenSq;
if ((temp_f0_2 < 0.0f) || (temp_f0_2 > 1.0f)) {
return false;
}
sphLinePerpendicularPoint.x = (lineDiff.x * temp_f0_2) + line->a.x;
sphLinePerpendicularPoint.y = (lineDiff.y * temp_f0_2) + line->a.y;
sphLinePerpendicularPoint.z = (lineDiff.z * temp_f0_2) + line->a.z;
if (SQ(sphLinePerpendicularPoint.x - sphere->center.x) + SQ(sphLinePerpendicularPoint.y - sphere->center.y) +
SQ(sphLinePerpendicularPoint.z - sphere->center.z) <=
SQ((f32)sphere->radius)) {
return true;
}
return false;
}
/**
* Gets the surface point of `sphere` intersecting with `tri` generated from the line formed from the
* sphere's surface to the midpoint of the line formed from the first two vertices of the tri
*/
void Math3D_GetSphVsTriIntersectPoint(Sphere16* sphere, TriNorm* tri, Vec3f* intersectPoint) {
static Vec3f v0v1Center;
static Vec3f sphereCenter;
f32 dist;
f32 splitRatio;
v0v1Center.x = ((tri->vtx[0].x + tri->vtx[1].x) * 0.5f);
v0v1Center.y = ((tri->vtx[0].y + tri->vtx[1].y) * 0.5f);
v0v1Center.z = ((tri->vtx[0].z + tri->vtx[1].z) * 0.5f);
sphereCenter.x = sphere->center.x;
sphereCenter.y = sphere->center.y;
sphereCenter.z = sphere->center.z;
dist = Math3D_Vec3f_DistXYZ(&v0v1Center, &sphereCenter);
// Distance from the sphere's center to the center of the line formed from v0->v1
if (IS_ZERO(dist)) {
intersectPoint->x = sphereCenter.x;
intersectPoint->y = sphereCenter.y;
intersectPoint->z = sphereCenter.z;
return;
}
splitRatio = sphere->radius / dist;
Math3D_LineSplitRatio(&sphereCenter, &v0v1Center, splitRatio, intersectPoint);
}
/**
* Determines if `sphere` and `tri` and touching, and outputs the intersection point to `intersectPoint`
*/
s32 Math3D_TriVsSphIntersect(Sphere16* sphere, TriNorm* tri, Vec3f* intersectPoint) {
static Linef triTestLine;
static Vec3f sphereCenter;
static Vec3f sphPlanePos;
f32 radius;
f32 nx;
f32 ny;
f32 nz;
f32 planeDist;
sphereCenter.x = sphere->center.x;
sphereCenter.y = sphere->center.y;
sphereCenter.z = sphere->center.z;
radius = sphere->radius;
if (!Math3D_SphCubeVsTriCube(&tri->vtx[0], &tri->vtx[1], &tri->vtx[2], &sphereCenter, radius)) {
return false;
}
planeDist = Math3D_UDistPlaneToPos(tri->plane.normal.x, tri->plane.normal.y, tri->plane.normal.z,
tri->plane.originDist, &sphereCenter);
if (radius < planeDist) {
// the point that lies within the plane of the triangle which is perpendicular to the sphere's center is more
// than the radius of the sphere, the plane never crosses the sphere.
return false;
}
// tests if any of the edges of the triangle are intersecting the sphere
triTestLine.a = tri->vtx[0];
triTestLine.b = tri->vtx[1];
if (Math3D_LineVsSph(sphere, &triTestLine)) {
Math3D_GetSphVsTriIntersectPoint(sphere, tri, intersectPoint);
return true;
}
triTestLine.a = tri->vtx[1];
triTestLine.b = tri->vtx[2];
if (Math3D_LineVsSph(sphere, &triTestLine)) {
Math3D_GetSphVsTriIntersectPoint(sphere, tri, intersectPoint);
return true;
}
triTestLine.a = tri->vtx[2];
triTestLine.b = tri->vtx[0];
if (Math3D_LineVsSph(sphere, &triTestLine)) {
Math3D_GetSphVsTriIntersectPoint(sphere, tri, intersectPoint);
return true;
}
nx = tri->plane.normal.x * planeDist;
ny = tri->plane.normal.y * planeDist;
nz = tri->plane.normal.z * planeDist;
if (Math3D_Planef(tri->plane.normal.x, tri->plane.normal.y, tri->plane.normal.z, tri->plane.originDist,
&sphereCenter) > 0.0f) {
sphPlanePos.x = sphereCenter.x - nx;
sphPlanePos.y = sphereCenter.y - ny;
sphPlanePos.z = sphereCenter.z - nz;
} else {
sphPlanePos.x = sphereCenter.x + nx;
sphPlanePos.y = sphereCenter.y + ny;
sphPlanePos.z = sphereCenter.z + nz;
}
if (fabsf(tri->plane.normal.y) > 0.5f) {
if (Math3D_TriChkPointParaYDeterminate(&tri->vtx[0], &tri->vtx[1], &tri->vtx[2], sphPlanePos.z, sphPlanePos.x,
0.0f, tri->plane.normal.y)) {
Math3D_GetSphVsTriIntersectPoint(sphere, tri, intersectPoint);
return true;
}
} else if (fabsf(tri->plane.normal.x) > 0.5f) {
if (Math3D_TriChkPointParaXDeterminate(&tri->vtx[0], &tri->vtx[1], &tri->vtx[2], sphPlanePos.y, sphPlanePos.z,
0.0f, tri->plane.normal.x)) {
Math3D_GetSphVsTriIntersectPoint(sphere, tri, intersectPoint);
return true;
}
} else if (Math3D_TriChkPointParaZDeterminate(&tri->vtx[0], &tri->vtx[1], &tri->vtx[2], sphPlanePos.x,
sphPlanePos.y, 0.0f, tri->plane.normal.z)) {
Math3D_GetSphVsTriIntersectPoint(sphere, tri, intersectPoint);
return true;
}
return false;
}
/*
* Checks if point `point` is within cylinder `cyl`
* Returns 1 if the point is inside the cylinder, 0 otherwise.
*/
s32 Math3D_PointInCyl(Cylinder16* cyl, Vec3f* point) {
f32 bottom;
f32 top;
f32 x;
f32 z;
x = cyl->pos.x - point->x;
z = cyl->pos.z - point->z;
bottom = (f32)cyl->pos.y + cyl->yShift;
top = cyl->height + bottom;
if ((SQ(x) + SQ(z)) < SQ(cyl->radius) && (bottom < point->y) && (point->y < top)) {
return true;
} else {
return false;
}
}
s32 Math3D_CylVsLineSeg(Cylinder16* cyl, Vec3f* linePointA, Vec3f* linePointB, Vec3f* intersectA, Vec3f* intersectB) {
Vec3f cylToPtA;
Vec3f cylToPtB;
Vec3f ptAToPtB;
f32 fracA;
f32 fracB;
f32 fracBase;
f32 zero = 0.0f;
f32 pad;
f32 cylRadiusSq;
f32 radSqDiff;
f32 distCent2;
f32 dot2AB;
s32 sideIntA;
s32 sideIntB;
s32 intBeyondA;
s32 intBeyondB;
s32 intFlags = 0;
Vec3f intPts[4];
s32 count;
s32 i;
if (Math3D_PointInCyl(cyl, linePointA) && Math3D_PointInCyl(cyl, linePointB)) {
// both points are in the cylinder
*intersectA = *linePointA;
*intersectB = *linePointB;
return 2;
}
cylToPtA.x = linePointA->x - cyl->pos.x;
cylToPtA.y = linePointA->y - cyl->pos.y - cyl->yShift;
cylToPtA.z = linePointA->z - cyl->pos.z;
cylToPtB.x = linePointB->x - cyl->pos.x;
cylToPtB.y = linePointB->y - cyl->pos.y - cyl->yShift;
cylToPtB.z = linePointB->z - cyl->pos.z;
Math_Vec3f_Diff(&cylToPtB, &cylToPtA, &ptAToPtB);
cylRadiusSq = SQ(cyl->radius);
/**
* This section checks for intersections with the cylinder's base and top
*/
if (!IS_ZERO(ptAToPtB.y)) {
// fraction of length along AB to reach y = 0
fracBase = -cylToPtA.y / ptAToPtB.y;
if ((0.0f <= fracBase) && (fracBase <= 1.0f)) {
f32 baseIntX = (ptAToPtB.x * fracBase) + cylToPtA.x;
f32 baseIntZ = (ptAToPtB.z * fracBase) + cylToPtA.z;
if (SQ(baseIntX) + SQ(baseIntZ) < cylRadiusSq) {
// adds base intersection point to intPts and sets its flag
intPts[0].x = cyl->pos.x + baseIntX;
intPts[0].y = (f32)cyl->pos.y + cyl->yShift;
intPts[0].z = cyl->pos.z + baseIntZ;
intFlags |= 1;
}
}
// fraction of length along AB to reach y = cyl->height
fracA = (cyl->height - cylToPtA.y) / ptAToPtB.y;
if ((0.0f <= fracA) && (fracA <= 1.0f)) {
f32 topIntX = ptAToPtB.x * fracA + cylToPtA.x;
f32 topIntZ = ptAToPtB.z * fracA + cylToPtA.z;
if (SQ(topIntX) + SQ(topIntZ) < cylRadiusSq) {
// adds top intersection point to intPts and sets its flag
intPts[1].x = cyl->pos.x + topIntX;
intPts[1].y = (f32)cyl->pos.y + cyl->yShift + cyl->height;
intPts[1].z = cyl->pos.z + topIntZ;
intFlags |= 2;
}
}
}
/**
* This section finds the points of intersection of the infinite line containing AB with the side of the infinite
* cylinder containing cyl. Intersection points beyond the bounds of the segment and cylinder are filtered out
* afterward.
*/
radSqDiff = SQXZ(cylToPtA) - cylRadiusSq;
if (!IS_ZERO(2.0f * SQXZ(ptAToPtB))) {
dot2AB = 2.0f * DOTXZ(ptAToPtB, cylToPtA);
if (SQ(dot2AB) < 4.0f * SQXZ(ptAToPtB) * radSqDiff) {
// Line's closest xz-approach is outside cylinder. No intersections.
return 0;
}
if (SQ(dot2AB) - (4.0f * SQXZ(ptAToPtB) * radSqDiff) > zero) {
sideIntA = sideIntB = 1;
} else {
// Line is tangent in xz-plane. At most 1 side intersection.
sideIntA = 1;
sideIntB = 0;
}
distCent2 = sqrtf(SQ(dot2AB) - (4.0f * SQXZ(ptAToPtB) * radSqDiff));
if (sideIntA == 1) {
// fraction of length along AB for side intersection closer to A
fracA = (distCent2 - dot2AB) / (2.0f * SQXZ(ptAToPtB));
}
if (sideIntB == 1) {
// fraction of length along AB for side intersection closer to B
fracB = (-dot2AB - distCent2) / (2.0f * SQXZ(ptAToPtB));
}
} else if (!IS_ZERO(2.0f * DOTXZ(ptAToPtB, cylToPtA))) {
// Used if the line segment is nearly vertical. Unclear what it's calculating.
fracA = -radSqDiff / (2.0f * DOTXZ(ptAToPtB, cylToPtA));
sideIntA = 1;
sideIntB = 0;
} else {
return 0;
}
// checks for intersection points outside the bounds of the segment
if (!sideIntB) {
if (fracA < 0.0f || 1.0f < fracA) {
return 0;
}
} else {
intBeyondA = fracA < 0.0f || 1.0f < fracA;
intBeyondB = fracB < 0.0f || 1.0f < fracB;
if (intBeyondA && intBeyondB) {
return 0;
}
if (intBeyondA) {
sideIntA = 0;
}
if (intBeyondB) {
sideIntB = 0;
}
}
// checks for intersection points outside the bounds of the cylinder
if ((sideIntA == 1) &&
((fracA * ptAToPtB.y + cylToPtA.y) < 0.0f || cyl->height < (fracA * ptAToPtB.y + cylToPtA.y))) {
sideIntA = 0;
}
if ((sideIntB == 1) &&
((fracB * ptAToPtB.y + cylToPtA.y) < 0.0f || cyl->height < (fracB * ptAToPtB.y + cylToPtA.y))) {
sideIntB = 0;
}
if (sideIntA == 0 && sideIntB == 0) {
return 0;
}
// Adds intersection points to intPts and sets side A and side B flags
if (sideIntA == 1 && sideIntB == 1) {
intPts[2].x = (fracA * ptAToPtB.x + cylToPtA.x) + cyl->pos.x;
intPts[2].y = (fracA * ptAToPtB.y + cylToPtA.y) + cyl->pos.y + cyl->yShift;
intPts[2].z = (fracA * ptAToPtB.z + cylToPtA.z) + cyl->pos.z;
intFlags |= 4;
intPts[3].x = (fracB * ptAToPtB.x + cylToPtA.x) + cyl->pos.x;
intPts[3].y = (fracB * ptAToPtB.y + cylToPtA.y) + cyl->pos.y + cyl->yShift;
intPts[3].z = (fracB * ptAToPtB.z + cylToPtA.z) + cyl->pos.z;
intFlags |= 8;
} else if (sideIntA == 1) {
intPts[2].x = (fracA * ptAToPtB.x + cylToPtA.x) + cyl->pos.x;
intPts[2].y = (fracA * ptAToPtB.y + cylToPtA.y) + cyl->pos.y + cyl->yShift;
intPts[2].z = (fracA * ptAToPtB.z + cylToPtA.z) + cyl->pos.z;
intFlags |= 4;
} else if (sideIntB == 1) {
intPts[2].x = (fracB * ptAToPtB.x + cylToPtA.x) + cyl->pos.x;
intPts[2].y = (fracB * ptAToPtB.y + cylToPtA.y) + cyl->pos.y + cyl->yShift;
intPts[2].z = (fracB * ptAToPtB.z + cylToPtA.z) + cyl->pos.z;
intFlags |= 4;
}
/**
* Places the found intersection points into intersectA and intersectB. IntersectA is always closer to point A
*/
for (count = 0, i = 0; i < 4; i++) {
if (intFlags & (1 << i)) {
if (count == 0) {
*intersectA = intPts[i];
} else if (count == 1) {
if (Math3D_Vec3fDistSq(intersectA, linePointA) < Math3D_Vec3fDistSq(intersectA, &intPts[i])) {
*intersectB = intPts[i];
} else {
*intersectB = *intersectA;
*intersectA = intPts[i];
}
break;
}
count++;
}
}
return count;
}
/*
* Determines if `cyl` and `tri` are touching. The point of intersection
* is placed in `intersect` Returns 1 if they are touching, 0 otherwise.
*/
s32 Math3D_CylTriVsIntersect(Cylinder16* cyl, TriNorm* tri, Vec3f* intersect) {
static Sphere16 topSphere;
static Sphere16 bottomSphere;
static Vec3f cylIntersectA;
static Vec3f cylIntersectB;
f32 yIntersect;
f32 cylTop;
f32 cylBottom;
f32 minDistSq;
f32 radiusTodistFromCylYIntersectTov0v1;
f32 distFromPointAToIntersectASq;
Vec3f cylIntersectCenter;
Vec3f midpointv0v1;
Vec3f diffMidpointIntersect;
s32 pad;
cylBottom = (f32)cyl->pos.y + cyl->yShift;
cylTop = cyl->height + cylBottom;
if (((tri->vtx[0].y < cylBottom) && (tri->vtx[1].y < cylBottom) && (tri->vtx[2].y < cylBottom)) ||
((cylTop < tri->vtx[0].y) && (cylTop < tri->vtx[1].y) && (cylTop < tri->vtx[2].y))) {
// If all of the vertices are below or all of the vertices are above the cylinder.
return false;
}
minDistSq = 1.e38f;
if (Math3D_CylVsLineSeg(cyl, &tri->vtx[0], &tri->vtx[1], &cylIntersectA, &cylIntersectB)) {
distFromPointAToIntersectASq = Math3D_Vec3fDistSq(&cylIntersectA, &tri->vtx[0]);
minDistSq = distFromPointAToIntersectASq;
*intersect = cylIntersectA;
}
if (Math3D_CylVsLineSeg(cyl, &tri->vtx[2], &tri->vtx[1], &cylIntersectA, &cylIntersectB)) {
distFromPointAToIntersectASq = Math3D_Vec3fDistSq(&cylIntersectA, &tri->vtx[2]);
if (distFromPointAToIntersectASq < minDistSq) {
*intersect = cylIntersectA;
minDistSq = distFromPointAToIntersectASq;
}
}
if (Math3D_CylVsLineSeg(cyl, &tri->vtx[0], &tri->vtx[2], &cylIntersectA, &cylIntersectB)) {
distFromPointAToIntersectASq = Math3D_Vec3fDistSq(&cylIntersectA, &tri->vtx[0]);
if (distFromPointAToIntersectASq < minDistSq) {
*intersect = cylIntersectA;
minDistSq = distFromPointAToIntersectASq;
}
}
if (minDistSq != 1.e38f) {
return true;
}
if (Math3D_TriChkLineSegParaYIntersect(&tri->vtx[0], &tri->vtx[1], &tri->vtx[2], tri->plane.normal.x,
tri->plane.normal.y, tri->plane.normal.z, tri->plane.originDist, cyl->pos.z,
cyl->pos.x, &yIntersect, cylBottom, cylTop)) {
f32 distFromCylYIntersectTov0v1;
cylIntersectCenter.x = cyl->pos.x;
cylIntersectCenter.y = yIntersect;
cylIntersectCenter.z = cyl->pos.z;
midpointv0v1.x = (tri->vtx[0].x + tri->vtx[1].x) * 0.5f;
midpointv0v1.y = (tri->vtx[0].y + tri->vtx[1].y) * 0.5f;
midpointv0v1.z = (tri->vtx[0].z + tri->vtx[1].z) * 0.5f;
Math_Vec3f_Diff(&midpointv0v1, &cylIntersectCenter, &diffMidpointIntersect);
distFromCylYIntersectTov0v1 = sqrtf(SQ(diffMidpointIntersect.x) + SQ(diffMidpointIntersect.z));
if (IS_ZERO(distFromCylYIntersectTov0v1)) {
Math_Vec3f_Copy(intersect, &midpointv0v1);
return true;
}
radiusTodistFromCylYIntersectTov0v1 = cyl->radius / distFromCylYIntersectTov0v1;
Math3D_PointOnInfiniteLine(&cylIntersectCenter, &diffMidpointIntersect, radiusTodistFromCylYIntersectTov0v1,
intersect);
return true;
}
topSphere.center.x = bottomSphere.center.x = cyl->pos.x;
topSphere.center.z = bottomSphere.center.z = cyl->pos.z;
topSphere.center.y = cylTop;
bottomSphere.center.y = cylBottom;
topSphere.radius = bottomSphere.radius = cyl->radius;
if ((Math3D_TriVsSphIntersect(&topSphere, tri, intersect)) ||
(Math3D_TriVsSphIntersect(&bottomSphere, tri, intersect))) {
return true;
}
return false;
}
/*
* Determines if `cyl` and `tri` are touching.
*/
s32 Math3D_CylVsTri(Cylinder16* cyl, TriNorm* tri) {
Vec3f intersect;
return Math3D_CylTriVsIntersect(cyl, tri, &intersect);
}
/*
* Determines if two spheres are touching.
*/
s32 Math3D_SphVsSph(Sphere16* sphereA, Sphere16* sphereB) {
f32 overlapSize;
return Math3D_SphVsSphOverlap(sphereA, sphereB, &overlapSize);
}
/*
* Determines if two spheres are touching. The amount that they're overlapping is placed in `overlapSize`
*/
s32 Math3D_SphVsSphOverlap(Sphere16* sphereA, Sphere16* sphereB, f32* overlapSize) {
f32 centerDist;
return Math3D_SphVsSphOverlapCenterDist(sphereA, sphereB, overlapSize, ¢erDist);
}
/*
* Determines if two spheres are touching The distance from the centers is placed in `centerDist`,
* and the amount that they're overlapping is placed in `overlapSize`
*/
s32 Math3D_SphVsSphOverlapCenterDist(Sphere16* sphereA, Sphere16* sphereB, f32* overlapSize, f32* centerDist) {
Vec3f diff;
diff.x = (f32)sphereA->center.x - (f32)sphereB->center.x;
diff.y = (f32)sphereA->center.y - (f32)sphereB->center.y;
diff.z = (f32)sphereA->center.z - (f32)sphereB->center.z;
*centerDist = sqrt(SQ(diff.x) + SQ(diff.y) + SQ(diff.z));
*overlapSize = (((f32)sphereA->radius + (f32)sphereB->radius) - *centerDist);
if (*overlapSize > 0.008f) {
return true;
}
*overlapSize = 0.0f;
return false;
}
/**
* Checks if `sph` and `cyl` are touching, output the amount of xz overlap to `overlapSize`
*/
s32 Math3D_SphVsCylOverlap(Sphere16* sph, Cylinder16* cyl, f32* overlapSize) {
f32 centerDist;
return Math3D_SphVsCylOverlapCenterDist(sph, cyl, overlapSize, ¢erDist);
}
/**
* Checks if `sph` and `cyl` are touching, output the xz distance of the centers to `centerDist`, and the amount of
* xz overlap to `overlapSize`
*/
s32 Math3D_SphVsCylOverlapCenterDist(Sphere16* sph, Cylinder16* cyl, f32* overlapSize, f32* centerDist) {
static Cylinderf cylf;
static Spheref sphf;
f32 x;
f32 z;
f32 combinedRadius;
f32 cylBottom;
f32 cylTop;
f32 sphBottom;
f32 sphTop;
if (sph->radius <= 0 || cyl->radius <= 0) {
// either radius is 0
return false;
}
sphf.center.y = sph->center.y;
sphf.radius = sph->radius;
cylf.pos.y = cyl->pos.y;
cylf.yShift = cyl->yShift;
cylf.height = cyl->height;
x = (f32)sph->center.x - cyl->pos.x;
z = (f32)sph->center.z - cyl->pos.z;
combinedRadius = (f32)sph->radius + cyl->radius;
*centerDist = sqrtf(SQ(x) + SQ(z));
if (combinedRadius < *centerDist) {
// if the combined radii is less than the distance to the centers, they cannot be touching.
return false;
}
cylBottom = (cylf.pos.y + cylf.yShift);
cylTop = cylBottom + cylf.height;
sphBottom = sphf.center.y - sphf.radius;
sphTop = sphf.center.y + sphf.radius;
if ((sphTop >= cylBottom) && (sphBottom <= cylTop)) {
// if the cylinder and sphere are intersecting on the xz plane, check if they're intersecting on
// the y axis.
*overlapSize = combinedRadius - *centerDist;
return true;
}
return false;
}
/**
* Checks if `ca` and `cb` are touching, output the amount of xz overlap to `overlapSize`
*/
s32 Math3D_CylVsCylOverlap(Cylinder16* ca, Cylinder16* cb, f32* overlapSize) {
f32 xzDist;
return Math3D_CylVsCylOverlapCenterDist(ca, cb, overlapSize, &xzDist);
}
/**
* Checks if `ca` and `cb` are touching, output the xz distance of the centers to `centerDist`, and the amount of
* xz overlap to `overlapSize`
*/
s32 Math3D_CylVsCylOverlapCenterDist(Cylinder16* ca, Cylinder16* cb, f32* overlapSize, f32* centerDist) {
static Cylinderf caf;
static Cylinderf cbf;
Math_Vec3s_ToVec3f(&caf.pos, &ca->pos);
caf.radius = ca->radius;
caf.yShift = ca->yShift;
caf.height = ca->height;
Math_Vec3s_ToVec3f(&cbf.pos, &cb->pos);
cbf.radius = cb->radius;
cbf.yShift = cb->yShift;
cbf.height = cb->height;
*centerDist = sqrtf(SQ(caf.pos.x - cbf.pos.x) + SQ(caf.pos.z - cbf.pos.z));
// The combined radii are within the xz distance
if ((caf.radius + cbf.radius) < *centerDist) {
return false;
}
// top of ca < bottom of cb or top of cb < bottom of ca
if (((caf.pos.y + caf.yShift) + caf.height) < (cbf.pos.y + cbf.yShift) ||
(((cbf.pos.y + cbf.yShift) + cbf.height) < (caf.pos.y + caf.yShift))) {
return false;
}
*overlapSize = caf.radius + cbf.radius - *centerDist;
return true;
}
/*
* Determines if triangle `ta` intersects with triangle `tb` the point of
* intersection is output to `intersect.
* Returns 1 is the triangles intersect, 0 otherwise
*/
s32 Math3D_TriVsTriIntersect(TriNorm* ta, TriNorm* tb, Vec3f* intersect) {
f32 dist0;
f32 dist1;
f32 dist2;
dist0 = Math3D_Plane(&ta->plane, &tb->vtx[0]);
dist1 = Math3D_Plane(&ta->plane, &tb->vtx[1]);
dist2 = Math3D_Plane(&ta->plane, &tb->vtx[2]);
if (((dist0 > 0.0f) && (dist1 > 0.0f) && (dist2 > 0.0f)) ||
(((dist0 < 0.0f) && (dist1 < 0.0f)) && (dist2 < 0.0f))) {
return false;
}
dist0 = Math3D_Plane(&tb->plane, &ta->vtx[0]);
dist1 = Math3D_Plane(&tb->plane, &ta->vtx[1]);
dist2 = Math3D_Plane(&tb->plane, &ta->vtx[2]);
if ((((dist0 > 0.0f) && (dist1 > 0.0f)) && (dist2 > 0.0f)) ||
((dist0 < 0.0f) && (dist1 < 0.0f) && (dist2 < 0.0f))) {
return false;
}
if (Math3D_TriLineIntersect(&tb->vtx[0], &tb->vtx[1], &tb->vtx[2], tb->plane.normal.x, tb->plane.normal.y,
tb->plane.normal.z, tb->plane.originDist, &ta->vtx[0], &ta->vtx[1], intersect, 0)) {
return true;
}
if (Math3D_TriLineIntersect(&tb->vtx[0], &tb->vtx[1], &tb->vtx[2], tb->plane.normal.x, tb->plane.normal.y,
tb->plane.normal.z, tb->plane.originDist, &ta->vtx[1], &ta->vtx[2], intersect, 0)) {
return true;
}
if (Math3D_TriLineIntersect(&tb->vtx[0], &tb->vtx[1], &tb->vtx[2], tb->plane.normal.x, tb->plane.normal.y,
tb->plane.normal.z, tb->plane.originDist, &ta->vtx[2], &ta->vtx[0], intersect, 0)) {
return true;
}
if (Math3D_TriLineIntersect(&ta->vtx[0], &ta->vtx[1], &ta->vtx[2], ta->plane.normal.x, ta->plane.normal.y,
ta->plane.normal.z, ta->plane.originDist, &tb->vtx[0], &tb->vtx[1], intersect,
0) == 1) {
return true;
}
if (Math3D_TriLineIntersect(&ta->vtx[0], &ta->vtx[1], &ta->vtx[2], ta->plane.normal.x, ta->plane.normal.y,
ta->plane.normal.z, ta->plane.originDist, &tb->vtx[1], &tb->vtx[2], intersect,
0) == 1) {
return true;
}
if (Math3D_TriLineIntersect(&ta->vtx[0], &ta->vtx[1], &ta->vtx[2], ta->plane.normal.x, ta->plane.normal.y,
ta->plane.normal.z, ta->plane.originDist, &tb->vtx[2], &tb->vtx[0], intersect,
0) == 1) {
return true;
}
return false;
}
s32 Math3D_XZInSphere(Sphere16* sphere, f32 x, f32 z) {
f32 xDiff;
f32 zDiff;
xDiff = sphere->center.x - x;
zDiff = sphere->center.z - z;
if ((SQ(xDiff) + SQ(zDiff)) <= SQ(sphere->radius)) {
return true;
}
return false;
}
s32 Math3D_XYInSphere(Sphere16* sphere, f32 x, f32 y) {
f32 xDiff;
f32 yDiff;
xDiff = sphere->center.x - x;
yDiff = sphere->center.y - y;
if ((SQ(xDiff) + SQ(yDiff)) <= SQ(sphere->radius)) {
return true;
}
return false;
}
s32 Math3D_YZInSphere(Sphere16* sphere, f32 y, f32 z) {
f32 yDiff;
f32 zDiff;
yDiff = sphere->center.y - y;
zDiff = sphere->center.z - z;
if ((SQ(yDiff) + SQ(zDiff)) <= SQ(sphere->radius)) {
return true;
}
return false;
}
|