Overview Package Class Source Class tree Glossary
previous class      next class frames      no frames

Vehicles.KCar


00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043
00044
00045
00046
00047
00048
00049
00050
00051
00052
00053
00054
00055
00056
00057
00058
00059
00060
00061
00062
00063
00064
00065
00066
00067
00068
00069
00070
00071
00072
00073
00074
00075
00076
00077
00078
00079
00080
00081
00082
00083
00084
00085
00086
00087
00088
00089
00090
00091
00092
00093
00094
00095
00096
00097
00098
00099
00100
00101
00102
00103
00104
00105
00106
00107
00108
00109
00110
00111
00112
00113
00114
00115
00116
00117
00118
00119
00120
00121
00122
00123
00124
00125
00126
00127
00128
00129
00130
00131
00132
00133
00134
00135
00136
00137
00138
00139
00140
00141
00142
00143
00144
00145
00146
00147
00148
00149
00150
00151
00152
00153
00154
00155
00156
00157
00158
00159
00160
00161
00162
00163
00164
00165
00166
00167
00168
00169
00170
00171
00172
00173
00174
00175
00176
00177
00178
00179
00180
00181
00182
00183
00184
00185
00186
00187
00188
00189
00190
00191
00192
00193
00194
00195
00196
00197
00198
00199
00200
00201
00202
00203
00204
00205
00206
00207
00208
00209
00210
00211
00212
00213
00214
00215
00216
00217
00218
00219
00220
00221
00222
00223
00224
00225
00226
00227
00228
00229
00230
00231
00232
00233
00234
00235
00236
00237
00238
00239
00240
00241
00242
00243
00244
00245
00246
00247
00248
00249
00250
00251
00252
00253
00254
00255
00256
00257
00258
00259
00260
00261
00262
00263
00264
00265
00266
00267
00268
00269
00270
00271
00272
00273
00274
00275
00276
00277
00278
00279
00280
00281
00282
00283
00284
00285
00286
00287
00288
00289
00290
00291
00292
00293
00294
00295
00296
00297
00298
00299
00300
00301
00302
00303
00304
00305
00306
00307
00308
00309
00310
00311
00312
00313
00314
00315
00316
00317
00318
00319
00320
00321
00322
00323
00324
00325
00326
00327
00328
00329
00330
00331
00332
00333
00334
00335
00336
00337
00338
00339
00340
00341
00342
00343
00344
00345
00346
00347
00348
00349
00350
00351
00352
00353
00354
00355
00356
00357
00358
00359
00360
00361
00362
00363
00364
00365
00366
00367
00368
00369
00370
00371
00372
00373
00374
00375
00376
00377
00378
00379
00380
00381
00382
00383
00384
00385
00386
00387
00388
00389
00390
00391
00392
00393
00394
00395
00396
00397
00398
00399
00400
00401
00402
00403
00404
00405
00406
00407
00408
00409
00410
00411
00412
00413
00414
00415
00416
00417
00418
00419
00420
00421
00422
00423
00424
00425
00426
00427
00428
00429
00430
00431
00432
00433
00434
00435
00436
00437
00438
00439
00440
00441
00442
00443
00444
00445
00446
00447
00448
00449
00450
00451
00452
00453
00454
00455
00456
00457
00458
00459
00460
00461
00462
00463
00464
00465
00466
00467
00468
00469
00470
00471
00472
00473
00474
00475
00476
00477
00478
00479
00480
00481
00482
00483
00484
00485
00486
00487
00488
00489
00490
00491
00492
00493
00494
00495
00496
00497
00498
00499
00500
00501
00502
00503
00504
00505
00506
00507
00508
00509
00510
00511
00512
00513
00514
00515
00516
00517
00518
00519
00520
00521
00522
00523
00524
00525
00526
00527
00528
00529
00530
00531
00532
00533
00534
00535
00536
00537
00538
00539
00540
00541
00542
00543
00544
00545
00546
00547
00548
00549
00550
00551
00552
00553
00554
00555
00556
00557
00558
00559
00560
00561
00562
00563
00564
00565
00566
00567
00568
00569
00570
00571
00572
00573
00574
00575
00576
00577
00578
00579
00580
00581
00582
00583
00584
00585
00586
00587
00588
00589
00590
00591
00592
00593
00594
00595
00596
00597
00598
00599
00600
00601
00602
00603
00604
00605
00606
00607
00608
00609
00610
00611
00612
00613
00614
00615
00616
00617
00618
00619
00620
00621
00622
00623
00624
00625
00626
00627
00628
00629
00630
00631
00632
00633
00634
00635
00636
00637
00638
00639
00640
00641
00642
00643
00644
00645
00646
00647
00648
00649
00650
00651
00652
00653
00654
00655
00656
00657
00658
00659
00660
00661
00662
00663
00664
00665
00666
00667
00668
00669
00670
00671
00672
00673
00674
00675
00676
00677
00678
00679
00680
00681
00682
00683
00684
00685
00686
00687
00688
00689
00690
00691
00692
00693
00694
00695
00696
00697
00698
00699
00700
00701
00702
00703
00704
00705
00706
00707
00708
00709
00710
00711
00712
00713
00714
00715
00716
00717
00718
00719
00720
00721
00722
00723
00724
00725
00726
00727
00728
00729
00730
00731
00732
00733
00734
00735
00736
00737
00738
00739
00740
00741
00742
00743
00744
00745
00746
00747
00748
00749
00750
00751
00752
00753
00754
00755
00756
00757
00758
00759
00760
00761
00762
00763
00764
00765
00766
00767
00768
00769
00770
00771
00772
00773
00774
00775
00776
00777
00778
00779
00780
00781
00782
00783
00784
00785
00786
00787
00788
00789
00790
00791
00792
00793
00794
00795
00796
00797
00798
00799
00800
00801
00802
00803
00804
00805
00806
00807
00808
00809
00810
00811
00812
00813
00814
00815
00816
00817
00818
00819
00820
00821
00822
00823
00824
00825
00826
00827
00828
00829
00830
00831
00832
00833
00834
00835
00836
00837
00838
00839
00840
00841
00842
00843
00844
00845
00846
00847
00848
00849
00850
00851
00852
00853
00854
00855
00856
00857
00858
00859
00860
00861
00862
00863
00864
00865
00866
00867
00868
00869
00870
00871
00872
00873
00874
00875
00876
00877
00878
00879
00880
00881
00882
00883
00884
00885
00886
00887
00888
00889
00890
00891
00892
00893
00894
00895
00896
00897
00898
00899
00900
00901
00902
00903
00904
00905
00906
00907
00908
00909
00910
00911
00912
00913
00914
00915
00916
00917
// Base class for 4-wheeled vehicles using Karma
// Assumes negative-X is forward, negative-Y is right
class KCar extends KVehicle
    abstract;

var KTire frontLeft, frontRight, rearLeft, rearRight;
var (KCar) class<KTire> FrontTireClass;
var (KCar) class<KTire> RearTireClass;

// Wheel positions
var const float WheelFrontAlong;
var const float WheelFrontAcross;
var const float WheelRearAlong;
var const float WheelRearAcross;
var const float WheelVert;

var (KCar) float       MaxSteerAngle;   // (65535 = 360 deg)
var (KCar) float       MaxBrakeTorque;  // Braking torque applied to all four wheels. Positive only.
var (KCar) float       TorqueSplit;     // front/rear drive torque split. 1 is fully RWD, 0 is fully FWD. 0.5 is standard 4WD.


// KCarWheelJoint setting for steering (see KCarWheelJoint). Duplicated here for handiness.
var (KCar) float       SteerPropGap;
var (KCar) float       SteerTorque;
var (KCar) float       SteerSpeed;

// KCarWheelSuspension setting
var (KCar) float       SuspStiffness;
var (KCar) float       SuspDamping;
var (KCar) float       SuspHighLimit;
var (KCar) float       SuspLowLimit;
var (KCar) float       SuspRef;

// KTire settings. Duplicated here for handy tuning.
var (KCar) float       TireRollFriction;
var (KCar) float       TireLateralFriction;
var (KCar) float       TireRollSlip;
var (KCar) float       TireLateralSlip;
var (KCar) float       TireMinSlip;
var (KCar) float       TireSlipRate;
var (KCar) float       TireSoftness;
var (KCar) float       TireAdhesion;
var (KCar) float       TireRestitution;
var (KCar) float       TireMass;

var (KCar) float	   HandbrakeThresh; // speed above which handbrake comes on =]
var (KCar) float	   TireHandbrakeSlip; // Additional lateral slip when handbrake engaged
var (KCar) float	   TireHandbrakeFriction; // Additional lateral friction when handbrake engaged

var (KCar) float       ChassisMass;

var (KCar) float	   StopThreshold; // Forward velocity under which brakes become drive.

var (KCar) InterpCurve	TorqueCurve; // Engine RPM in, Torque out.

var (KCar) float		FlipTorque;
var (KCar) float		FlipTime;

var (KCar) float		MaxNetUpdateInterval;

var int      Gear;  // 1 is forward, -1 is backward. Currently symmetric power/torque curve

// Car output
var float              WheelSpinSpeed;  // Current (averaged) RPM of rear wheels
var float			   ForwardVel;		// Component of cars velocity in its forward direction.
var bool			   bIsInverted;     // Updated in Tick - indicates if car is not upright.

// Internal
var bool			   IsDriving;
var float			   FlipTimeLeft;
var float			   NextNetUpdateTime;	// Next time we should force an update of vehicles state.

// Low-level drive data (this is replicated)
var bool			   OutputBrake;
var float			   OutputTorque;
var bool			   OutputHandbrakeOn;


// Networking
struct KCarState
{
	var KRBVec				ChassisPosition;
	var Quat				ChassisQuaternion;
	var KRBVec				ChassisLinVel;
	var KRBVec				ChassisAngVel;

	var float				WheelHeight[4]; // FL, FR, RL, RR
	var float				FrontWheelAng[2]; // FL, FR

	var float				WheelVertVel[4];
	//var float				WheelSpinVel[4];

	var float				ServerSteering;

	var float				ServerTorque;
	var bool				ServerBrake;
	var bool			    ServerHandbrakeOn;

	var bool				bNewState; // Set to true whenever a new state is received and should be processed
};

var KRigidBodyState		ChassisState;

var KCarState			CarState; // This is replicated to the car, and processed to update all the parts.
var bool				bNewCarState; // Indicated there is new data processed, and chassis RBState should be updated.

replication
{
	// We replicate the Gear for brake-lights etc.
	unreliable if(Role == ROLE_Authority)
		CarState, Gear;

	reliable if(Role == ROLE_Authority)
		FlipTimeLeft;
}

// When new information is received, see if its new. If so, pass bits off the the wheels.
// Each part will then update its rigid body position via the KUpdateState event.
// JTODO: This is where clever unpacking would happen.
simulated event VehicleStateReceived()
{
	local vector ChassisY, SteerY, ChassisZ, calcPos, WheelY, lPos;
	local vector chassisPos, chassisLinVel, chassisAngVel, WheelLinVel, wPosRel;
	local Quat relQ, WheelQ;

	if(!CarState.bNewState)
		return;

	// Don't do anything if car isn't started up.	
	if(frontLeft == None || frontRight == None || rearLeft == None || rearRight == None)
		return;

	// Get root chassis info
	ChassisState.Position = CarState.ChassisPosition;
	ChassisState.Quaternion = CarState.ChassisQuaternion;
	ChassisState.LinVel = CarState.ChassisLinVel;
	ChassisState.AngVel = CarState.ChassisAngVel;

	chassisPos = KRBVecToVector(CarState.ChassisPosition);
	chassisLinVel = KRBVecToVector(CarState.ChassisLinVel);
	chassisAngVel = KRBVecToVector(CarState.ChassisAngVel);

	// Calc chassis state axes
	ChassisY = QuatRotateVector(CarState.ChassisQuaternion, vect(0, 1, 0));
	ChassisZ = QuatRotateVector(CarState.ChassisQuaternion, vect(0, 0, 1));

	// Get root chassis info
	ChassisState.Position = CarState.ChassisPosition;
	ChassisState.Quaternion = CarState.ChassisQuaternion;
	ChassisState.LinVel = CarState.ChassisLinVel;
	ChassisState.AngVel = CarState.ChassisAngVel;
	

	// Figure out new state of wheels

	// Wheel positions are only supplied with a chassis-space Z (vertical) value - X and Y are assumed no to change
	// Rear wheel orientations are not supplied. The only constraint is their Y-axis (axle) is parallel to 
	// the chassis Y-axis. A quaternion is calculated to go from current orientation to fulfil that criteria, which
	// should produce minimum difference to the 'roll' of the wheel - which is allowed to differ on server and client.
	// For front wheel we do send the current 'steering' angle. That is added after the above process as a quaternion 
	// around chassis Z (up).
	// For linear velocity of wheels - calculate based on linear and angular velocity of chassis, and add on vertical 
	// component sent over the net.

	////////////////////////// FRONT LEFT //////////////////////////
	frontLeft.KGetRigidBodyState(frontLeft.ReceiveState);

	// Position
	lPos.X = WheelFrontAlong;
	lPos.Y = WheelFrontAcross;
	lPos.Z = CarState.WheelHeight[0];
	calcPos = chassisPos + QuatRotateVector(CarState.ChassisQuaternion, lPos); // Convert from chassis state to world space
	frontLeft.ReceiveState.Position = KRBVecFromVector(calcPos);

	// Rotation
	wheelQ = frontLeft.KGetRBQuaternion();
	WheelY = QuatRotateVector(wheelQ, vect(0, 1, 0));
	SteerY = QuatRotateVector( QuatFromAxisAndAngle(ChassisZ, CarState.FrontWheelAng[0]), ChassisY );
	relQ = QuatFindBetween(WheelY, SteerY);
	frontLeft.ReceiveState.Quaternion = QuatProduct(relQ, wheelQ);

	// Velocity
	wPosRel = calcPos - chassisPos;
	WheelLinVel = chassisLinVel + (chassisAngVel Cross wPosRel);
	WheelLinVel += CarState.WheelVertVel[0] * ChassisZ;
	frontLeft.ReceiveState.LinVel = KRBVecFromVector(WheelLinVel);

	//frontLeft.ReceiveState.AngVel = KRBVecFromVector(chassisAngVel + (WheelY * CarState.WheelSpinVel[0]));

	frontLeft.bReceiveStateNew = true;

	////////////////////////// FRONT RIGHT //////////////////////////
	frontRight.KGetRigidBodyState(frontRight.ReceiveState);

	// Position
	lPos.X = WheelFrontAlong;
	lPos.Y = -WheelFrontAcross;
	lPos.Z = CarState.WheelHeight[1];
	calcPos = chassisPos + QuatRotateVector(CarState.ChassisQuaternion, lPos);
	frontRight.ReceiveState.Position = KRBVecFromVector(calcPos);


	// Rotation
	wheelQ = frontRight.KGetRBQuaternion();
	WheelY = QuatRotateVector(wheelQ, vect(0, 1, 0));
	SteerY = QuatRotateVector( QuatFromAxisAndAngle(ChassisZ, CarState.FrontWheelAng[1]), ChassisY );
	relQ = QuatFindBetween(WheelY, SteerY);
	frontRight.ReceiveState.Quaternion = QuatProduct(relQ, wheelQ);

	// Velocity
	wPosRel = calcPos - chassisPos;
	WheelLinVel = chassisLinVel + (chassisAngVel Cross wPosRel);
	WheelLinVel += CarState.WheelVertVel[1] * ChassisZ;
	frontRight.ReceiveState.LinVel = KRBVecFromVector(WheelLinVel);

	//frontRight.ReceiveState.AngVel = KRBVecFromVector(chassisAngVel + (WheelY * CarState.WheelSpinVel[1]));

	frontRight.bReceiveStateNew = true;

	////////////////////////// REAR LEFT //////////////////////////
	rearLeft.KGetRigidBodyState(rearLeft.ReceiveState);

	// Position
	lPos.X = WheelRearAlong;
	lPos.Y = WheelFrontAcross;
	lPos.Z = CarState.WheelHeight[2];
	calcPos = chassisPos + QuatRotateVector(CarState.ChassisQuaternion, lPos);
	rearLeft.ReceiveState.Position = KRBVecFromVector(calcPos);


	// Rotation
	wheelQ = rearLeft.KGetRBQuaternion();
	WheelY = QuatRotateVector(wheelQ, vect(0, 1, 0));
	relQ = QuatFindBetween(WheelY, ChassisY);
	rearLeft.ReceiveState.Quaternion = QuatProduct(relQ, wheelQ);
	
	// Velocity
	wPosRel = calcPos - chassisPos;
	WheelLinVel = chassisLinVel + (chassisAngVel Cross wPosRel);
	WheelLinVel += CarState.WheelVertVel[2] * ChassisZ;
	rearLeft.ReceiveState.LinVel = KRBVecFromVector(WheelLinVel);

	//rearLeft.ReceiveState.AngVel = KRBVecFromVector(chassisAngVel + (WheelY * CarState.WheelSpinVel[2]));

	rearLeft.bReceiveStateNew = true;

	////////////////////////// REAR RIGHT //////////////////////////
	rearRight.KGetRigidBodyState(rearRight.ReceiveState);

	// Position
	lPos.X = WheelRearAlong;
	lPos.Y = -WheelFrontAcross;
	lPos.Z = CarState.WheelHeight[3];
	calcPos = chassisPos + QuatRotateVector(CarState.ChassisQuaternion, lPos);
	rearRight.ReceiveState.Position = KRBVecFromVector(calcPos);

	// Rotation
	wheelQ = rearRight.KGetRBQuaternion();
	WheelY = QuatRotateVector(wheelQ, vect(0, 1, 0));
	relQ = QuatFindBetween(WheelY, ChassisY);
	rearRight.ReceiveState.Quaternion = QuatProduct(relQ, wheelQ);

	// Velocity
	wPosRel = calcPos - chassisPos;
	WheelLinVel = chassisLinVel + (chassisAngVel Cross wPosRel);
	WheelLinVel += CarState.WheelVertVel[3] * ChassisZ;
	rearRight.ReceiveState.LinVel = KRBVecFromVector(WheelLinVel);

	//rearRight.ReceiveState.AngVel = KRBVecFromVector(chassisAngVel + (WheelY * CarState.WheelSpinVel[3]));

	rearRight.bReceiveStateNew = true;

	////// OTHER //////

	// Update control inputs
	Steering = CarState.ServerSteering;
	OutputTorque = CarState.ServerTorque;
	OutputBrake = CarState.ServerBrake;
	OutputHandbrakeOn = CarState.ServerHandbrakeOn;

	// Update flags
	CarState.bNewState = false;
	bNewCarState = true;

	// For debugging...
	//KDrawRigidBodyState(ChassisState, false);
	//KDrawRigidBodyState(frontLeft.ReceiveState, false);
	//KDrawRigidBodyState(frontRight.ReceiveState, false);
	//KDrawRigidBodyState(rearLeft.ReceiveState, false);
	//KDrawRigidBodyState(rearRight.ReceiveState, false);
}

// This only update the chassis. The wheels update themselves.
simulated event bool KUpdateState(out KRigidBodyState newState)
{
	// This should never get called on the server - but just in case!
	if(Role == ROLE_Authority || !bNewCarState)
		return false;
		
	// Apply received data as new position of car chassis.
	newState = ChassisState;
	bNewCarState = false;

	return true;
	//return false;
}

// Pack current state of whole car into the state struct, to be sent to the client.
// Should only get called on the server.
function PackState()
{
	local vector lPos, wPos, chassisPos, chassisLinVel, chassisAngVel, wPosRel, WheelLinVel;
	local vector ChassisX, ChassisZ, WheelY, oldPos, oldLinVel;
	local KRigidBodyState CurrentChassisState, WheelState;

	// Get chassis state.
	KGetRigidBodyState(CurrentChassisState);

	chassisPos = KRBVecToVector(CurrentChassisState.Position);
	chassisLinVel = KRBVecToVector(CurrentChassisState.LinVel);
	chassisAngVel = KRBVecToVector(CurrentChassisState.AngVel);

	// Last position we sent
	oldPos = KRBVectoVector(CarState.ChassisPosition);
	oldLinVel = KRBVectoVector(CarState.ChassisLinVel);

	// See if state has changed enough, or enough time has passed, that we 
	// should send out another update by updating the state struct.
	if( !KIsAwake() )
	{
		return; // Never send updates if physics is at rest
	}

	if( VSize(oldPos - chassisPos) > 5 ||
		VSize(oldLinVel - chassisLinVel) > 1 ||
		Abs(CarState.ServerTorque - OutputTorque) > 0.1 ||
		Abs(CarState.ServerSteering - Steering) > 0.1 ||
		Level.TimeSeconds > NextNetUpdateTime )
	{
		NextNetUpdateTime = Level.TimeSeconds + MaxNetUpdateInterval;
	}
	else
	{
		return;
		//NextNetUpdateTime = Level.TimeSeconds + MaxNetUpdateInterval;
	}

	CarState.ChassisPosition = CurrentChassisState.Position;
	CarState.ChassisQuaternion = CurrentChassisState.Quaternion;
	CarState.ChassisLinVel = CurrentChassisState.LinVel;
	CarState.ChassisAngVel = CurrentChassisState.AngVel;


	ChassisX = QuatRotateVector(CarState.ChassisQuaternion, vect(1, 0, 0));
	ChassisZ = QuatRotateVector(CarState.ChassisQuaternion, vect(0, 0, 1));
	// Get each wheel state.

	////////////////////////// FRONT LEFT //////////////////////////
	frontLeft.KGetRigidBodyState(WheelState);
	wPos = KRBVecToVector(WheelState.Position);
	lPos = QuatRotateVector(QuatInvert(CarState.ChassisQuaternion), wPos - chassisPos); // Convert from world to chassis state space
	CarState.WheelHeight[0] = lPos.Z; // X should be WheelFrontAlong, Y should be WheelFrontAcross

	// For front wheels - we store their current angle around Z as well.
	WheelY = QuatRotateVector(WheelState.Quaternion, vect(0, 1, 0));
	CarState.FrontWheelAng[0] = -ASin(ChassisX Dot WheelY);

	// Find component of relative wheel linear velocity along suspension travel (chassisZ).
	wPosRel = KRBVecToVector(WheelState.Position) - chassisPos;
	WheelLinVel = chassisLinVel + (chassisAngVel Cross wPosRel);

	CarState.WheelVertVel[0] = 
		((WheelState.LinVel.X - WheelLinVel.X)* ChassisZ.X) + 
		((WheelState.LinVel.Y - WheelLinVel.Y)* ChassisZ.Y) + 
		((WheelState.LinVel.Z - WheelLinVel.Z)* ChassisZ.Z);

	//CarState.WheelSpinVel[0] = KRBVecToVector(WheelState.AngVel) Dot WheelY;

	////////////////////////// FRONT RIGHT //////////////////////////
	frontRight.KGetRigidBodyState(WheelState);
	wPos = KRBVecToVector(WheelState.Position);
	lPos = QuatRotateVector(QuatInvert(CarState.ChassisQuaternion), wPos - chassisPos);
	CarState.WheelHeight[1] = lPos.Z;

	WheelY = QuatRotateVector(WheelState.Quaternion, vect(0, 1, 0));
	CarState.FrontWheelAng[1] = -ASin(ChassisX Dot WheelY);

	CarState.WheelVertVel[1] = 
		((WheelState.LinVel.X - WheelLinVel.X)* ChassisZ.X) + 
		((WheelState.LinVel.Y - WheelLinVel.Y)* ChassisZ.Y) + 
		((WheelState.LinVel.Z - WheelLinVel.Z)* ChassisZ.Z);

	//CarState.WheelSpinVel[1] = KRBVecToVector(WheelState.AngVel) Dot WheelY;

	////////////////////////// REAR LEFT //////////////////////////
	rearLeft.KGetRigidBodyState(WheelState);
	wPos = KRBVecToVector(WheelState.Position);
	lPos = QuatRotateVector(QuatInvert(CarState.ChassisQuaternion), wPos - chassisPos);
	CarState.WheelHeight[2] = lPos.Z;

	wPosRel = KRBVecToVector(WheelState.Position) - chassisPos;
	WheelLinVel = chassisLinVel + (chassisAngVel Cross wPosRel);

	CarState.WheelVertVel[2] = 
		((WheelState.LinVel.X - WheelLinVel.X)* ChassisZ.X) + 
		((WheelState.LinVel.Y - WheelLinVel.Y)* ChassisZ.Y) + 
		((WheelState.LinVel.Z - WheelLinVel.Z)* ChassisZ.Z);

	WheelY = QuatRotateVector(WheelState.Quaternion, vect(0, 1, 0));
	//CarState.WheelSpinVel[2] = KRBVecToVector(WheelState.AngVel) Dot WheelY;

	////////////////////////// REAR RIGHT //////////////////////////
	rearRight.KGetRigidBodyState(WheelState);
	wPos = KRBVecToVector(WheelState.Position);
	lPos = QuatRotateVector(QuatInvert(CarState.ChassisQuaternion), wPos - chassisPos);
	CarState.WheelHeight[3] = lPos.Z;

	wPosRel = KRBVecToVector(WheelState.Position) - chassisPos;
	WheelLinVel = chassisLinVel + (chassisAngVel Cross wPosRel);

	CarState.WheelVertVel[3] = 
		((WheelState.LinVel.X - WheelLinVel.X)* ChassisZ.X) + 
		((WheelState.LinVel.Y - WheelLinVel.Y)* ChassisZ.Y) + 
		((WheelState.LinVel.Z - WheelLinVel.Z)* ChassisZ.Z);

	WheelY = QuatRotateVector(WheelState.Quaternion, vect(0, 1, 0));
	//CarState.WheelSpinVel[3] = KRBVecToVector(WheelState.AngVel) Dot WheelY;

	// OTHER
	CarState.ServerSteering = Steering;
	CarState.ServerTorque = OutputTorque;
	CarState.ServerBrake = OutputBrake;
	CarState.ServerHandbrakeOn = OutputHandbrakeOn;

	// This flag lets the client know this data is new.
	CarState.bNewState = true;
}

simulated function PostNetBeginPlay()
{
	local vector RotX, RotY, RotZ, lPos;

    Super.PostNetBeginPlay();

    // Set up suspension graphics

    GetAxes(Rotation,RotX,RotY,RotZ);

    // Spawn wheels, and flip graphics where necessary
    frontLeft = spawn(FrontTireClass, self,, Location + WheelFrontAlong*RotX + WheelFrontAcross*RotY + WheelVert*RotZ, Rotation);
    //frontLeft.SetDrawScale(1);
    frontLeft.SetDrawScale3D(vect(1, 1, 1));

    frontRight = spawn(FrontTireClass, self,, Location + WheelFrontAlong*RotX - WheelFrontAcross*RotY + WheelVert*RotZ, Rotation);
    frontRight.SetDrawScale3D(vect(1, -1, 1));

    rearLeft = spawn(RearTireClass, self,, Location + WheelRearAlong*RotX + WheelRearAcross*RotY + WheelVert*RotZ, Rotation);
    //rearLeft.SetDrawScale(1);
    rearLeft.SetDrawScale3D(vect(1, 1, 1));

    rearRight = spawn(RearTireClass, self,, Location + WheelRearAlong*RotX - WheelRearAcross*RotY + WheelVert*RotZ, Rotation);
    rearRight.SetDrawScale3D(vect(1, -1, 1));

    // Create joints
	lPos.X = WheelFrontAlong;
	lPos.Y = WheelFrontAcross;
	lPos.Z = WheelVert;
    frontLeft.WheelJoint = spawn(class'KCarWheelJoint', self);
    frontLeft.WheelJoint.KPos1 = lPos/50;
    frontLeft.WheelJoint.KPriAxis1 = vect(0, 0, 1);
    frontLeft.WheelJoint.KSecAxis1 = vect(0, 1, 0);
    frontLeft.WheelJoint.KConstraintActor1 = self;
    frontLeft.WheelJoint.KPos2 = vect(0, 0, 0);
    frontLeft.WheelJoint.KPriAxis2 = vect(0, 0, 1);
    frontLeft.WheelJoint.KSecAxis2 = vect(0, 1, 0);
    frontLeft.WheelJoint.KConstraintActor2 = frontLeft;
    frontLeft.WheelJoint.SetPhysics(PHYS_Karma);

	lPos.Y = -WheelFrontAcross;
    frontRight.WheelJoint = spawn(class'KCarWheelJoint', self);
    frontRight.WheelJoint.KPos1 = lPos/50;
    frontRight.WheelJoint.KPriAxis1 = vect(0, 0, 1);
    frontRight.WheelJoint.KSecAxis1 = vect(0, 1, 0);
    frontRight.WheelJoint.KConstraintActor1 = self;
    frontRight.WheelJoint.KPos2 = vect(0, 0, 0);
    frontRight.WheelJoint.KPriAxis2 = vect(0, 0, 1);
    frontRight.WheelJoint.KSecAxis2 = vect(0, 1, 0);
    frontRight.WheelJoint.KConstraintActor2 = frontRight;
    frontRight.WheelJoint.SetPhysics(PHYS_Karma);

	lPos.X = WheelRearAlong;
	lPos.Y = WheelRearAcross;
    rearLeft.WheelJoint = spawn(class'KCarWheelJoint', self);
    rearLeft.WheelJoint.KPos1 = lPos/50;
    rearLeft.WheelJoint.KPriAxis1 = vect(0, 0, 1);
    rearLeft.WheelJoint.KSecAxis1 = vect(0, 1, 0);
    rearLeft.WheelJoint.KConstraintActor1 = self;
    rearLeft.WheelJoint.KPos2 = vect(0, 0, 0);
    rearLeft.WheelJoint.KPriAxis2 = vect(0, 0, 1);
    rearLeft.WheelJoint.KSecAxis2 = vect(0, 1, 0);
    rearLeft.WheelJoint.KConstraintActor2 = rearLeft;
    rearLeft.WheelJoint.SetPhysics(PHYS_Karma);

    lPos.Y = -WheelRearAcross;
	rearRight.WheelJoint = spawn(class'KCarWheelJoint', self);
    rearRight.WheelJoint.KPos1 = lPos/50;
    rearRight.WheelJoint.KPriAxis1 = vect(0, 0, 1);
    rearRight.WheelJoint.KSecAxis1 = vect(0, 1, 0);
    rearRight.WheelJoint.KConstraintActor1 = self;
    rearRight.WheelJoint.KPos2 = vect(0, 0, 0);
    rearRight.WheelJoint.KPriAxis2 = vect(0, 0, 1);
    rearRight.WheelJoint.KSecAxis2 = vect(0, 1, 0);
    rearRight.WheelJoint.KConstraintActor2 = rearRight;
    rearRight.WheelJoint.SetPhysics(PHYS_Karma);

	// Initially make sure parameters are sync'ed with Karma
	KVehicleUpdateParams();
}

// Clean up wheels etc.
simulated event Destroyed()
{
	// Destroy joints holding wheels to car
	frontLeft.WheelJoint.Destroy();
	frontRight.WheelJoint.Destroy();
	rearLeft.WheelJoint.Destroy();
	rearRight.WheelJoint.Destroy();

	// Destroy wheels themselves.
	frontLeft.Destroy();
	frontRight.Destroy();
	rearLeft.Destroy();
	rearRight.Destroy();

	Super.Destroyed();
}

// Call this if you change any parameters (tire, suspension etc.) and they
// will be passed down to each wheel/joint.

simulated event KVehicleUpdateParams()
{
	Super.KVehicleUpdateParams();

    rearLeft.WheelJoint.bKSteeringLocked = true;
    rearRight.WheelJoint.bKSteeringLocked = true;
    
    frontLeft.WheelJoint.bKSteeringLocked = false;
    frontLeft.WheelJoint.KProportionalGap = SteerPropGap;
    frontLeft.WheelJoint.KMaxSteerTorque = SteerTorque;
    frontLeft.WheelJoint.KMaxSteerSpeed = SteerSpeed;

    frontRight.WheelJoint.bKSteeringLocked = false;
    frontRight.WheelJoint.KProportionalGap = SteerPropGap;
    frontRight.WheelJoint.KMaxSteerTorque = SteerTorque;
    frontRight.WheelJoint.KMaxSteerSpeed = SteerSpeed;

    frontLeft.WheelJoint.KSuspHighLimit = SuspHighLimit;
    frontLeft.WheelJoint.KSuspLowLimit = SuspLowLimit;
    frontLeft.WheelJoint.KSuspStiffness = SuspStiffness;
    frontLeft.WheelJoint.KSuspDamping = SuspDamping;

    frontRight.WheelJoint.KSuspHighLimit = SuspHighLimit;
    frontRight.WheelJoint.KSuspLowLimit = SuspLowLimit;
    frontRight.WheelJoint.KSuspStiffness = SuspStiffness;
    frontRight.WheelJoint.KSuspDamping = SuspDamping;

    rearLeft.WheelJoint.KSuspHighLimit = SuspHighLimit;
    rearLeft.WheelJoint.KSuspLowLimit = SuspLowLimit;
    rearLeft.WheelJoint.KSuspStiffness = SuspStiffness;
    rearLeft.WheelJoint.KSuspDamping = SuspDamping;

    rearRight.WheelJoint.KSuspHighLimit = SuspHighLimit;
    rearRight.WheelJoint.KSuspLowLimit = SuspLowLimit;
    rearRight.WheelJoint.KSuspStiffness = SuspStiffness;
    rearRight.WheelJoint.KSuspDamping = SuspDamping;

	// Sync params with Karma.
	frontLeft.WheelJoint.KUpdateConstraintParams();
	frontRight.WheelJoint.KUpdateConstraintParams();
	rearLeft.WheelJoint.KUpdateConstraintParams();
	rearRight.WheelJoint.KUpdateConstraintParams();

    // Mass
    KSetMass(ChassisMass);
    frontLeft.KSetMass(TireMass);
    frontRight.KSetMass(TireMass);
    rearLeft.KSetMass(TireMass);
    rearRight.KSetMass(TireMass);

    // Tire params handy tuning
    frontLeft.RollFriction = TireRollFriction;
    frontLeft.LateralFriction = TireLateralFriction;
    frontLeft.RollSlip = TireRollSlip;
    frontLeft.LateralSlip = TireLateralSlip;
    frontLeft.MinSlip = TireMinSlip;
    frontLeft.SlipRate = TireSlipRate;
    frontLeft.Softness = TireSoftness;
    frontLeft.Adhesion = TireAdhesion;
    frontLeft.Restitution = TireRestitution;

    frontRight.RollFriction = TireRollFriction;
    frontRight.LateralFriction = TireLateralFriction;
    frontRight.RollSlip = TireRollSlip;
    frontRight.LateralSlip = TireLateralSlip;
    frontRight.MinSlip = TireMinSlip;
    frontRight.SlipRate = TireSlipRate;
    frontRight.Softness = TireSoftness;
    frontRight.Adhesion = TireAdhesion;
    frontRight.Restitution = TireRestitution;

    rearLeft.RollFriction = TireRollFriction;
    rearLeft.LateralFriction = TireLateralFriction;
    rearLeft.RollSlip = TireRollSlip;
    rearLeft.LateralSlip = TireLateralSlip;
    rearLeft.MinSlip = TireMinSlip;
    rearLeft.SlipRate = TireSlipRate;
    rearLeft.Softness = TireSoftness;
    rearLeft.Adhesion = TireAdhesion;
    rearLeft.Restitution = TireRestitution;

    rearRight.RollFriction = TireRollFriction;
    rearRight.LateralFriction = TireLateralFriction;
    rearRight.RollSlip = TireRollSlip;
    rearRight.LateralSlip = TireLateralSlip;
    rearRight.MinSlip = TireMinSlip;
    rearRight.SlipRate = TireSlipRate;
    rearRight.Softness = TireSoftness;
    rearRight.Adhesion = TireAdhesion;
    rearRight.Restitution = TireRestitution;
}

// Possibly apply force to flip car over.
simulated event KApplyForce(out vector Force, out vector Torque)
{
	local float torqueScale;
	local vector worldForward, worldUp, worldRight, torqueAxis;

	if(FlipTimeLeft == 0)
		return;

	worldForward = vect(-1, 0, 0) >> Rotation;
	worldUp = vect(0, 0, 1) >> Rotation;
	worldRight = vect(0, 1, 0) >> Rotation;

	torqueAxis = Normal(worldUp Cross vect(0, 0, 1));

	// Torque scaled by how far over we are. 
	// This will be between 0 and PI - so convert to between 0 and 1.
	torqueScale = Acos(worldUp Dot vect(0, 0, 1))/3.1416;

	Torque = FlipTorque * torqueScale * torqueAxis;
}

function StartFlip(Pawn Pusher)
{
	//local vector toPusher, worldUp;

	// if we are already flipping the car - dont do it again!
	if(FlipTimeLeft > 0)
		return;

	// Dont let you push the car if you are going to be underneath it!
  	//worldUp = vect(0, 0, 1) >> Rotation;
	//toPusher = Pusher.Location - Location;
	//if( (worldUp Dot toPusher) < 0)
	//	return;

	FlipTimeLeft = FlipTime; // Start the flip on the server
}

// Tell it your current throttle, and it will give you an output torque
// This is currently like an electric motor
function float Engine(float Throttle)
{
    local float torque;
    
	torque = Abs(Throttle) * Gear * InterpCurveEval(TorqueCurve, WheelSpinSpeed);

	GraphData("SpinSpeed", WheelSpinSpeed);
	GraphData("Torque", torque);

    return -1 * torque;
}

function ProcessCarInput()
{
	local vector worldForward, worldUp;

	//Log("PCI S:"$Steering$" T:"$Throttle);

  	worldForward = vect(-1, 0, 0) >> Rotation;
  	worldUp = vect(0, 0, 1) >> Rotation;
  	  	
  	ForwardVel = Velocity Dot worldForward;

	bIsInverted = worldUp.Z < 0.2;

	// 'ForwardVel' isn't very helpful if we are inverted, so we just pretend its positive.
	if(bIsInverted)
		ForwardVel = 2 * StopThreshold;

	//Log("F:"$ForwardVel$"IsI:"$bIsInverted);

	if( Driver == None )
	{
		if(bAutoDrive == true)
		{
			Gear = 1;
			OutputBrake = false;

			Throttle = 0.4;
			Steering = 1;

			//log("Thr:"$Throttle);

			KWake();
		}
		else
		{
			Gear = 0;
			OutputBrake = true;
		}
	}
	else
	{
		if(Throttle > 0.01) // pressing forwards
		{
			if(ForwardVel < -StopThreshold && Gear != 1) // going backwards - so brake first
			{
				//Log("F - Brake");
				Gear = 0;
				OutputBrake = true;
				IsDriving = false;
			}
			else // stopped or going forwards, so drive
			{
				//Log("F - Drive");
				Gear = 1;
				OutputBrake = false;
				IsDriving = true;
			}
		}
		else if(Throttle < -0.01) // pressing backwards
		{
			// We have to release the brakes and then press reverse again to go into reverse
			if(ForwardVel < StopThreshold && IsDriving == false)
			{
				//Log("B - Drive");
				Gear = -1;
				OutputBrake = false;
				IsDriving = false;
			}
			else // otherwise, we are going forwards, or still holding brake, so just brake
			{
				//Log("B - Brake");
				Gear = 0;
				OutputBrake = true;
				IsDriving = true;
			}
		}
		else // not pressing either
		{
			// If stationary, stick brakes on
			if(Abs(ForwardVel) < StopThreshold)
			{
				//Log("B - Brake");
				Gear = 0;
				OutputBrake = true;
				IsDriving = false;
				OutputHandbrakeOn = false; // force handbrake off if stopped.
			}
			else // otherwise, coast
			{
				//Log("Coast");
				Gear = 0;
				OutputBrake = false;
				IsDriving = false;
			}
		}

		KWake(); // currently, never let the car go to sleep whilst being driven.
	}

	// If we are going forwards, steering, and pressing the brake,
	// enable extra-slippy handbrake.
	if((ForwardVel > HandbrakeThresh || OutputHandbrakeOn == true) && Abs(Steering) > 0.01 && OutputBrake == true)
		OutputHandbrakeOn = true;
	else
		OutputHandbrakeOn = false;

	// Engine model
    OutputTorque = Engine(Throttle);
}

// Car Simulation
simulated function Tick(float Delta)
{
	local float tana, sFactor;

	Super.Tick(Delta);

    WheelSpinSpeed = (rearLeft.SpinSpeed + rearRight.SpinSpeed)/2;
    //log("WheelSpinSpeed:"$WheelSpinSpeed);

	// if we are in the process of flipping the car, keep it enabled!
	if( FlipTimeLeft > 0  )
		KWake();

    // If the server, process input and pack updated car info into struct.
    if(Role == ROLE_Authority)
	{
		ProcessCarInput();
		PackState();
	}

    // Motor

	// FRONT
    frontLeft.WheelJoint.KMotorTorque = 0.5 * OutputTorque * (1-TorqueSplit);
    frontRight.WheelJoint.KMotorTorque = 0.5 * OutputTorque * (1-TorqueSplit);

	// REAR
    rearLeft.WheelJoint.KMotorTorque = 0.5 * OutputTorque * TorqueSplit;
    rearRight.WheelJoint.KMotorTorque = 0.5 * OutputTorque * TorqueSplit;

    // Braking

	if(OutputBrake)
	{
		frontLeft.WheelJoint.KBraking = MaxBrakeTorque;
		frontRight.WheelJoint.KBraking = MaxBrakeTorque;
		rearLeft.WheelJoint.KBraking = MaxBrakeTorque;
		rearRight.WheelJoint.KBraking = MaxBrakeTorque;
	}
	else
	{
		frontLeft.WheelJoint.KBraking = 0.0;
		frontRight.WheelJoint.KBraking = 0.0;
		rearLeft.WheelJoint.KBraking = 0.0;
		rearRight.WheelJoint.KBraking = 0.0;
	}

	// Steering

	tana = Tan(6.283/65536 * Steering * MaxSteerAngle);

    sFactor = 0.5 * tana * (2 * WheelFrontAcross) / Abs(WheelFrontAlong - WheelRearAlong);
    frontLeft.WheelJoint.KSteerAngle = 65536/6.283 * Atan(tana, (1-sFactor));
    frontRight.WheelJoint.KSteerAngle = 65536/6.283 * Atan(tana, (1+sFactor));

	// Handbrake

	if(OutputHandbrakeOn == true)
	{
		//Log("HANDBRAKE!!");
		rearLeft.LateralFriction = TireLateralFriction + TireHandbrakeFriction;
		rearLeft.LateralSlip = TireLateralSlip + TireHandbrakeSlip;

		rearRight.LateralFriction = TireLateralFriction + TireHandbrakeFriction;
		rearRight.LateralSlip = TireLateralSlip + TireHandbrakeSlip;
	}
	else
	{
		rearLeft.LateralFriction = TireLateralFriction;
		rearLeft.LateralSlip = TireLateralSlip;

		rearRight.LateralFriction = TireLateralFriction;
		rearRight.LateralSlip = TireLateralSlip;
	}

	// Flipping
	if(FlipTimeLeft > 0)
	{
		FlipTimeLeft -= Delta;
		FlipTimeLeft = FMax(FlipTimeLeft, 0.0); // Make sure it doesn't go negative
	}

}

defaultproperties
{
     WheelFrontAlong=-180.000000
     WheelFrontAcross=140.000000
     WheelRearAlong=160.000000
     WheelRearAcross=140.000000
     WheelVert=-0.500000
     MaxSteerAngle=3900.000000
     MaxBrakeTorque=50.000000
     TorqueSplit=0.500000
     SteerPropGap=1000.000000
     SteerTorque=1000.000000
     SteerSpeed=15000.000000
     SuspStiffness=50.000000
     SuspDamping=5.000000
     SuspHighLimit=1.000000
     SuspLowLimit=-1.000000
     TireRollFriction=1.000000
     TireLateralFriction=1.000000
     TireRollSlip=0.085000
     TireLateralSlip=0.060000
     TireMinSlip=0.001000
     TireSlipRate=0.000500
     TireSoftness=0.000200
     TireMass=0.500000
     HandbrakeThresh=1000.000000
     TireHandbrakeSlip=0.060000
     TireHandbrakeFriction=-0.500000
     ChassisMass=4.000000
     StopThreshold=100.000000
     TorqueCurve=(Points=((OutVal=150.000000),(InVal=245756.000000,OutVal=150.000000),(InVal=491512.000000)))
     FlipTorque=350.000000
     FlipTime=3.000000
     MaxNetUpdateInterval=0.400000
     Gear=1
}

Overview Package Class Source Class tree Glossary
previous class      next class frames      no frames
Class file time: pr 5-2-2018 12:35:52.000 - Creation time: sk 18-3-2018 09:50:11.665 - Created with UnCodeX