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
|
//========= Copyright Valve Corporation, All rights reserved. ============//
//
// Purpose: The airboat, a sporty nimble water craft.
//
//=============================================================================//
#include "cbase.h"
#include "physics_airboat.h"
#include "cmodel.h"
#include <ivp_ray_solver.hxx>
// memdbgon must be the last include file in a .cpp file!!!
#include "tier0/memdbgon.h"
#ifdef _X360
#define AIRBOAT_STEERING_RATE_MIN 0.000225f
#define AIRBOAT_STEERING_RATE_MAX (10.0f * AIRBOAT_STEERING_RATE_MIN)
#define AIRBOAT_STEERING_INTERVAL 1.5f
#else
#define AIRBOAT_STEERING_RATE_MIN 0.00045f
#define AIRBOAT_STEERING_RATE_MAX (5.0f * AIRBOAT_STEERING_RATE_MIN)
#define AIRBOAT_STEERING_INTERVAL 0.5f
#endif //_X360
#define AIRBOAT_ROT_DRAG 0.00004f
#define AIRBOAT_ROT_DAMPING 0.001f
// Mass-independent thrust values
#define AIRBOAT_THRUST_MAX 11.0f // N / kg
#define AIRBOAT_THRUST_MAX_REVERSE 7.5f // N / kg
// Mass-independent drag values
#define AIRBOAT_WATER_DRAG_LEFT_RIGHT 0.6f
#define AIRBOAT_WATER_DRAG_FORWARD_BACK 0.005f
#define AIRBOAT_WATER_DRAG_UP_DOWN 0.0025f
#define AIRBOAT_GROUND_DRAG_LEFT_RIGHT 2.0
#define AIRBOAT_GROUND_DRAG_FORWARD_BACK 1.0
#define AIRBOAT_GROUND_DRAG_UP_DOWN 0.8
#define AIRBOAT_DRY_FRICTION_SCALE 0.6f // unitless, reduces our friction on all surfaces other than water
#define AIRBOAT_RAYCAST_DIST 0.35f // m (~14in)
#define AIRBOAT_RAYCAST_DIST_WATER_LOW 0.1f // m (~4in)
#define AIRBOAT_RAYCAST_DIST_WATER_HIGH 0.35f // m (~16in)
// Amplitude of wave noise. Blend from max to min as speed increases.
#define AIRBOAT_WATER_NOISE_MIN 0.01 // m (~0.4in)
#define AIRBOAT_WATER_NOISE_MAX 0.03 // m (~1.2in)
// Frequency of wave noise. Blend from min to max as speed increases.
#define AIRBOAT_WATER_FREQ_MIN 1.5
#define AIRBOAT_WATER_FREQ_MAX 1.5
// Phase difference in wave noise between left and right pontoons
// Blend from max to min as speed increases.
#define AIRBOAT_WATER_PHASE_MIN 0.0 // s
#define AIRBOAT_WATER_PHASE_MAX 1.5 // s
#define AIRBOAT_GRAVITY 9.81f // m/s2
// Pontoon indices
enum
{
AIRBOAT_PONTOON_FRONT_LEFT = 0,
AIRBOAT_PONTOON_FRONT_RIGHT,
AIRBOAT_PONTOON_REAR_LEFT,
AIRBOAT_PONTOON_REAR_RIGHT,
};
class IVP_Ray_Solver_Template;
class IVP_Ray_Hit;
class IVP_Event_Sim;
//-----------------------------------------------------------------------------
// Purpose: Constructor
//-----------------------------------------------------------------------------
class CAirboatFrictionData : public IPhysicsCollisionData
{
public:
CAirboatFrictionData()
{
m_vecPoint.Init( 0, 0, 0 );
m_vecNormal.Init( 0, 0, 0 );
m_vecVelocity.Init( 0, 0, 0 );
}
virtual void GetSurfaceNormal( Vector &out )
{
out = m_vecPoint;
}
virtual void GetContactPoint( Vector &out )
{
out = m_vecNormal;
}
virtual void GetContactSpeed( Vector &out )
{
out = m_vecVelocity;
}
public:
Vector m_vecPoint;
Vector m_vecNormal;
Vector m_vecVelocity;
};
//-----------------------------------------------------------------------------
// Purpose: Constructor
//-----------------------------------------------------------------------------
CPhysics_Airboat::CPhysics_Airboat( IVP_Environment *pEnv, const IVP_Template_Car_System *pCarSystem,
IPhysicsGameTrace *pGameTrace )
{
InitRaycastCarBody( pCarSystem );
InitRaycastCarEnvironment( pEnv, pCarSystem );
InitRaycastCarWheels( pCarSystem );
InitRaycastCarAxes( pCarSystem );
InitAirboat( pCarSystem );
m_pGameTrace = pGameTrace;
m_SteeringAngle = 0;
m_bSteeringReversed = false;
m_flThrust = 0;
m_bAirborne = false;
m_flAirTime = 0;
m_bWeakJump = false;
m_flPitchErrorPrev = 0;
m_flRollErrorPrev = 0;
}
//-----------------------------------------------------------------------------
// Purpose: Deconstructor
//-----------------------------------------------------------------------------
CPhysics_Airboat::~CPhysics_Airboat()
{
m_pAirboatBody->get_environment()->get_controller_manager()->remove_controller_from_environment( this, IVP_TRUE );
}
//-----------------------------------------------------------------------------
// Purpose: Setup the car system wheels.
//-----------------------------------------------------------------------------
void CPhysics_Airboat::InitAirboat( const IVP_Template_Car_System *pCarSystem )
{
for ( int iWheel = 0; iWheel < pCarSystem->n_wheels; ++iWheel )
{
m_pWheels[iWheel] = pCarSystem->car_wheel[iWheel];
m_pWheels[iWheel]->enable_collision_detection( IVP_FALSE );
}
CPhysicsObject* pBodyObject = static_cast<CPhysicsObject*>(pCarSystem->car_body->client_data);
pBodyObject->EnableGravity( false );
// We do our own buoyancy simulation.
pBodyObject->SetCallbackFlags( pBodyObject->GetCallbackFlags() & ~CALLBACK_DO_FLUID_SIMULATION );
}
//-----------------------------------------------------------------------------
// Purpose: Get the raycast wheel.
//-----------------------------------------------------------------------------
IPhysicsObject *CPhysics_Airboat::GetWheel( int index )
{
Assert( index >= 0 );
Assert( index < n_wheels );
return ( IPhysicsObject* )m_pWheels[index]->client_data;
}
//-----------------------------------------------------------------------------
// Purpose:
//-----------------------------------------------------------------------------
void CPhysics_Airboat::SetWheelFriction( int iWheel, float flFriction )
{
change_friction_of_wheel( IVP_POS_WHEEL( iWheel ), flFriction );
}
//-----------------------------------------------------------------------------
// Purpose: Returns an amount to add to the front pontoon raycasts to simulate wave noise.
// Input : nPontoonIndex - Which pontoon we're dealing with (0 or 1).
// flSpeedRatio - Speed as a ratio of max speed [0..1]
//-----------------------------------------------------------------------------
float CPhysics_Airboat::ComputeFrontPontoonWaveNoise( int nPontoonIndex, float flSpeedRatio )
{
// Add in sinusoidal noise cause by undulating water. Reduce the amplitude of the noise at higher speeds.
IVP_FLOAT flNoiseScale = RemapValClamped( 1.0 - flSpeedRatio, 0, 1, AIRBOAT_WATER_NOISE_MIN, AIRBOAT_WATER_NOISE_MAX );
// Apply a phase shift between left and right pontoons to simulate waves passing under the boat.
IVP_FLOAT flPhaseShift = 0;
if ( flSpeedRatio < 0.3 )
{
// BUG: this allows a discontinuity in the waveform - use two superimposed sine waves instead?
flPhaseShift = nPontoonIndex * AIRBOAT_WATER_PHASE_MAX;
}
// Increase the wave frequency as speed increases.
IVP_FLOAT flFrequency = RemapValClamped( flSpeedRatio, 0, 1, AIRBOAT_WATER_FREQ_MIN, AIRBOAT_WATER_FREQ_MAX );
//Msg( "Wave amp=%f, freq=%f, phase=%f\n", flNoiseScale, flFrequency, flPhaseShift );
return flNoiseScale * sin( flFrequency * ( m_pCore->environment->get_current_time().get_seconds() + flPhaseShift ) );
}
//-----------------------------------------------------------------------------
// Purpose:: Convert data to HL2 measurements, and test direction of raycast.
//-----------------------------------------------------------------------------
void CPhysics_Airboat::pre_raycasts_gameside( int nRaycastCount, IVP_Ray_Solver_Template *pRays,
Ray_t *pGameRays, IVP_Raycast_Airboat_Impact *pImpacts )
{
IVP_FLOAT flForwardSpeedRatio = clamp( m_vecLocalVelocity.k[2] / 10.0f, 0.f, 1.0f );
//Msg( "flForwardSpeedRatio = %f\n", flForwardSpeedRatio );
IVP_FLOAT flSpeed = ( IVP_FLOAT )m_pCore->speed.real_length();
IVP_FLOAT flSpeedRatio = clamp( flSpeed / 15.0f, 0.f, 1.0f );
if ( !m_flThrust )
{
flForwardSpeedRatio *= 0.5;
}
// This is a little weird. We adjust the front pontoon ray lengths based on forward velocity,
// but ONLY if both pontoons are in the water, which we won't know until we do the raycast.
// So we do most of the work here, and cache some of the results to use them later.
Vector vecStart[4];
Vector vecDirection[4];
Vector vecZero( 0.0f, 0.0f, 0.0f );
int nFrontPontoonsInWater = 0;
int iRaycast;
for ( iRaycast = 0; iRaycast < nRaycastCount; ++iRaycast )
{
// Setup the ray.
ConvertPositionToHL( pRays[iRaycast].ray_start_point, vecStart[iRaycast] );
ConvertDirectionToHL( pRays[iRaycast].ray_normized_direction, vecDirection[iRaycast] );
float flRayLength = IVP2HL( pRays[iRaycast].ray_length );
// Check to see if that point is in water.
pImpacts[iRaycast].bInWater = IVP_FALSE;
if ( m_pGameTrace->VehiclePointInWater( vecStart[iRaycast] ) )
{
vecDirection[iRaycast].Negate();
pImpacts[iRaycast].bInWater = IVP_TRUE;
}
Vector vecEnd = vecStart[iRaycast] + ( vecDirection[iRaycast] * flRayLength );
// Adjust the trace if the pontoon is in the water.
if ( m_pGameTrace->VehiclePointInWater( vecEnd ) )
{
// Reduce the ray length in the water.
pRays[iRaycast].ray_length = AIRBOAT_RAYCAST_DIST_WATER_LOW;
if ( iRaycast < 2 )
{
nFrontPontoonsInWater++;
// Front pontoons.
// Add a little sinusoidal noise to simulate waves.
IVP_FLOAT flNoise = ComputeFrontPontoonWaveNoise( iRaycast, flSpeedRatio );
pRays[iRaycast].ray_length += flNoise;
}
else
{
// Recalculate the end position in HL coordinates.
flRayLength = IVP2HL( pRays[iRaycast].ray_length );
vecEnd = vecStart[iRaycast] + ( vecDirection[iRaycast] * flRayLength );
}
}
pGameRays[iRaycast].Init( vecStart[iRaycast], vecEnd, vecZero, vecZero );
}
// If both front pontoons are in the water, add in a bit of lift proportional to our
// forward speed. We can't do this to only one of the front pontoons because it causes
// some twist if we do.
// FIXME: this does some redundant work (computes the wave noise again)
if ( nFrontPontoonsInWater == 2 )
{
for ( int i = 0; i < 2; i++ )
{
// Front pontoons.
// Raise it higher out of the water as we go faster forward.
pRays[i].ray_length = RemapValClamped( flForwardSpeedRatio, 0, 1, AIRBOAT_RAYCAST_DIST_WATER_LOW, AIRBOAT_RAYCAST_DIST_WATER_HIGH );
// Add a little sinusoidal noise to simulate waves.
IVP_FLOAT flNoise = ComputeFrontPontoonWaveNoise( i, flSpeedRatio );
pRays[i].ray_length += flNoise;
// Recalculate the end position in HL coordinates.
float flRayLength = IVP2HL( pRays[i].ray_length );
Vector vecEnd = vecStart[i] + ( vecDirection[i] * flRayLength );
pGameRays[i].Init( vecStart[i], vecEnd, vecZero, vecZero );
}
}
}
//-----------------------------------------------------------------------------
// Purpose:
//-----------------------------------------------------------------------------
float CPhysics_Airboat::GetWaterDepth( Ray_t *pGameRay, IPhysicsObject *pPhysAirboat )
{
float flDepth = 0.0f;
trace_t trace;
Ray_t waterRay;
Vector vecStart = pGameRay->m_Start;
Vector vecEnd( vecStart.x, vecStart.y, vecStart.z + 1000.0f );
Vector vecZero( 0.0f, 0.0f, 0.0f );
waterRay.Init( vecStart, vecEnd, vecZero, vecZero );
m_pGameTrace->VehicleTraceRayWithWater( waterRay, pPhysAirboat->GetGameData(), &trace );
flDepth = 1000.0f * trace.fractionleftsolid;
return flDepth;
}
//-----------------------------------------------------------------------------
// Purpose: Performs traces to figure out what is at each of the raycast points
// and fills out the pImpacts array with that information.
// Input : nRaycastCount - Number of elements in the arrays pointed to by pRays
// and pImpacts.
// pRays - Holds the rays to trace with.
// pImpacts - Receives the trace results.
//-----------------------------------------------------------------------------
void CPhysics_Airboat::do_raycasts_gameside( int nRaycastCount, IVP_Ray_Solver_Template *pRays,
IVP_Raycast_Airboat_Impact *pImpacts )
{
Assert( nRaycastCount >= 0 );
Assert( nRaycastCount <= IVP_RAYCAST_AIRBOAT_MAX_WHEELS );
Ray_t gameRays[IVP_RAYCAST_AIRBOAT_MAX_WHEELS];
pre_raycasts_gameside( nRaycastCount, pRays, gameRays, pImpacts );
// Do the raycasts and set impact data.
trace_t trace;
for ( int iRaycast = 0; iRaycast < nRaycastCount; ++iRaycast )
{
// Trace.
if ( pImpacts[iRaycast].bInWater )
{
// The start position is underwater. Trace up to find the water surface.
IPhysicsObject *pPhysAirboat = static_cast<IPhysicsObject*>( m_pAirboatBody->client_data );
m_pGameTrace->VehicleTraceRay( gameRays[iRaycast], pPhysAirboat->GetGameData(), &trace );
pImpacts[iRaycast].flDepth = GetWaterDepth( &gameRays[iRaycast], pPhysAirboat );
}
else
{
// Trace down to find the ground or water.
IPhysicsObject *pPhysAirboat = static_cast<IPhysicsObject*>( m_pAirboatBody->client_data );
m_pGameTrace->VehicleTraceRayWithWater( gameRays[iRaycast], pPhysAirboat->GetGameData(), &trace );
}
ConvertPositionToIVP( gameRays[iRaycast].m_Start + gameRays[iRaycast].m_StartOffset, m_CarSystemDebugData.wheelRaycasts[iRaycast][0] );
ConvertPositionToIVP( gameRays[iRaycast].m_Start + gameRays[iRaycast].m_StartOffset + gameRays[iRaycast].m_Delta, m_CarSystemDebugData.wheelRaycasts[iRaycast][1] );
m_CarSystemDebugData.wheelRaycastImpacts[iRaycast] = trace.fraction * gameRays[iRaycast].m_Delta.Length();
// Set impact data.
pImpacts[iRaycast].bImpactWater = IVP_FALSE;
pImpacts[iRaycast].bImpact = IVP_FALSE;
if ( trace.fraction != 1.0f )
{
pImpacts[iRaycast].bImpact = IVP_TRUE;
// Set water surface flag.
pImpacts[iRaycast].flDepth = 0.0f;
if ( trace.contents & MASK_WATER )
{
pImpacts[iRaycast].bImpactWater = IVP_TRUE;
}
// Save impact surface data.
ConvertPositionToIVP( trace.endpos, pImpacts[iRaycast].vecImpactPointWS );
ConvertDirectionToIVP( trace.plane.normal, pImpacts[iRaycast].vecImpactNormalWS );
// Save surface properties.
const surfacedata_t *pSurfaceData = physprops->GetSurfaceData( trace.surface.surfaceProps );
pImpacts[iRaycast].nSurfaceProps = trace.surface.surfaceProps;
if (pImpacts[iRaycast].vecImpactNormalWS.k[1] < -0.707)
{
// dampening is 1/t, where t is how long it takes to come to a complete stop
pImpacts[iRaycast].flDampening = pSurfaceData->physics.dampening;
pImpacts[iRaycast].flFriction = pSurfaceData->physics.friction;
}
else
{
// This surface is too vertical -- no friction or damping from it.
pImpacts[iRaycast].flDampening = pSurfaceData->physics.dampening;
pImpacts[iRaycast].flFriction = pSurfaceData->physics.friction;
}
}
}
}
//-----------------------------------------------------------------------------
// Purpose: Entry point for airboat simulation.
//-----------------------------------------------------------------------------
void CPhysics_Airboat::do_simulation_controller( IVP_Event_Sim *pEventSim, IVP_U_Vector<IVP_Core> * )
{
IVP_Ray_Solver_Template raySolverTemplates[IVP_RAYCAST_AIRBOAT_MAX_WHEELS];
IVP_Raycast_Airboat_Impact impacts[IVP_RAYCAST_AIRBOAT_MAX_WHEELS];
// Cache some data into members here so we only do the work once.
m_pCore = m_pAirboatBody->get_core();
const IVP_U_Matrix *matWorldFromCore = m_pCore->get_m_world_f_core_PSI();
// Cache the speed.
m_flSpeed = ( IVP_FLOAT )m_pCore->speed.real_length();
// Cache the local velocity vector.
matWorldFromCore->vimult3(&m_pCore->speed, &m_vecLocalVelocity);
// Raycasts.
PreRaycasts( raySolverTemplates, matWorldFromCore, impacts );
do_raycasts_gameside( n_wheels, raySolverTemplates, impacts );
if ( !PostRaycasts( raySolverTemplates, matWorldFromCore, impacts ) )
return;
UpdateAirborneState( impacts, pEventSim );
// Enumerate the controllers attached to us.
//for (int i = m_pCore->controllers_of_core.len() - 1; i >= 0; i--)
//{
// IVP_Controller *pController = m_pCore->controllers_of_core.element_at(i);
//}
// Pontoons. Buoyancy or ground impacts.
DoSimulationPontoons( impacts, pEventSim );
// Drag due to water and ground friction.
DoSimulationDrag( impacts, pEventSim );
// Turbine (fan).
DoSimulationTurbine( pEventSim );
// Steering.
DoSimulationSteering( pEventSim );
// Anti-pitch.
DoSimulationKeepUprightPitch( impacts, pEventSim );
// Anti-roll.
DoSimulationKeepUprightRoll( impacts, pEventSim );
// Additional gravity based on speed.
DoSimulationGravity( pEventSim );
}
//-----------------------------------------------------------------------------
// Purpose: Initialize the rays to be cast from the vehicle wheel positions to
// the "ground."
// Input : pRaySolverTemplates -
// matWorldFromCore -
// pImpacts -
//-----------------------------------------------------------------------------
void CPhysics_Airboat::PreRaycasts( IVP_Ray_Solver_Template *pRaySolverTemplates,
const IVP_U_Matrix *matWorldFromCore,
IVP_Raycast_Airboat_Impact *pImpacts )
{
int nPontoonPoints = n_wheels;
for ( int iPoint = 0; iPoint < nPontoonPoints; ++iPoint )
{
IVP_Raycast_Airboat_Wheel *pPontoonPoint = get_wheel( IVP_POS_WHEEL( iPoint ) );
if ( pPontoonPoint )
{
// Fill the in the ray solver template for the current wheel.
IVP_Ray_Solver_Template &raySolverTemplate = pRaySolverTemplates[iPoint];
// Transform the wheel "start" position from vehicle core-space to world-space. This is
// the raycast starting position.
matWorldFromCore->vmult4( &pPontoonPoint->raycast_start_cs, &raySolverTemplate.ray_start_point );
// Transform the shock (spring) direction from vehicle core-space to world-space. This is
// the raycast direction.
matWorldFromCore->vmult3( &pPontoonPoint->raycast_dir_cs, &pImpacts[iPoint].raycast_dir_ws );
raySolverTemplate.ray_normized_direction.set( &pImpacts[iPoint].raycast_dir_ws );
// Set the length of the ray cast.
raySolverTemplate.ray_length = AIRBOAT_RAYCAST_DIST;
// Set the ray solver template flags. This defines which objects you wish to
// collide against in the physics environment.
raySolverTemplate.ray_flags = IVP_RAY_SOLVER_ALL;
}
}
}
//-----------------------------------------------------------------------------
// Purpose: Determines whether we are airborne and whether we just performed a
// weak or strong jump. Weak jumps are jumps at below a threshold speed,
// and disable the turbine and pitch controller.
// Input : pImpacts -
// Output : Returns true on success, false on failure.
//-----------------------------------------------------------------------------
void CPhysics_Airboat::UpdateAirborneState( IVP_Raycast_Airboat_Impact *pImpacts, IVP_Event_Sim *pEventSim )
{
int nCount = CountSurfaceContactPoints(pImpacts);
if (!nCount)
{
if (!m_bAirborne)
{
m_bAirborne = true;
m_flAirTime = 0;
IVP_FLOAT flSpeed = ( IVP_FLOAT )m_pCore->speed.real_length();
if (flSpeed < 11.0f)
{
//Msg("*** WEAK JUMP at %f!!!\n", flSpeed);
m_bWeakJump = true;
}
else
{
//Msg("Strong JUMP at %f\n", flSpeed);
}
}
else
{
m_flAirTime += pEventSim->delta_time;
}
}
else
{
m_bAirborne = false;
m_bWeakJump = false;
}
}
//-----------------------------------------------------------------------------
// Purpose:
//-----------------------------------------------------------------------------
bool CPhysics_Airboat::PostRaycasts( IVP_Ray_Solver_Template *pRaySolverTemplates, const IVP_U_Matrix *matWorldFromCore,
IVP_Raycast_Airboat_Impact *pImpacts )
{
bool bReturn = true;
int nPontoonPoints = n_wheels;
for( int iPoint = 0; iPoint < nPontoonPoints; ++iPoint )
{
// Get data at raycast position.
IVP_Raycast_Airboat_Wheel *pPontoonPoint = get_wheel( IVP_POS_WHEEL( iPoint ) );
IVP_Raycast_Airboat_Impact *pImpact = &pImpacts[iPoint];
IVP_Ray_Solver_Template *pRaySolver = &pRaySolverTemplates[iPoint];
if ( !pPontoonPoint || !pImpact || !pRaySolver )
continue;
// Copy the ray length back, it may have changed.
pPontoonPoint->raycast_length = pRaySolver->ray_length;
// Test for inverted raycast direction.
if ( pImpact->bInWater )
{
pImpact->raycast_dir_ws.set_multiple( &pImpact->raycast_dir_ws, -1 );
}
// Impact.
if ( pImpact->bImpact )
{
// Save impact distance.
IVP_U_Point vecDelta;
vecDelta.subtract( &pImpact->vecImpactPointWS, &pRaySolver->ray_start_point );
pPontoonPoint->raycast_dist = vecDelta.real_length();
// Get the inverse portion of the surface normal in the direction of the ray cast (shock - used in the shock simulation code for the sign
// and percentage of force applied to the shock).
pImpact->inv_normal_dot_dir = 1.1f / ( IVP_Inline_Math::fabsd( pImpact->raycast_dir_ws.dot_product( &pImpact->vecImpactNormalWS ) ) + 0.1f );
// Set the wheel friction - ground friction (if any) + wheel friction.
pImpact->friction_value = pImpact->flFriction * pPontoonPoint->friction_of_wheel;
}
// No impact.
else
{
pPontoonPoint->raycast_dist = pPontoonPoint->raycast_length;
pImpact->inv_normal_dot_dir = 1.0f;
pImpact->moveable_object_hit_by_ray = NULL;
pImpact->vecImpactNormalWS.set_multiple( &pImpact->raycast_dir_ws, -1 );
pImpact->friction_value = 1.0f;
}
// Set the new wheel position (the impact point or the full ray distance). Make this from the wheel not the ray trace position.
pImpact->vecImpactPointWS.add_multiple( &pRaySolver->ray_start_point, &pImpact->raycast_dir_ws, pPontoonPoint->raycast_dist );
// Get the speed (velocity) at the impact point.
m_pCore->get_surface_speed_ws( &pImpact->vecImpactPointWS, &pImpact->surface_speed_wheel_ws );
pImpact->projected_surface_speed_wheel_ws.set_orthogonal_part( &pImpact->surface_speed_wheel_ws, &pImpact->vecImpactNormalWS );
matWorldFromCore->vmult3( &pPontoonPoint->axis_direction_cs, &pImpact->axis_direction_ws );
pImpact->projected_axis_direction_ws.set_orthogonal_part( &pImpact->axis_direction_ws, &pImpact->vecImpactNormalWS );
if ( pImpact->projected_axis_direction_ws.normize() == IVP_FAULT )
{
DevMsg( "CPhysics_Airboat::do_simulation_controller projected_axis_direction_ws.normize failed\n" );
bReturn = false;
}
}
return bReturn;
}
//-----------------------------------------------------------------------------
// Purpose:
//-----------------------------------------------------------------------------
void CPhysics_Airboat::DoSimulationPontoons( IVP_Raycast_Airboat_Impact *pImpacts, IVP_Event_Sim *pEventSim )
{
int nPontoonPoints = n_wheels;
for ( int iPoint = 0; iPoint < nPontoonPoints; ++iPoint )
{
IVP_Raycast_Airboat_Wheel *pPontoonPoint = get_wheel( IVP_POS_WHEEL( iPoint ) );
if ( !pPontoonPoint )
continue;
if ( pImpacts[iPoint].bImpact )
{
DoSimulationPontoonsGround( pPontoonPoint, &pImpacts[iPoint], pEventSim );
}
else if ( pImpacts[iPoint].bInWater )
{
DoSimulationPontoonsWater( pPontoonPoint, &pImpacts[iPoint], pEventSim );
}
}
}
//-----------------------------------------------------------------------------
// Purpose: Handle pontoons on ground.
//-----------------------------------------------------------------------------
void CPhysics_Airboat::DoSimulationPontoonsGround( IVP_Raycast_Airboat_Wheel *pPontoonPoint,
IVP_Raycast_Airboat_Impact *pImpact, IVP_Event_Sim *pEventSim )
{
// Check to see if we hit anything, otherwise the no force on this point.
IVP_DOUBLE flDiff = pPontoonPoint->raycast_dist - pPontoonPoint->raycast_length;
if ( flDiff >= 0 )
return;
IVP_FLOAT flSpringConstant, flSpringRelax, flSpringCompress;
flSpringConstant = pPontoonPoint->spring_constant;
flSpringRelax = pPontoonPoint->spring_damp_relax;
flSpringCompress = pPontoonPoint->spring_damp_compress;
IVP_DOUBLE flForce = -flDiff * flSpringConstant;
IVP_FLOAT flInvNormalDotDir = clamp(pImpact->inv_normal_dot_dir, 0.0f, 3.0f);
flForce *= flInvNormalDotDir;
IVP_U_Float_Point vecSpeedDelta;
vecSpeedDelta.subtract( &pImpact->projected_surface_speed_wheel_ws, &pImpact->surface_speed_wheel_ws );
IVP_DOUBLE flSpeed = vecSpeedDelta.dot_product( &pImpact->raycast_dir_ws );
if ( flSpeed > 0 )
{
flForce -= flSpringRelax * flSpeed;
}
else
{
flForce -= flSpringCompress * flSpeed;
}
if ( flForce < 0 )
{
flForce = 0.0f;
}
// NOTE: Spring constants are all mass-independent, so no need to multiply by mass here.
IVP_DOUBLE flImpulse = flForce * pEventSim->delta_time;
IVP_U_Float_Point vecImpulseWS;
vecImpulseWS.set_multiple( &pImpact->vecImpactNormalWS, flImpulse );
m_pCore->push_core_ws( &pImpact->vecImpactPointWS, &vecImpulseWS );
}
//-----------------------------------------------------------------------------
// Purpose: Handle pontoons on water.
//-----------------------------------------------------------------------------
void CPhysics_Airboat::DoSimulationPontoonsWater( IVP_Raycast_Airboat_Wheel *pPontoonPoint,
IVP_Raycast_Airboat_Impact *pImpact, IVP_Event_Sim *pEventSim )
{
#define AIRBOAT_BUOYANCY_SCALAR 1.6f
#define PONTOON_AREA_2D 2.8f // 2 pontoons x 16 in x 136 in = 4352 sq inches = 2.8 sq meters
#define PONTOON_HEIGHT 0.41f // 16 inches high = 0.41 meters
float flDepth = clamp( pImpact->flDepth, 0.f, PONTOON_HEIGHT );
//Msg("depth: %f\n", pImpact->flDepth);
// Depth is in inches, so multiply by 0.0254 meters/inch
IVP_FLOAT flSubmergedVolume = PONTOON_AREA_2D * flDepth * 0.0254;
// Buoyancy forces are equal to the mass of the water displaced, which is 1000 kg/m^3
// There are 4 pontoon points, so each one can exert 1/4th of the total buoyancy force.
IVP_FLOAT flForce = AIRBOAT_BUOYANCY_SCALAR * 0.25f * m_pCore->get_mass() * flSubmergedVolume * 1000.0f;
IVP_DOUBLE flImpulse = flForce * pEventSim->delta_time;
IVP_U_Float_Point vecImpulseWS;
vecImpulseWS.set( 0, -1, 0 );
vecImpulseWS.mult( flImpulse );
m_pCore->push_core_ws( &pImpact->vecImpactPointWS, &vecImpulseWS );
// Vector vecPoint;
// Vector vecDir(0, 0, 1);
//
// ConvertPositionToHL( pImpact->vecImpactPointWS, vecPoint );
// CPhysicsEnvironment *pEnv = (CPhysicsEnvironment *)m_pAirboatBody->get_core()->environment->client_data;
// IVPhysicsDebugOverlay *debugoverlay = pEnv->GetDebugOverlay();
// debugoverlay->AddLineOverlay(vecPoint, vecPoint + vecDir * 128, 255, 0, 255, false, 10.0 );
}
//-----------------------------------------------------------------------------
// Purpose:
//-----------------------------------------------------------------------------
void CPhysics_Airboat::PerformFrictionNotification( float flEliminatedEnergy, float dt, int nSurfaceProp, IPhysicsCollisionData *pCollisionData )
{
CPhysicsObject *pPhysAirboat = static_cast<CPhysicsObject*>( m_pAirboatBody->client_data );
if ( ( pPhysAirboat->CallbackFlags() & CALLBACK_GLOBAL_FRICTION ) == 0 )
return;
IPhysicsCollisionEvent *pEventHandler = pPhysAirboat->GetVPhysicsEnvironment()->GetCollisionEventHandler();
if ( !pEventHandler )
return;
// scrape with an estimate for the energy per unit mass
// This assumes that the game is interested in some measure of vibration
// for sound effects. This also assumes that more massive objects require
// more energy to vibrate.
flEliminatedEnergy *= dt / pPhysAirboat->GetMass();
if ( flEliminatedEnergy > 0.05f )
{
pEventHandler->Friction( pPhysAirboat, flEliminatedEnergy, pPhysAirboat->GetMaterialIndexInternal(), nSurfaceProp, pCollisionData );
}
}
//-----------------------------------------------------------------------------
// Purpose: Drag due to water and ground friction.
//-----------------------------------------------------------------------------
void CPhysics_Airboat::DoSimulationDrag( IVP_Raycast_Airboat_Impact *pImpacts,
IVP_Event_Sim *pEventSim )
{
const IVP_U_Matrix *matWorldFromCore = m_pCore->get_m_world_f_core_PSI();
IVP_FLOAT flSpeed = ( IVP_FLOAT )m_pCore->speed.real_length();
// Used to make airboat sliding sounds
CAirboatFrictionData frictionData;
ConvertDirectionToHL( m_pCore->speed, frictionData.m_vecVelocity );
// Count the pontoons in the water.
int nPontoonPoints = n_wheels;
int nPointsInWater = 0;
int nPointsOnGround = 0;
float flGroundFriction = 0;
float flAverageDampening = 0.0f;
int *pSurfacePropCount = (int *)stackalloc( n_wheels * sizeof(int) );
int *pSurfaceProp = (int *)stackalloc( n_wheels * sizeof(int) );
memset( pSurfacePropCount, 0, n_wheels * sizeof(int) );
memset( pSurfaceProp, 0xFF, n_wheels * sizeof(int) );
int nSurfacePropCount = 0;
int nMaxSurfacePropIdx = 0;
for( int iPoint = 0; iPoint < nPontoonPoints; ++iPoint )
{
// Get data at raycast position.
IVP_Raycast_Airboat_Impact *pImpact = &pImpacts[iPoint];
if ( !pImpact || !pImpact->bImpact )
continue;
if ( pImpact->bImpactWater )
{
flAverageDampening += pImpact->flDampening;
nPointsInWater++;
}
else
{
flGroundFriction += pImpact->flFriction;
nPointsOnGround++;
// This logic is used to determine which surface prop we hit the most.
int i;
for ( i = 0; i < nSurfacePropCount; ++i )
{
if ( pSurfaceProp[i] == pImpact->nSurfaceProps )
break;
}
if ( i == nSurfacePropCount )
{
++nSurfacePropCount;
}
pSurfaceProp[i] = pImpact->nSurfaceProps;
if ( ++pSurfacePropCount[i] > pSurfacePropCount[nMaxSurfacePropIdx] )
{
nMaxSurfacePropIdx = i;
}
Vector frictionPoint, frictionNormal;
ConvertPositionToHL( pImpact->vecImpactPointWS, frictionPoint );
ConvertDirectionToHL( pImpact->vecImpactNormalWS, frictionNormal );
frictionData.m_vecPoint += frictionPoint;
frictionData.m_vecNormal += frictionNormal;
}
}
int nSurfaceProp = pSurfaceProp[nMaxSurfacePropIdx];
if ( nPointsOnGround > 0 )
{
frictionData.m_vecPoint /= nPointsOnGround;
frictionData.m_vecNormal /= nPointsOnGround;
VectorNormalize( frictionData.m_vecNormal );
}
if ( nPointsInWater > 0 )
{
flAverageDampening /= nPointsInWater;
}
//IVP_FLOAT flDebugSpeed = ( IVP_FLOAT )m_pCore->speed.real_length();
//Msg("(water=%d/land=%d) speed=%f (%f %f %f)\n", nPointsInWater, nPointsOnGround, flDebugSpeed, vecAirboatDirLS.k[0], vecAirboatDirLS.k[1], vecAirboatDirLS.k[2]);
if ( nPointsInWater )
{
// Apply the drag force opposite to the direction of motion in local space.
IVP_U_Float_Point vecAirboatNegDirLS;
vecAirboatNegDirLS.set_negative( &m_vecLocalVelocity );
// Water drag is directional -- the pontoons resist left/right motion much more than forward/back.
IVP_U_Float_Point vecDragLS;
vecDragLS.set( AIRBOAT_WATER_DRAG_LEFT_RIGHT * vecAirboatNegDirLS.k[0],
AIRBOAT_WATER_DRAG_UP_DOWN * vecAirboatNegDirLS.k[1],
AIRBOAT_WATER_DRAG_FORWARD_BACK * vecAirboatNegDirLS.k[2] );
vecDragLS.mult( flSpeed * m_pCore->get_mass() * pEventSim->delta_time );
// dvs TODO: apply flAverageDampening here
// Convert the drag force to world space and apply the drag.
IVP_U_Float_Point vecDragWS;
matWorldFromCore->vmult3(&vecDragLS, &vecDragWS);
m_pCore->center_push_core_multiple_ws( &vecDragWS );
}
//
// Calculate ground friction drag:
//
if ( nPointsOnGround && ( flSpeed > 0 ))
{
// Calculate the average friction across all contact points.
flGroundFriction /= (float)nPointsOnGround;
// Apply the drag force opposite to the direction of motion.
IVP_U_Float_Point vecAirboatNegDir;
vecAirboatNegDir.set_negative( &m_pCore->speed );
IVP_FLOAT flFrictionDrag = m_pCore->get_mass() * AIRBOAT_GRAVITY * AIRBOAT_DRY_FRICTION_SCALE * flGroundFriction;
flFrictionDrag /= flSpeed;
IPhysicsObject *pPhysAirboat = static_cast<IPhysicsObject*>( m_pAirboatBody->client_data );
float flEliminatedEnergy = pPhysAirboat->GetEnergy();
// Apply the drag force opposite to the direction of motion in local space.
IVP_U_Float_Point vecAirboatNegDirLS;
vecAirboatNegDirLS.set_negative( &m_vecLocalVelocity );
// Ground drag is directional -- the pontoons resist left/right motion much more than forward/back.
IVP_U_Float_Point vecDragLS;
vecDragLS.set( AIRBOAT_GROUND_DRAG_LEFT_RIGHT * vecAirboatNegDirLS.k[0],
AIRBOAT_GROUND_DRAG_UP_DOWN * vecAirboatNegDirLS.k[1],
AIRBOAT_GROUND_DRAG_FORWARD_BACK * vecAirboatNegDirLS.k[2] );
vecDragLS.mult( flFrictionDrag * pEventSim->delta_time );
// dvs TODO: apply flAverageDampening here
// Convert the drag force to world space and apply the drag.
IVP_U_Float_Point vecDragWS;
matWorldFromCore->vmult3(&vecDragLS, &vecDragWS);
m_pCore->center_push_core_multiple_ws( &vecDragWS );
// Figure out how much energy was eliminated by friction.
flEliminatedEnergy -= pPhysAirboat->GetEnergy();
PerformFrictionNotification( flEliminatedEnergy, pEventSim->delta_time, nSurfaceProp, &frictionData );
}
}
//-----------------------------------------------------------------------------
// Purpose:
//-----------------------------------------------------------------------------
void CPhysics_Airboat::DoSimulationTurbine( IVP_Event_Sim *pEventSim )
{
// Reduce the turbine power during weak jumps to avoid unrealistic air control.
// Also, reduce reverse thrust while airborne.
float flThrust = m_flThrust;
if ((m_bWeakJump) || (m_bAirborne && (flThrust < 0)))
{
flThrust *= 0.5;
}
// Get the forward vector in world-space.
IVP_U_Float_Point vecForwardWS;
const IVP_U_Matrix *matWorldFromCore = m_pCore->get_m_world_f_core_PSI();
matWorldFromCore->get_col( IVP_COORDINATE_INDEX( index_z ), &vecForwardWS );
//Msg("thrust: %f\n", m_flThrust);
if ( ( vecForwardWS.k[1] < -0.5 ) && ( flThrust > 0 ) )
{
// Driving up a slope. Reduce upward thrust to prevent ludicrous climbing of steep surfaces.
float flFactor = 1 + vecForwardWS.k[1];
//Msg("FWD: y=%f, factor=%f\n", vecForwardWS.k[1], flFactor);
flThrust *= flFactor;
}
else if ( ( vecForwardWS.k[1] > 0.5 ) && ( flThrust < 0 ) )
{
// Reversing up a slope. Reduce upward thrust to prevent ludicrous climbing of steep surfaces.
float flFactor = 1 - vecForwardWS.k[1];
//Msg("REV: y=%f, factor=%f\n", vecForwardWS.k[1], flFactor);
flThrust *= flFactor;
}
// Forward (Front/Back) force
IVP_U_Float_Point vecImpulse;
vecImpulse.set_multiple( &vecForwardWS, flThrust * m_pCore->get_mass() * pEventSim->delta_time );
m_pCore->center_push_core_multiple_ws( &vecImpulse );
}
//-----------------------------------------------------------------------------
// Purpose:
//-----------------------------------------------------------------------------
void CPhysics_Airboat::DoSimulationSteering( IVP_Event_Sim *pEventSim )
{
// Calculate the steering direction: forward or reverse.
// Don't mess with the steering direction while we're steering, unless thrust is applied.
// This prevents the steering from reversing because we started drifting backwards.
if ( ( m_SteeringAngle == 0 ) || ( m_flThrust != 0 ) )
{
if ( !m_bAnalogSteering )
{
// If we're applying reverse thrust, steering is always reversed.
if ( m_flThrust < 0 )
{
m_bSteeringReversed = true;
}
// Else if we are applying forward thrust or moving forward, use forward steering.
else if ( ( m_flThrust > 0 ) || ( m_vecLocalVelocity.k[2] > 0 ) )
{
m_bSteeringReversed = false;
}
}
else
{
// Create a dead zone through the middle of the joystick where we don't reverse thrust.
// If we're applying reverse thrust, steering is always reversed.
if ( m_flThrust < -2.0f )
{
m_bSteeringReversed = true;
}
// Else if we are applying forward thrust or moving forward, use forward steering.
else if ( ( m_flThrust > 2.0f ) || ( m_vecLocalVelocity.k[2] > 0 ) )
{
m_bSteeringReversed = false;
}
}
}
// Calculate the steering force.
IVP_FLOAT flForceSteering = 0.0f;
if ( fabsf( m_SteeringAngle ) > 0.01 )
{
// Get the sign of the steering force.
IVP_FLOAT flSteeringSign = m_SteeringAngle < 0.0f ? -1.0f : 1.0f;
if ( m_bSteeringReversed )
{
flSteeringSign *= -1.0f;
}
// If we changed steering sign or went from not steering to steering, reset the steer time
// to blend the new steering force in over time.
IVP_FLOAT flPrevSteeringSign = m_flPrevSteeringAngle < 0.0f ? -1.0f : 1.0f;
if ( ( fabs( m_flPrevSteeringAngle ) < 0.01 ) || ( flSteeringSign != flPrevSteeringSign ) )
{
m_flSteerTime = 0;
}
float flSteerScale = 0.f;
if ( !m_bAnalogSteering )
{
// Ramp the steering force up over two seconds.
flSteerScale = RemapValClamped( m_flSteerTime, 0, AIRBOAT_STEERING_INTERVAL, AIRBOAT_STEERING_RATE_MIN, AIRBOAT_STEERING_RATE_MAX );
}
else // consoles
{
// Analog steering
flSteerScale = RemapValClamped( fabs(m_SteeringAngle), 0, AIRBOAT_STEERING_INTERVAL, AIRBOAT_STEERING_RATE_MIN, AIRBOAT_STEERING_RATE_MAX );
}
flForceSteering = flSteerScale * m_pCore->get_mass() * pEventSim->i_delta_time;
flForceSteering *= -flSteeringSign;
m_flSteerTime += pEventSim->delta_time;
}
//Msg("steer force=%f\n", flForceSteering);
m_flPrevSteeringAngle = m_SteeringAngle * ( m_bSteeringReversed ? -1.0 : 1.0 );
// Get the sign of the drag forces.
IVP_FLOAT flRotSpeedSign = m_pCore->rot_speed.k[1] < 0.0f ? -1.0f : 1.0f;
// Apply drag proportional to the square of the angular velocity.
IVP_FLOAT flRotationalDrag = AIRBOAT_ROT_DRAG * m_pCore->rot_speed.k[1] * m_pCore->rot_speed.k[1] * m_pCore->get_mass() * pEventSim->i_delta_time;
flRotationalDrag *= flRotSpeedSign;
// Apply dampening proportional to angular velocity.
IVP_FLOAT flRotationalDamping = AIRBOAT_ROT_DAMPING * fabs(m_pCore->rot_speed.k[1]) * m_pCore->get_mass() * pEventSim->i_delta_time;
flRotationalDamping *= flRotSpeedSign;
// Calculate the net rotational force.
IVP_FLOAT flForceRotational = flForceSteering + flRotationalDrag + flRotationalDamping;
// Apply it.
IVP_U_Float_Point vecRotImpulse;
vecRotImpulse.set( 0, -1, 0 );
vecRotImpulse.mult( flForceRotational );
m_pCore->rot_push_core_cs( &vecRotImpulse );
}
//-----------------------------------------------------------------------------
// Purpose: Adds extra gravity unless we are performing a strong jump.
//-----------------------------------------------------------------------------
void CPhysics_Airboat::DoSimulationGravity( IVP_Event_Sim *pEventSim )
{
return;
if ( !m_bAirborne || m_bWeakJump )
{
IVP_U_Float_Point vecGravity;
vecGravity.set( 0, AIRBOAT_GRAVITY / 2.0f, 0 );
vecGravity.mult( m_pCore->get_mass() * pEventSim->delta_time );
m_pCore->center_push_core_multiple_ws( &vecGravity );
}
}
//-----------------------------------------------------------------------------
// Purpose: Returns the number of pontoon raycast points that were found to contact
// the ground or water.
//-----------------------------------------------------------------------------
int CPhysics_Airboat::CountSurfaceContactPoints( IVP_Raycast_Airboat_Impact *pImpacts )
{
int nContacts = 0;
int nPontoonPoints = n_wheels;
for ( int iPoint = 0; iPoint < nPontoonPoints; iPoint++ )
{
// Get data at raycast position.
IVP_Raycast_Airboat_Impact *pImpact = &pImpacts[iPoint];
if ( !pImpact )
continue;
if ( pImpact->bImpact )
{
nContacts++;
}
}
return nContacts;
}
//-----------------------------------------------------------------------------
// Purpose: Prevents us from nosing down dramatically during jumps, which
// increases our maximum jump distance.
//-----------------------------------------------------------------------------
void CPhysics_Airboat::DoSimulationKeepUprightPitch( IVP_Raycast_Airboat_Impact *pImpacts, IVP_Event_Sim *pEventSim )
{
// Disable pitch control during weak jumps. This reduces the unreal 'floaty' sensation.
if (m_bWeakJump)
{
return;
}
// Reference vector in core space.
// Pitch back by 10 degrees while airborne.
IVP_U_Float_Point vecUpCS;
vecUpCS.set( 0, -cos(DEG2RAD(10)), sin(DEG2RAD(10)));
// Calculate the goal vector in core space. We will try to align the reference
// vector with the goal vector.
IVP_U_Float_Point vecGoalAxisWS;
vecGoalAxisWS.set( 0, -1, 0 );
const IVP_U_Matrix *matWorldFromCore = m_pCore->get_m_world_f_core_PSI();
IVP_U_Float_Point vecGoalAxisCS;
matWorldFromCore->vimult3( &vecGoalAxisWS, &vecGoalAxisCS );
// Eliminate roll control
vecGoalAxisCS.k[0] = vecUpCS.k[0];
vecGoalAxisCS.normize();
// Get an axis to rotate around.
IVP_U_Float_Point vecRotAxisCS;
vecRotAxisCS.calc_cross_product( &vecUpCS, &vecGoalAxisCS );
// Get the amount that we need to rotate.
// atan2() is well defined, so do a Dot & Cross instead of asin(Cross)
IVP_FLOAT cosine = vecUpCS.dot_product( &vecGoalAxisCS );
IVP_FLOAT sine = vecRotAxisCS.real_length_plus_normize();
IVP_FLOAT angle = atan2( sine, cosine );
//Msg("angle: %.2f, axis: (%.2f %.2f %.2f)\n", RAD2DEG(angle), vecRotAxisCS.k[0], vecRotAxisCS.k[1], vecRotAxisCS.k[2]);
// Don't keep upright if any pontoons are contacting a surface.
if ( CountSurfaceContactPoints( pImpacts ) > 0 )
{
m_flPitchErrorPrev = angle;
return;
}
// Don't do any correction if we're within 15 degrees of the goal orientation.
//if ( fabs( angle ) < DEG2RAD( 15 ) )
//{
// m_flPitchErrorPrev = angle;
// return;
//}
//Msg("CORRECTING\n");
// Generate an angular impulse describing the rotation.
IVP_U_Float_Point vecAngularImpulse;
vecAngularImpulse.set_multiple( &vecRotAxisCS, m_pCore->get_mass() * ( 0.1f * angle + 0.04f * pEventSim->i_delta_time * ( angle - m_flPitchErrorPrev ) ) );
// Save the last error value for calculating the derivative.
m_flPitchErrorPrev = angle;
// Clamp the impulse at a maximum length.
IVP_FLOAT len = vecAngularImpulse.real_length_plus_normize();
if ( len > ( DEG2RAD( 1.5 ) * m_pCore->get_mass() ) )
{
len = DEG2RAD( 1.5 ) * m_pCore->get_mass();
}
vecAngularImpulse.mult( len );
// Apply the rotation.
m_pCore->rot_push_core_cs( &vecAngularImpulse );
#if DRAW_AIRBOAT_KEEP_UPRIGHT_PITCH_VECTORS
CPhysicsEnvironment *pEnv = (CPhysicsEnvironment *)m_pAirboatBody->get_core()->environment->client_data;
IVPhysicsDebugOverlay *debugoverlay = pEnv->GetDebugOverlay();
IVP_U_Float_Point vecPosIVP = m_pCore->get_position_PSI();
Vector vecPosHL;
ConvertPositionToHL(vecPosIVP, vecPosHL);
Vector vecGoalAxisHL;
ConvertDirectionToHL(vecGoalAxisWS, vecGoalAxisHL);
IVP_U_Float_Point vecUpWS;
matWorldFromCore->vmult3( &vecUpCS, &vecUpWS );
Vector vecCurHL;
ConvertDirectionToHL(vecUpWS, vecCurHL);
static IVP_FLOAT flLastLen = 0;
IVP_FLOAT flDebugLen = vecAngularImpulse.real_length();
if ( flLastLen && ( fabs( flDebugLen - flLastLen ) > DEG2RAD( 1 ) * m_pCore->get_mass() ) )
{
debugoverlay->AddLineOverlay(vecPosHL, vecPosHL + Vector(0, 0, 10) * flDebugLen, 255, 0, 255, false, 100.0 );
}
else
{
debugoverlay->AddLineOverlay(vecPosHL, vecPosHL + Vector(0, 0, 10) * flDebugLen, 255, 255, 255, false, 100.0 );
}
debugoverlay->AddLineOverlay(vecPosHL + Vector(0, 0, 10) * flDebugLen, vecPosHL + Vector(0, 0, 10) * flDebugLen + vecGoalAxisHL * 10, 0, 255, 0, false, 100.0 );
debugoverlay->AddLineOverlay(vecPosHL + Vector(0, 0, 10) * flDebugLen, vecPosHL + Vector(0, 0, 10) * flDebugLen + vecCurHL * 10, 255, 0, 0, false, 100.0 );
flLastLen = flDebugLen;
#endif
}
//-----------------------------------------------------------------------------
// Purpose: Roll stabilizer when airborne.
//-----------------------------------------------------------------------------
void CPhysics_Airboat::DoSimulationKeepUprightRoll( IVP_Raycast_Airboat_Impact *pImpacts, IVP_Event_Sim *pEventSim )
{
// Reference vector in core space.
// Pitch back by 10 degrees while airborne.
IVP_U_Float_Point vecUpCS;
vecUpCS.set( 0, -cos(DEG2RAD(10)), sin(DEG2RAD(10)));
// Calculate the goal vector in core space. We will try to align the reference
// vector with the goal vector.
IVP_U_Float_Point vecGoalAxisWS;
vecGoalAxisWS.set( 0, -1, 0 );
const IVP_U_Matrix *matWorldFromCore = m_pCore->get_m_world_f_core_PSI();
IVP_U_Float_Point vecGoalAxisCS;
matWorldFromCore->vimult3( &vecGoalAxisWS, &vecGoalAxisCS );
// Eliminate pitch control
vecGoalAxisCS.k[1] = vecUpCS.k[1];
vecGoalAxisCS.normize();
// Get an axis to rotate around.
IVP_U_Float_Point vecRotAxisCS;
vecRotAxisCS.calc_cross_product( &vecUpCS, &vecGoalAxisCS );
// Get the amount that we need to rotate.
// atan2() is well defined, so do a Dot & Cross instead of asin(Cross)
IVP_FLOAT cosine = vecUpCS.dot_product( &vecGoalAxisCS );
IVP_FLOAT sine = vecRotAxisCS.real_length_plus_normize();
IVP_FLOAT angle = atan2( sine, cosine );
//Msg("angle: %.2f, axis: (%.2f %.2f %.2f)\n", RAD2DEG(angle), vecRotAxisCS.k[0], vecRotAxisCS.k[1], vecRotAxisCS.k[2]);
// Don't keep upright if any pontoons are contacting a surface.
if ( CountSurfaceContactPoints( pImpacts ) > 0 )
{
m_flRollErrorPrev = angle;
return;
}
// Don't do any correction if we're within 10 degrees of the goal orientation.
if ( fabs( angle ) < DEG2RAD( 10 ) )
{
m_flRollErrorPrev = angle;
return;
}
//Msg("CORRECTING\n");
// Generate an angular impulse describing the rotation.
IVP_U_Float_Point vecAngularImpulse;
vecAngularImpulse.set_multiple( &vecRotAxisCS, m_pCore->get_mass() * ( 0.2f * angle + 0.3f * pEventSim->i_delta_time * ( angle - m_flRollErrorPrev ) ) );
// Save the last error value for calculating the derivative.
m_flRollErrorPrev = angle;
// Clamp the impulse at a maximum length.
IVP_FLOAT len = vecAngularImpulse.real_length_plus_normize();
if ( len > ( DEG2RAD( 2 ) * m_pCore->get_mass() ) )
{
len = DEG2RAD( 2 ) * m_pCore->get_mass();
}
vecAngularImpulse.mult( len );
m_pCore->rot_push_core_cs( &vecAngularImpulse );
// Debugging visualization.
#if DRAW_AIRBOAT_KEEP_UPRIGHT_ROLL_VECTORS
CPhysicsEnvironment *pEnv = (CPhysicsEnvironment *)m_pAirboatBody->get_core()->environment->client_data;
IVPhysicsDebugOverlay *debugoverlay = pEnv->GetDebugOverlay();
IVP_U_Float_Point vecPosIVP = m_pCore->get_position_PSI();
Vector vecPosHL;
ConvertPositionToHL(vecPosIVP, vecPosHL);
Vector vecGoalAxisHL;
ConvertDirectionToHL(vecGoalAxisWS, vecGoalAxisHL);
IVP_U_Float_Point vecUpWS;
matWorldFromCore->vmult3( &vecUpCS, &vecUpWS );
Vector vecCurHL;
ConvertDirectionToHL(vecUpWS, vecCurHL);
static IVP_FLOAT flLastLen = 0;
IVP_FLOAT flDebugLen = vecAngularImpulse.real_length();
if ( flLastLen && ( fabs( flDebugLen - flLastLen ) > ( DEG2RAD( 0.25 ) * m_pCore->get_mass() ) )
{
debugoverlay->AddLineOverlay(vecPosHL, vecPosHL + Vector(0, 0, 10) * flDebugLen, 255, 0, 255, false, 100.0 );
}
else
{
debugoverlay->AddLineOverlay(vecPosHL, vecPosHL + Vector(0, 0, 10) * flDebugLen, 255, 255, 255, false, 100.0 );
}
debugoverlay->AddLineOverlay(vecPosHL + Vector(0, 0, 10) * flDebugLen, vecPosHL + Vector(0, 0, 10) * flDebugLen + vecGoalAxisHL * 10, 0, 255, 0, false, 100.0 );
debugoverlay->AddLineOverlay(vecPosHL + Vector(0, 0, 10) * flDebugLen, vecPosHL + Vector(0, 0, 10) * flDebugLen + vecCurHL * 10, 255, 0, 0, false, 100.0 );
flLastLen = flDebugLen;
#endif
}
//-----------------------------------------------------------------------------
// Purpose:
// Input : wheel_nr -
// s_angle -
//-----------------------------------------------------------------------------
void CPhysics_Airboat::do_steering_wheel(IVP_POS_WHEEL wheel_nr, IVP_FLOAT s_angle)
{
IVP_Raycast_Airboat_Wheel *wheel = get_wheel(wheel_nr);
wheel->axis_direction_cs.set_to_zero();
wheel->axis_direction_cs.k[ index_x ] = 1.0f;
wheel->axis_direction_cs.rotate( IVP_COORDINATE_INDEX(index_y), s_angle);
}
//-----------------------------------------------------------------------------
// Purpose:
// Input : pos -
// spring_constant -
//-----------------------------------------------------------------------------
void CPhysics_Airboat::change_spring_constant(IVP_POS_WHEEL pos, IVP_FLOAT spring_constant)
{
IVP_Raycast_Airboat_Wheel *wheel = get_wheel(pos);
wheel->spring_constant = spring_constant;
}
//-----------------------------------------------------------------------------
// Purpose:
// Input : pos -
// spring_dampening -
//-----------------------------------------------------------------------------
void CPhysics_Airboat::change_spring_dampening(IVP_POS_WHEEL pos, IVP_FLOAT spring_dampening)
{
IVP_Raycast_Airboat_Wheel *wheel = get_wheel(pos);
wheel->spring_damp_relax = spring_dampening;
}
//-----------------------------------------------------------------------------
// Purpose:
// Input : pos -
// spring_dampening -
//-----------------------------------------------------------------------------
void CPhysics_Airboat::change_spring_dampening_compression(IVP_POS_WHEEL pos, IVP_FLOAT spring_dampening)
{
IVP_Raycast_Airboat_Wheel *wheel = get_wheel(pos);
wheel->spring_damp_compress = spring_dampening;
}
//-----------------------------------------------------------------------------
// Purpose:
// Input : pos -
// pre_tension_length -
//-----------------------------------------------------------------------------
void CPhysics_Airboat::change_spring_pre_tension(IVP_POS_WHEEL pos, IVP_FLOAT pre_tension_length)
{
IVP_Raycast_Airboat_Wheel *wheel = get_wheel(pos);
wheel->spring_len = gravity_y_direction * (wheel->distance_orig_hp_to_hp - pre_tension_length);
}
//-----------------------------------------------------------------------------
// Purpose:
// Input : pos -
// spring_length -
//-----------------------------------------------------------------------------
void CPhysics_Airboat::change_spring_length(IVP_POS_WHEEL pos, IVP_FLOAT spring_length)
{
IVP_Raycast_Airboat_Wheel *wheel = get_wheel(pos);
wheel->spring_len = spring_length;
}
//-----------------------------------------------------------------------------
// Purpose:
// Input : pos -
// torque -
//-----------------------------------------------------------------------------
void CPhysics_Airboat::change_wheel_torque(IVP_POS_WHEEL pos, IVP_FLOAT torque)
{
IVP_Raycast_Airboat_Wheel *wheel = get_wheel(pos);
wheel->torque = torque;
// Wake the physics object if need be!
m_pAirboatBody->get_environment()->get_controller_manager()->ensure_controller_in_simulation( this );
}
IVP_FLOAT CPhysics_Airboat::get_wheel_torque(IVP_POS_WHEEL pos)
{
return get_wheel(pos)->torque;
}
//-----------------------------------------------------------------------------
// Purpose:
// Throttle input is -1 to 1.
//-----------------------------------------------------------------------------
void CPhysics_Airboat::update_throttle( IVP_FLOAT flThrottle )
{
// Forward
if ( fabs( flThrottle ) < 0.01f )
{
m_flThrust = 0.0f;
}
else if ( flThrottle > 0.0f )
{
m_flThrust = AIRBOAT_THRUST_MAX * flThrottle;
}
else if ( flThrottle < 0.0f )
{
m_flThrust = AIRBOAT_THRUST_MAX_REVERSE * flThrottle;
}
}
//-----------------------------------------------------------------------------
// Purpose:
// Input : pos -
// stop_wheel -
//-----------------------------------------------------------------------------
void CPhysics_Airboat::fix_wheel(IVP_POS_WHEEL pos, IVP_BOOL stop_wheel)
{
IVP_Raycast_Airboat_Wheel *wheel = get_wheel(pos);
wheel->wheel_is_fixed = stop_wheel;
}
//-----------------------------------------------------------------------------
// Purpose:
// Input : pos -
// friction -
//-----------------------------------------------------------------------------
void CPhysics_Airboat::change_friction_of_wheel( IVP_POS_WHEEL pos, IVP_FLOAT friction )
{
IVP_Raycast_Airboat_Wheel *wheel = get_wheel(pos);
wheel->friction_of_wheel = friction;
}
//-----------------------------------------------------------------------------
// Purpose:
// Input : pos -
// stabi_constant -
//-----------------------------------------------------------------------------
void CPhysics_Airboat::change_stabilizer_constant(IVP_POS_AXIS pos, IVP_FLOAT stabi_constant)
{
IVP_Raycast_Airboat_Axle *pAxle = get_axle( pos );
pAxle->stabilizer_constant = stabi_constant;
}
//-----------------------------------------------------------------------------
// Purpose:
// Input : fast_turn_factor_ -
//-----------------------------------------------------------------------------
void CPhysics_Airboat::change_fast_turn_factor( IVP_FLOAT fast_turn_factor_ )
{
//fast_turn_factor = fast_turn_factor_;
}
//-----------------------------------------------------------------------------
// Purpose:
// Input : force -
//-----------------------------------------------------------------------------
void CPhysics_Airboat::change_body_downforce(IVP_FLOAT force)
{
down_force = force;
}
//-----------------------------------------------------------------------------
// Purpose:
// Output : IVP_CONTROLLER_PRIORITY
//-----------------------------------------------------------------------------
IVP_CONTROLLER_PRIORITY CPhysics_Airboat::get_controller_priority()
{
return IVP_CP_CONSTRAINTS_MAX;
}
//-----------------------------------------------------------------------------
// Purpose:
// Input : steering_angle_in -
//-----------------------------------------------------------------------------
void CPhysics_Airboat::do_steering( IVP_FLOAT steering_angle_in, bool bAnalog )
{
// Check for a change.
if ( m_SteeringAngle == steering_angle_in)
return;
MEM_ALLOC_CREDIT();
// Set the new steering angle.
m_bAnalogSteering = bAnalog;
m_SteeringAngle = steering_angle_in;
// Make sure the simulation is awake - we just go input.
m_pAirboatBody->get_environment()->get_controller_manager()->ensure_controller_in_simulation( this );
// Steer each wheel.
for ( int iWheel = 0; iWheel < wheels_per_axis; ++iWheel )
{
do_steering_wheel( IVP_POS_WHEEL( iWheel ), m_SteeringAngle );
}
}
//-----------------------------------------------------------------------------
// Purpose:
// Input : pos -
// Output : IVP_DOUBLE
//-----------------------------------------------------------------------------
IVP_DOUBLE CPhysics_Airboat::get_wheel_angular_velocity(IVP_POS_WHEEL pos)
{
IVP_Raycast_Airboat_Wheel *wheel = get_wheel(pos);
return wheel->wheel_angular_velocity;
}
//-----------------------------------------------------------------------------
// Purpose:
// Input : index -
// Output : IVP_DOUBLE
//-----------------------------------------------------------------------------
IVP_DOUBLE CPhysics_Airboat::get_body_speed(IVP_COORDINATE_INDEX index)
{
// return (IVP_FLOAT)car_body->get_geom_center_speed();
IVP_U_Float_Point *vec_ws = &m_pAirboatBody->get_core()->speed;
// works well as we do not use merged cores
const IVP_U_Matrix *mat_ws = m_pAirboatBody->get_core()->get_m_world_f_core_PSI();
IVP_U_Point orientation;
mat_ws->get_col(index, &orientation);
return orientation.dot_product(vec_ws);
};
//-----------------------------------------------------------------------------
// Purpose:
//-----------------------------------------------------------------------------
IVP_DOUBLE CPhysics_Airboat::get_orig_front_wheel_distance()
{
IVP_U_Float_Point *left_wheel_cs = &this->get_wheel(IVP_FRONT_LEFT)->hp_cs;
IVP_U_Float_Point *right_wheel_cs = &this->get_wheel(IVP_FRONT_RIGHT)->hp_cs;
IVP_DOUBLE dist = left_wheel_cs->k[this->index_x] - right_wheel_cs->k[this->index_x];
return IVP_Inline_Math::fabsd(dist); // was fabs, which was a sml call
}
//-----------------------------------------------------------------------------
// Purpose:
//-----------------------------------------------------------------------------
IVP_DOUBLE CPhysics_Airboat::get_orig_axles_distance()
{
IVP_U_Float_Point *front_wheel_cs = &this->get_wheel(IVP_FRONT_LEFT)->hp_cs;
IVP_U_Float_Point *rear_wheel_cs = &this->get_wheel(IVP_REAR_LEFT)->hp_cs;
IVP_DOUBLE dist = front_wheel_cs->k[this->index_z] - rear_wheel_cs->k[this->index_z];
return IVP_Inline_Math::fabsd(dist); // was fabs, which was a sml call
}
//-----------------------------------------------------------------------------
// Purpose:
// Input : *array_of_skid_info_out -
//-----------------------------------------------------------------------------
void CPhysics_Airboat::get_skid_info( IVP_Wheel_Skid_Info *array_of_skid_info_out)
{
for ( int w = 0; w < n_wheels; w++)
{
IVP_Wheel_Skid_Info &info = array_of_skid_info_out[w];
//IVP_Constraint_Car_Object *wheel = car_constraint_solver->wheel_objects.element_at(w);
info.last_contact_position_ws.set_to_zero(); // = wheel->last_contact_position_ws;
info.last_skid_value = 0.0f; // wheel->last_skid_value;
info.last_skid_time = 0.0f; //wheel->last_skid_time;
}
}
//-----------------------------------------------------------------------------
// Purpose:
//-----------------------------------------------------------------------------
void CPhysics_Airboat::InitRaycastCarEnvironment( IVP_Environment *pEnvironment,
const IVP_Template_Car_System *pCarSystemTemplate )
{
// Copies of the car system template component indices and handedness.
index_x = pCarSystemTemplate->index_x;
index_y = pCarSystemTemplate->index_y;
index_z = pCarSystemTemplate->index_z;
is_left_handed = pCarSystemTemplate->is_left_handed;
IVP_Standard_Gravity_Controller *pGravityController = new IVP_Standard_Gravity_Controller();
IVP_U_Point vecGravity( 0.0f, AIRBOAT_GRAVITY, 0.0f );
pGravityController->grav_vec.set( &vecGravity );
BEGIN_IVP_ALLOCATION();
m_pAirboatBody->get_core()->add_core_controller( pGravityController );
// Add this controller to the physics environment and setup the objects gravity.
pEnvironment->get_controller_manager()->announce_controller_to_environment( this );
END_IVP_ALLOCATION();
extra_gravity = pCarSystemTemplate->extra_gravity_force_value;
// This works because gravity is still int the same direction, just smaller.
if ( pEnvironment->get_gravity()->k[index_y] > 0 )
{
gravity_y_direction = 1.0f;
}
else
{
gravity_y_direction = -1.0f;
}
normized_gravity_ws.set( pEnvironment->get_gravity() );
normized_gravity_ws.normize();
}
//-----------------------------------------------------------------------------
// Purpose:
//-----------------------------------------------------------------------------
void CPhysics_Airboat::InitRaycastCarBody( const IVP_Template_Car_System *pCarSystemTemplate )
{
// Car body attributes.
n_wheels = pCarSystemTemplate->n_wheels;
n_axis = pCarSystemTemplate->n_axis;
wheels_per_axis = n_wheels / n_axis;
// Add the car body "core" to the list of raycast car controller "cores."
m_pAirboatBody = pCarSystemTemplate->car_body;
this->vector_of_cores.add( m_pAirboatBody->get_core() );
// Init extra downward force applied to car.
down_force_vertical_offset = pCarSystemTemplate->body_down_force_vertical_offset;
down_force = 0.0f;
// Initialize.
for ( int iAxis = 0; iAxis < 3; ++iAxis )
{
m_pAirboatBody->get_core()->rot_speed.k[iAxis] = 0.0f;
m_pAirboatBody->get_core()->speed.k[iAxis] = 0.0f;
}
}
//-----------------------------------------------------------------------------
// Purpose:
//-----------------------------------------------------------------------------
void CPhysics_Airboat::InitRaycastCarWheels( const IVP_Template_Car_System *pCarSystemTemplate )
{
IVP_U_Matrix m_core_f_object;
m_pAirboatBody->calc_m_core_f_object( &m_core_f_object );
// Initialize the car wheel system.
for ( int iWheel = 0; iWheel < n_wheels; iWheel++ )
{
// Get and clear out memory for the current raycast wheel.
IVP_Raycast_Airboat_Wheel *pRaycastWheel = get_wheel( IVP_POS_WHEEL( iWheel ) );
P_MEM_CLEAR( pRaycastWheel );
// Put the wheel in car space.
m_core_f_object.vmult4( &pCarSystemTemplate->wheel_pos_Bos[iWheel], &pRaycastWheel->hp_cs );
m_core_f_object.vmult4( &pCarSystemTemplate->trace_pos_Bos[iWheel], &pRaycastWheel->raycast_start_cs );
// Add in the raycast start offset.
pRaycastWheel->raycast_length = AIRBOAT_RAYCAST_DIST;
pRaycastWheel->raycast_dir_cs.set_to_zero();
pRaycastWheel->raycast_dir_cs.k[index_y] = gravity_y_direction;
// Spring (Shocks) data.
pRaycastWheel->spring_len = -pCarSystemTemplate->spring_pre_tension[iWheel];
pRaycastWheel->spring_direction_cs.set_to_zero();
pRaycastWheel->spring_direction_cs.k[index_y] = gravity_y_direction;
pRaycastWheel->spring_constant = pCarSystemTemplate->spring_constant[iWheel];
pRaycastWheel->spring_damp_relax = pCarSystemTemplate->spring_dampening[iWheel];
pRaycastWheel->spring_damp_compress = pCarSystemTemplate->spring_dampening_compression[iWheel];
// Wheel data.
pRaycastWheel->friction_of_wheel = 1.0f;//pCarSystemTemplate->friction_of_wheel[iWheel];
pRaycastWheel->wheel_radius = pCarSystemTemplate->wheel_radius[iWheel];
pRaycastWheel->inv_wheel_radius = 1.0f / pCarSystemTemplate->wheel_radius[iWheel];
do_steering_wheel( IVP_POS_WHEEL( iWheel ), 0.0f );
pRaycastWheel->wheel_is_fixed = IVP_FALSE;
pRaycastWheel->max_rotation_speed = pCarSystemTemplate->wheel_max_rotation_speed[iWheel>>1];
pRaycastWheel->wheel_is_fixed = IVP_TRUE;
}
}
//-----------------------------------------------------------------------------
// Purpose:
//-----------------------------------------------------------------------------
void CPhysics_Airboat::InitRaycastCarAxes( const IVP_Template_Car_System *pCarSystemTemplate )
{
m_SteeringAngle = -1.0f; // make sure next call is not optimized
this->do_steering( 0.0f, false ); // make sure next call gets through
for ( int iAxis = 0; iAxis < n_axis; iAxis++ )
{
IVP_Raycast_Airboat_Axle *pAxle = get_axle( IVP_POS_AXIS( iAxis ) );
pAxle->stabilizer_constant = pCarSystemTemplate->stabilizer_constant[iAxis];
}
}
//-----------------------------------------------------------------------------
// Purpose: Debug data for use in vphysics and the engine to visualize car data.
//-----------------------------------------------------------------------------
void CPhysics_Airboat::SetCarSystemDebugData( const IVP_CarSystemDebugData_t &carSystemDebugData )
{
// Wheels (raycast data only!)
for ( int iWheel = 0; iWheel < IVP_RAYCAST_AIRBOAT_MAX_WHEELS; ++iWheel )
{
m_CarSystemDebugData.wheelRaycasts[iWheel][0] = carSystemDebugData.wheelRaycasts[iWheel][0];
m_CarSystemDebugData.wheelRaycasts[iWheel][1] = carSystemDebugData.wheelRaycasts[iWheel][1];
m_CarSystemDebugData.wheelRaycastImpacts[iWheel] = carSystemDebugData.wheelRaycastImpacts[iWheel];
}
}
//-----------------------------------------------------------------------------
// Purpose: Debug data for use in vphysics and the engine to visualize car data.
//-----------------------------------------------------------------------------
void CPhysics_Airboat::GetCarSystemDebugData( IVP_CarSystemDebugData_t &carSystemDebugData )
{
// Wheels (raycast data only!)
for ( int iWheel = 0; iWheel < IVP_RAYCAST_AIRBOAT_MAX_WHEELS; ++iWheel )
{
carSystemDebugData.wheelRaycasts[iWheel][0] = m_CarSystemDebugData.wheelRaycasts[iWheel][0];
carSystemDebugData.wheelRaycasts[iWheel][1] = m_CarSystemDebugData.wheelRaycasts[iWheel][1];
carSystemDebugData.wheelRaycastImpacts[iWheel] = m_CarSystemDebugData.wheelRaycastImpacts[iWheel];
}
}
//-----------------------------------------------------------------------------
// Purpose:
// Output : IVP_U_Vector<IVP_Core>
//-----------------------------------------------------------------------------
IVP_U_Vector<IVP_Core> *CPhysics_Airboat::get_associated_controlled_cores( void )
{
return &vector_of_cores;
}
//-----------------------------------------------------------------------------
// Purpose:
// Input : *core -
//-----------------------------------------------------------------------------
void CPhysics_Airboat::core_is_going_to_be_deleted_event( IVP_Core *core )
{
P_DELETE_THIS(this);
}
//-----------------------------------------------------------------------------
// Purpose:
// Input : i -
// Output : IVP_Raycast_Airboat_Axle
//-----------------------------------------------------------------------------
IVP_Raycast_Airboat_Axle *CPhysics_Airboat::get_axle( IVP_POS_AXIS i )
{
return &m_aAirboatAxles[i];
}
//-----------------------------------------------------------------------------
// Purpose:
// Input : i -
// Output : IVP_Raycast_Airboat_Wheel
//-----------------------------------------------------------------------------
IVP_Raycast_Airboat_Wheel *CPhysics_Airboat::get_wheel( IVP_POS_WHEEL i )
{
return &m_aAirboatWheels[i];
}
//-----------------------------------------------------------------------------
// Purpose:
//-----------------------------------------------------------------------------
IVP_Controller_Raycast_Airboat_Vector_of_Cores_1::IVP_Controller_Raycast_Airboat_Vector_of_Cores_1():
IVP_U_Vector<IVP_Core>( &elem_buffer[0],1 )
{
}
|