forked from Courseplay/courseplay
-
Notifications
You must be signed in to change notification settings - Fork 0
/
vehicles.lua
1412 lines (1213 loc) · 53.2 KB
/
vehicles.lua
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
local abs, max, rad, deg, cos, sin, ceil = math.abs, math.max, math.rad, math.deg, math.cos, math.sin, math.ceil;
local _;
local truckAttacherJoint = {};
-- ##### VEHICLE TOOLS ##### --
courseplay.attacherJointNodeRotationList = {};
function courseplay:calculateTurnRadius(type, wheelBase, rotMax, CPRatio)
local turnRadius = 0;
CPRatio = CPRatio or 0;
-- ArticulatedAxis Steering
if (type == "ASW" or type == "Tool") and CPRatio > 0 and CPRatio < 1 then
if CPRatio <= 0.5 then
turnRadius = wheelBase * (1-CPRatio) / sin(rotMax) * (CPRatio * cos(rotMax) + 1 - CPRatio);
else
turnRadius = wheelBase * CPRatio / sin(rotMax) * (CPRatio + (1 - CPRatio) * cos(rotMax));
end;
-- 4 Wheel Steering
elseif type == "4WS" then
turnRadius = wheelBase / (2 * sin(rotMax)) * cos(rotMax);
-- 2 Wheel Steering
elseif (type == "2WS" or type == "Tool") then
turnRadius = wheelBase / sin(rotMax) * cos(rotMax);
end;
return turnRadius;
end;
function courseplay:createNewLinkedNode(object, nodeName, linkToNode)
if not object.cp.notesToDelete then object.cp.notesToDelete = {}; end;
local node = createTransformGroup(nodeName);
link(linkToNode, node);
table.insert(object.cp.notesToDelete, 1, node);
return node;
end;
function courseplay:disableCropDestruction(vehicle)
-- Make sure we have the cp table
if vehicle.cp == nil then vehicle.cp = {}; end;
-- Disable crop destruction if enabled
if vehicle.cropDestruction then
vehicle.cp.cropDestructionIsActiveBackup = vehicle.cropDestruction.isActive;
vehicle.cropDestruction.isActive = false;
end;
-- CHECK ATTACHED IMPLEMENTS
for _,impl in pairs(vehicle.attachedImplements) do
courseplay:disableCropDestruction(impl.object);
end;
end;
function courseplay:enableCropDestruction(vehicle)
-- Enable crop destruction if backup is set
if vehicle.cropDestruction and vehicle.cp and vehicle.cp.cropDestructionIsActiveBackup ~= nil then
vehicle.cropDestruction.isActive = vehicle.cp.cropDestructionIsActiveBackup;
vehicle.cp.cropDestructionIsActiveBackup = nil;
end;
-- CHECK ATTACHED IMPLEMENTS
for _,impl in pairs(vehicle.attachedImplements) do
courseplay:enableCropDestruction(impl.object);
end;
end;
--- courseplay:findJointNodeConnectingToNode(workTool, fromNode, toNode, doReverse)
-- Returns: (node, backtrack, rotLimits)
-- node will return either: 1. The jointNode that connects to the toNode,
-- 2. The toNode if no jointNode is found but the fromNode is inside the same component as the toNode
-- 3. nil in case none of the above fails.
-- backTrack will return either: 1. A table of all the jointNodes found from fromNode to toNode, if the jointNode that connects to the toNode is found.
-- 2: nil if no jointNode is found.
-- rotLimits will return either: 1. A table of all the rotLimits of the componentJoint, found from fromNode to toNode, if the jointNode that connects to the toNode is found.
-- 2: nil if no jointNode is found.
function courseplay:findJointNodeConnectingToNode(workTool, fromNode, toNode, doReverse)
if fromNode == toNode then return toNode; end;
-- Attempt to find the jointNode by backtracking the compomentJoints.
for index, component in ipairs(workTool.components) do
if courseplay:isPartOfNode(fromNode, component.node) then
if not doReverse then
for _, joint in ipairs(workTool.componentJoints) do
if joint.componentIndices[2] == index then
if workTool.components[joint.componentIndices[1]].node == toNode then
-- node backtrack rotLimits
return joint.jointNode, {joint.jointNode}, {joint.rotLimit};
else
local node, backTrack, rotLimits = courseplay:findJointNodeConnectingToNode(workTool, workTool.components[joint.componentIndices[1]].node, toNode);
if backTrack then table.insert(backTrack, 1, joint.jointNode); end;
if rotLimits then table.insert(rotLimits, 1, joint.rotLimit); end;
return node, backTrack, rotLimits;
end;
end;
end;
end;
-- Do Reverse in case not found
for _, joint in ipairs(workTool.componentJoints) do
if joint.componentIndices[1] == index then
if workTool.components[joint.componentIndices[2]].node == toNode then
-- node backtrack rotLimits
return joint.jointNode, {joint.jointNode}, {joint.rotLimit};
else
local node, backTrack, rotLimits = courseplay:findJointNodeConnectingToNode(workTool, workTool.components[joint.componentIndices[2]].node, toNode, true);
if backTrack then table.insert(backTrack, 1, joint.jointNode); end;
if rotLimits then table.insert(rotLimits, 1, joint.rotLimit); end;
return node, backTrack, rotLimits;
end;
end;
end;
end;
end;
-- Last attempt to find the jointNode by getting parent of parent untill hit or the there is no more parents.
if courseplay:isPartOfNode(fromNode, toNode) then
return toNode, nil;
end;
-- If anything else fails, return nil
return nil, nil;
end;
function courseplay:getDistances(object)
if not object.cp.distances then
cpPrintLine(6);
local distances = {};
-- STEERABLES
if object.cp.DirectionNode then
-- Finde the front and rear distance from the direction node
local front, rear = 0, 0;
local haveRunnedOnce = false
for _, wheel in ipairs(object.wheels) do
local wdnrxTemp, wdnryTemp, wdnrzTemp = getRotation(wheel.driveNode);
setRotation(wheel.driveNode, 0, 0, 0);
local wreprxTemp, wrepryTemp, wreprzTemp = getRotation(wheel.repr);
setRotation(wheel.repr, 0, 0, 0);
local xw, yw, zw = getWorldTranslation(wheel.driveNode);
local _,_,dis = worldToLocal(object.cp.DirectionNode, xw, yw, zw);
setRotation(wheel.repr, wreprxTemp, wrepryTemp, wreprzTemp);
setRotation(wheel.driveNode, wdnrxTemp, wdnryTemp, wdnrzTemp);
if haveRunnedOnce then
if dis < rear then rear = dis; end;
if dis > front then front = dis; end;
else
rear = dis;
front = dis;
haveRunnedOnce = true;
end;
end;
-- Set the wheel offset anddistance
distances.frontWheelToDirectionNodeOffset = front * -1;
distances.frontWheelToRearWheel = abs(front - rear);
courseplay:debug(('%s: frontWheelToDirectionNodeOffset=%.2f, frontWheelToRearWheel=%.2f'):format(nameNum(object), distances.frontWheelToDirectionNodeOffset, distances.frontWheelToRearWheel), 6);
-- Finde the attacherJoints distance from the direction node
for _, attacherJoint in ipairs(object.attacherJoints) do
local xj, yj, zj = getWorldTranslation(attacherJoint.jointTransform);
local _,_,dis = worldToLocal(object.cp.DirectionNode, xj, yj, zj);
if dis < front then
if not distances.frontWheelToRearTrailerAttacherJoints then
distances.frontWheelToRearTrailerAttacherJoints = {};
end;
distances.frontWheelToRearTrailerAttacherJoints[attacherJoint.jointType] = abs(front - dis);
courseplay:debug(('%s: frontWheelToRearTrailerAttacherJoints[%d]=%.2f'):format(nameNum(object), attacherJoint.jointType, distances.frontWheelToRearTrailerAttacherJoints[attacherJoint.jointType]), 6);
end;
end
-- Finde the attacherJoints distance from the turning node
local turningNode = courseplay:getRealTurningNode(object);
for _, attacherJoint in ipairs(object.attacherJoints) do
local xj, yj, zj = getWorldTranslation(attacherJoint.jointTransform);
local _, _, deltaZ = worldToLocal(object.cp.DirectionNode, xj, yj, zj);
-- If we are behind the front wheel, then it should be an attacherJoing on the rear
if deltaZ < front then
local _,_,dis = worldToLocal(turningNode, xj, yj, zj);
dis = dis * -1;
if not distances.turningNodeToRearTrailerAttacherJoints then
distances.turningNodeToRearTrailerAttacherJoints = {};
end;
distances.turningNodeToRearTrailerAttacherJoints[attacherJoint.jointType] = dis;
courseplay:debug(('%s: turningNodeToRearTrailerAttacherJoints[%d]=%.2f'):format(nameNum(object), attacherJoint.jointType, distances.turningNodeToRearTrailerAttacherJoints[attacherJoint.jointType]), 6);
end;
end
-- IMPLEMENTS OR TRAILERS
else
local node = object.attacherJoint.node;
local isHookLift = courseplay:isHookLift(object);
local lastNode = courseplay:getLastComponentNodeWithWheels(object)
if object.attacherJoint.rootNode ~= lastNode and not isHookLift then
local tempNode, backTrack, rotLimits = courseplay:findJointNodeConnectingToNode(object, object.attacherJoint.rootNode, lastNode);
if tempNode and backTrack then
node = tempNode;
local tnx, tny, tnz = getWorldTranslation(tempNode);
local xdis,ydis,dis = worldToLocal(object.attacherJoint.node, tnx, tny, tnz);
local nodeLength = 0;
local isPivoted = false;
for i = 1, #backTrack do
if rotLimits ~= nil and rotLimits[i]~= nil and rotLimits[i][2] ~= nil and rotLimits[i][2] > rad(15) then
isPivoted = true;
end;
if i == 1 then
tempNode = object.attacherJoint.node;
else
tempNode = backTrack[i-1];
end;
local tmpnx, tmpny, tmpnz = getWorldTranslation(tempNode);
local _,_,dis = worldToLocal(backTrack[i], tmpnx, tmpny, tmpnz);
courseplay:debug(('%s: backTrack[%d](node: %s) Length = %.2f'):format(nameNum(object), i, tostring(backTrack[i]), abs(dis)), 6);
nodeLength = nodeLength + abs(dis);
end;
if isPivoted then
distances.attacherJointToPivot = nodeLength;
courseplay:debug(('%s: attacherJointToPivot=%.2f'):format(nameNum(object), distances.attacherJointToPivot), 6);
end;
end;
end;
-- backup node rotation and set the rotation to 0
local nodeXTemp, nodeYTemp, nodeZTemp = getRotation(node);
setRotation(node, 0, 0, 0);
-- Find the distance from attacherJoint to rear wheel
if object.wheels and #object.wheels > 0 and not isHookLift then
local length = 0;
for _, wheel in ipairs(object.wheels) do
local nx, ny, nz = getWorldTranslation(wheel.driveNode);
local _,_,dis = worldToLocal(node, nx, ny, nz);
if abs(dis) > length then
length = abs(dis);
end;
end;
if distances.attacherJointToPivot then
distances.pivotToRearWheel = length;
distances.attacherJointToRearWheel = distances.attacherJointToPivot + length;
else
distances.attacherJointToRearWheel = length;
end;
courseplay:debug(('%s: attacherJointToRearWheel=%.2f'):format(nameNum(object), distances.attacherJointToRearWheel), 6);
end;
-- Finde the attacherJoints distance from the direction node
for _, attacherJoint in ipairs(object.attacherJoints) do
local nx, ny, nz = getWorldTranslation(attacherJoint.jointTransform);
local _,_,dis = worldToLocal(node, nx, ny, nz);
dis = dis * -1;
if dis > 0 then
if not distances.attacherJointToRearTrailerAttacherJoints then
distances.attacherJointToRearTrailerAttacherJoints = {};
end;
if distances.attacherJointToPivot then
if not distances.pivotToRearTrailerAttacherJoints then
distances.pivotToRearTrailerAttacherJoints = {};
end;
distances.pivotToRearTrailerAttacherJoints[attacherJoint.jointType] = abs(dis);
distances.attacherJointToRearTrailerAttacherJoints[attacherJoint.jointType] = distances.attacherJointToPivot + abs(dis);
else
distances.attacherJointToRearTrailerAttacherJoints[attacherJoint.jointType] = abs(dis);
end;
courseplay:debug(('%s: attacherJointToRearTrailerAttacherJoints[%d]=%.2f'):format(nameNum(object), attacherJoint.jointType, distances.attacherJointToRearTrailerAttacherJoints[attacherJoint.jointType]), 6);
end;
end;
if distances.attacherJointToRearWheel then
local turningNode = courseplay:getRealTurningNode(object);
-- Finde the attacherJoints distance from the turning node
for _, attacherJoint in ipairs(object.attacherJoints) do
local nx, ny, nz = getWorldTranslation(attacherJoint.jointTransform);
local _,_,dis = worldToLocal(turningNode, nx, ny, nz);
dis = dis * -1;
if not distances.turningNodeToTrailerAttacherJoints then
distances.turningNodeToTrailerAttacherJoints = {};
end;
distances.turningNodeToTrailerAttacherJoints[attacherJoint.jointType] = dis;
courseplay:debug(('%s: turningNodeToTrailerAttacherJoints[%d]=%.2f'):format(nameNum(object), attacherJoint.jointType, distances.turningNodeToTrailerAttacherJoints[attacherJoint.jointType]), 6);
end;
-- Finde the attacherJoint/Pivot distance to the turning node
local nx, ny, nz = getWorldTranslation(node);
local _,_,dis = worldToLocal(turningNode, nx, ny, nz);
distances.attacherJointOrPivotToTurningNode = dis;
courseplay:debug(('%s: attacherJointOrPivotToTurningNode=%.2f'):format(nameNum(object), distances.attacherJointOrPivotToTurningNode), 6);
end;
-- restore node rotation from backup.
setRotation(node, nodeXTemp, nodeYTemp, nodeZTemp);
end;
object.cp.distances = distances;
end;
return object.cp.distances;
end;
function courseplay:getDirectionNodeToTurnNodeLength(vehicle)
local distances = vehicle.cp.distances;
local totalDistance = 0;
for _, imp in ipairs(vehicle.attachedImplements) do
if courseplay:isRearAttached(vehicle, imp.jointDescIndex) then
local workTool = imp.object;
if courseplay:isWheeledWorkTool(workTool) then
local workToolDistances = workTool.cp.distances;
if workToolDistances.attacherJointToPivot then
totalDistance = totalDistance + workToolDistances.attacherJointToPivot;
end;
totalDistance = totalDistance + workToolDistances.attacherJointOrPivotToTurningNode;
else
if not distances.attacherJointOrPivotToTurningNode and distances.attacherJointToRearTrailerAttacherJoints then
totalDistance = totalDistance + distances.attacherJointToRearTrailerAttacherJoints[workTool.attacherJoint.jointType];
end;
totalDistance = totalDistance + courseplay:getDirectionNodeToTurnNodeLength(workTool);
end;
break;
end;
end;
if vehicle.cp.DirectionNode and totalDistance > 0 then
for _, imp in ipairs(vehicle.attachedImplements) do
if courseplay:isRearAttached(vehicle, imp.jointDescIndex) then
local workTool = imp.object;
totalDistance = totalDistance + distances.turningNodeToRearTrailerAttacherJoints[workTool.attacherJoint.jointType];
break;
end;
end;
end;
return totalDistance;
end;
function courseplay:getRealDollyFrontNode(dolly)
if dolly.cp.realDollyFrontNode == nil then
local node, _ = courseplay:findJointNodeConnectingToNode(dolly, dolly.attacherJoint.rootNode, dolly.rootNode);
if node then
-- Trailers without pivote
if (node == dolly.rootNode and dolly.attacherJoint.jointType ~= AttacherJoints.JOINTTYPE_IMPLEMENT)
-- Implements with pivot and wheels that do not lift the wheels from the ground.
or (node ~= dolly.rootNode and dolly.attacherJoint.jointType == AttacherJoints.JOINTTYPE_IMPLEMENT and not dolly.attacherJoint.topReferenceNode) then
dolly.cp.realDollyFrontNode = courseplay:getRealTurningNode(dolly);
else
dolly.cp.realDollyFrontNode = false;
end;
end;
end;
return dolly.cp.realDollyFrontNode
end;
function courseplay:getRealTrailerDistanceToPivot(workTool)
-- Attempt to find the pivot node.
local node, backTrack = courseplay:findJointNodeConnectingToNode(workTool, workTool.attacherJoint.rootNode, courseplay:getLastComponentNodeWithWheels(workTool));
if node then
local x,y,z;
if node == workTool.rootNode then
x,y,z = getWorldTranslation(workTool.attacherJoint.node);
else
x,y,z = getWorldTranslation(node);
end;
local _,_,tz = worldToLocal(courseplay:getRealTurningNode(workTool), x,y,z);
return tz;
else
return 3;
end;
end;
function courseplay:getRealTrailerFrontNode(workTool)
if not workTool.cp.realFrontNode then
local jointNode, backtrack = courseplay:findJointNodeConnectingToNode(workTool, workTool.attacherJoint.rootNode, workTool.rootNode);
if jointNode and backtrack and workTool.attacherJoint.jointType ~= AttacherJoints.JOINTTYPE_IMPLEMENT then
local rootNode;
for _, joint in ipairs(workTool.componentJoints) do
if joint.jointNode == jointNode and joint.rotLimit~= nil and joint.rotLimit[2] ~= nil and joint.rotLimit[2] > rad(15) then
rootNode = workTool.components[joint.componentIndices[2]].node;
break;
end;
end;
if rootNode then
local node = courseplay:createNewLinkedNode(workTool, "realFrontNode", rootNode);
local x, y, z = getWorldTranslation(jointNode);
local _,_,delta = worldToLocal(rootNode, x, y, z);
setTranslation(node, 0, 0, delta);
if courseplay:isInvertedToolNode(workTool, node) then
setRotation(node, 0, rad(180), 0);
end;
workTool.cp.realFrontNode = node;
end;
end;
if not workTool.cp.realFrontNode then
if courseplay:getLastComponentNodeWithWheels(workTool) ~= workTool.rootNode then
workTool.cp.realFrontNode = courseplay:getRealTurningNode(workTool, workTool.rootNode, "realFrontNode");
else
workTool.cp.realFrontNode = courseplay:getRealTurningNode(workTool);
end;
end;
end;
return workTool.cp.realFrontNode
end;
function courseplay:getRealTurningNode(object, useNode, nodeName)
if not object.cp.turningNode or useNode then
local node; -- Define local value
local _, y, _ = getWorldTranslation(object.rootNode);
local minDis, maxDis = 0, 0;
local minDisRot, maxDisRot = 0, 0;
local haveStraitWheels, haveTurningWheels = false, false;
local Distance = 0;
-- STEERABLES
if object.cp.DirectionNode then
-- Giants have provided us with steeringCenterNode, so use it.
if object.steeringCenterNode then
-- The steeringCenterNode is already set for us to use.
node = object.steeringCenterNode;
-- Check if it's actually an four wheel steering
if not object.crawlers or #object.crawlers == 0 then
for index, wheel in ipairs(object.wheels) do
-- Strait wheels
if wheel.rotMax == 0 and wheel.maxLatStiffness > 0 then
haveStraitWheels = true;
-- Turning wheels
else
haveTurningWheels = true;
end;
end;
if not haveStraitWheels and haveTurningWheels then
object.cp.isFourWheelSteering = true;
end;
end;
else
-- Greate an new linked node.
node = courseplay:createNewLinkedNode(object, "realTurningNode", object.rootNode);
-- Find the pivot point on articulated vehicle
if object.articulatedAxis then
local jointNode = object.articulatedAxis.componentJoint.jointNode;
local x,_,z = getWorldTranslation(jointNode);
_,_,Distance = worldToLocal(object.rootNode, x, y, z);
-- Get the distance from root node to the wheels turning point.
else
local rotMax = 0;
-- Sort wheels in turning wheels and strait wheels and find the min and max distance for each set.
for index, wheel in ipairs(object.wheels) do
local x,_,z = getWorldTranslation(wheel.repr);
local _,_,dis = worldToLocal(object.rootNode, x, y, z);
-- Strait wheels
if wheel.rotMax == 0 and wheel.maxLatStiffness > 0 then
if haveStraitWheels then
if dis < minDis then minDis = dis; end;
if dis > maxDis then maxDis = dis; end;
else
minDis = dis;
maxDis = dis;
haveStraitWheels = true;
end;
-- Turning wheels
else
if abs(wheel.rotMax) > rotMax then
rotMax = abs(wheel.rotMax)
end;
if haveTurningWheels then
if dis < minDisRot then minDisRot = dis; end;
if dis > maxDisRot then maxDisRot = dis; end;
else
minDisRot = dis;
maxDisRot = dis;
haveTurningWheels = true;
end;
end;
end;
-- 2WS: Calculate strait wheel median distance
if haveStraitWheels then
if minDis == maxDis then
Distance = minDis;
else
Distance = (minDis + maxDis) * 0.5;
end;
-- 4WS: Calculate turning wheel median distance if there are no strait wheels.
elseif haveTurningWheels then
object.cp.isFourWheelSteering = true;
object.cp.fourWheelSteerMaxRot = rotMax;
if minDisRot == maxDisRot then
Distance = minDisRot;
else
Distance = (minDisRot + maxDisRot) * 0.5;
end;
end;
end;
if Distance ~= 0 then
setTranslation(node, 0, 0, Distance);
end;
end;
-- IMPLEMENTS OR TRAILERS
else
local invert = courseplay:isInvertedToolNode(object) and -1 or 1;
local steeringAxleScale = 0;
-- Use useNode or Get the last component node with wheels
local componentNode = useNode or courseplay:getLastComponentNodeWithWheels(object);
-- Greate an new linked node based on what component to use or nodeName.
local transformGroupName = nodeName or "realTurningNode";
node = courseplay:createNewLinkedNode(object, transformGroupName, componentNode);
if not useNode and not nodeName then
-- Get the distance from root node to the wheels turning point.
if object.wheels and #object.wheels > 0 then
local steeringAxleScaleMin, steeringAxleScaleMax = 0, 0;
-- Sort wheels in turning wheels and strait wheels and find the min and max distance for each set.
for i = 1, #object.wheels do
if courseplay:isPartOfNode(object.wheels[i].node, componentNode) and object.wheels[i].isLeft ~= nil and object.wheels[i].maxLatStiffness > 0 then
local x,_,z = getWorldTranslation(object.wheels[i].driveNode);
local _,_,dis = worldToLocal(componentNode, x, y, z);
dis = dis * invert;
courseplay:debug(('%s: getRealTurningNode(): wheel%d distance = %.2f'):format(nameNum(object), i, dis), 6);
if object.steeringAxleUpdateBackwards == false or object.wheels[i].steeringAxleScale == 0 then
if haveStraitWheels then
if dis < minDis then minDis = dis; end;
if dis > maxDis then maxDis = dis; end;
else
minDis = dis;
maxDis = dis;
haveStraitWheels = true;
end;
else
if object.wheels[i].steeringAxleScale < 0 and object.wheels[i].steeringAxleScale < steeringAxleScaleMin then
steeringAxleScaleMin = object.wheels[i].steeringAxleScale;
elseif object.wheels[i].steeringAxleScale > 0 and object.wheels[i].steeringAxleScale > steeringAxleScaleMax then
steeringAxleScaleMax = object.wheels[i].steeringAxleScale;
end;
if haveTurningWheels then
if dis < minDisRot then minDisRot = dis; end;
if dis > maxDisRot then maxDisRot = dis; end;
else
minDisRot = dis;
maxDisRot = dis;
haveTurningWheels = true;
end;
end;
end;
end;
-- Calculate strait wheel median distance
if haveStraitWheels then
if minDis == maxDis then
Distance = minDis;
else
Distance = (minDis + maxDis) * 0.5;
end;
-- Calculate turning wheel median distance if there are no strait wheels.
elseif haveTurningWheels then
steeringAxleScale = steeringAxleScaleMin + steeringAxleScaleMax;
if minDisRot == maxDisRot then
Distance = minDisRot;
else
Distance = (minDisRot + maxDisRot) * 0.5;
end;
end;
courseplay:debug(('%s: getRealTurningNode(): haveStraitWheels=%q, haveTurningWheels=%q, Distance=%2f'):format(nameNum(object), tostring(haveStraitWheels), tostring(haveTurningWheels), Distance), 6);
end;
else
local jointNode = courseplay:getPivotJointNode(object);
if jointNode then
local x,_,z = getWorldTranslation(jointNode);
local _,_,dis = worldToLocal(node, x, y, z);
Distance = dis * invert;
end;
courseplay:debug(('%s: getRealTurningNode(): useNode=%q, nodeName=%q, Distance=%2f'):format(nameNum(object), tostring(useNode ~= nil), tostring(transformGroupName), Distance), 6);
end;
if object.cp.realTurnNodeOffsetZ and type(object.cp.realTurnNodeOffsetZ) == "number" then
Distance = Distance + object.cp.realTurnNodeOffsetZ;
courseplay:debug(('%s: getRealTurningNode(): Special turn node offset set: realTurnNodeOffsetZ=%2f, New Distance=%2f'):format(nameNum(object), object.cp.realTurnNodeOffsetZ, Distance), 6);
end;
if Distance ~= 0 then
setTranslation(node, 0, 0, Distance);
end;
if courseplay:isInvertedToolNode(object, node) then
setRotation(node, 0, rad(180), 0);
end;
if not haveStraitWheels and object.steeringAxleUpdateBackwards and steeringAxleScale < 0 then
local tempNode, _ = courseplay:findJointNodeConnectingToNode(object, object.attacherJoint.rootNode, componentNode);
if tempNode then
local x, y, z;
if tempNode == object.rootNode then
x, y, z = getWorldTranslation(object.attacherJoint.node);
else
x, y, z = getWorldTranslation(tempNode);
end;
local _,_,dis = worldToLocal(node, x, y, z);
local offset = (dis * abs(steeringAxleScale)) + Distance;
setTranslation(node, 0, 0, offset);
object.cp.steeringAxleUpdateBackwards = true;
end;
end;
end;
if useNode then
return node;
else
object.cp.turningNode = node;
end;
end;
return object.cp.turningNode;
end;
function courseplay:getPivotJointNode(workTool)
if workTool.cp.jointNode == nil then
local componentNode = courseplay:getLastComponentNodeWithWheels(workTool);
for index, component in ipairs(workTool.components) do
-- Check if we are in the right component.
if component.node == componentNode then
for jointIndex, joint in ipairs(workTool.componentJoints) do
-- Check if we have the right componentJoint and if it's an pivot joint
if joint.componentIndices[2] ~= nil and joint.rotLimit~= nil and joint.rotLimit[2]~= nil and joint.componentIndices[2] == index and joint.rotLimit[2] > rad(15) then
-- Set the joint index and stop the loop.
workTool.cp.jointNode = workTool.componentJoints[jointIndex].jointNode;
break;
end;
end;
break;
end;
end;
if not workTool.cp.jointNode or not courseplay:isWheeledWorkTool(workTool) then workTool.cp.jointNode = false end;
end;
return workTool.cp.jointNode;
end;
function courseplay:getLastComponentNodeWithWheels(workTool)
-- Check if there is more than 1 component
if workTool.wheels and #workTool.wheels > 0 and #workTool.components > 1 then
-- Check if the tool has inverted nodes
local invert = courseplay:isInvertedToolNode(workTool) and -1 or 1;
-- Set default node to start from.
local node = workTool.rootNode;
-- Loop through all the components.
for index, component in ipairs(workTool.components) do
-- Don't use the component that is the rootNode.
if component.node ~= node then
-- Loop through all the wheels and see if they are attached to this component.
for i = 1, #workTool.wheels do
-- isLeft is only set for real wheels and not dummy wheels, so we can use that to sort out the dummy wheels
if workTool.wheels[i].isLeft ~= nil then
if courseplay:isPartOfNode(workTool.wheels[i].node, component.node) then
-- Check if they are linked together
for _, joint in ipairs(workTool.componentJoints) do
if joint.componentIndices[2] == index then
if workTool.components[joint.componentIndices[1]].node == node then
-- Check if the component is behind the node.
local xJoint,yJoint,zJoint = getWorldTranslation(joint.jointNode);
local offset,_,direction = worldToLocal(node, xJoint,yJoint,zJoint);
--offset check to make sure we are selecting a node that is centered
if (direction * invert) < 0 and offset == 0 then
-- Component is hehind, so set the node to the new component node.
node = component.node;
end;
end;
end;
end;
break;
end;
end;
end;
end;
end;
-- Return the found node.
return node;
end;
-- Return default rootNode if none is found.
return workTool.rootNode
end;
function courseplay:getRealUnloadOrFillNode(workTool)
if workTool.cp.unloadOrFillNode == nil then
-- BALELOADERS and STRAWBLOWERS
if courseplay:isBaleLoader(workTool) or (courseplay:isSpecialBaleLoader(workTool) and workTool.cp.specialUnloadDistance) or workTool.cp.isStrawBlower then
-- Create the new node and link it to realTurningNode
local node = courseplay:createNewLinkedNode(workTool, "UnloadOrFillNode", courseplay:getRealTurningNode(workTool));
-- make sure we set the node distance position
local Distance = workTool.cp.specialUnloadDistance or -5;
setTranslation(node, 0, 0, Distance);
workTool.cp.unloadOrFillNode = node;
-- NORMAL FILLABLE TRAILERS WITH ALLOW TO BE FILLED FROM THE AIR
elseif workTool.cp.hasSpecializationFillable and workTool.allowFillFromAir then
-- Create the new node and link it to exactFillRootNode
local node = courseplay:createNewLinkedNode(workTool, "UnloadOrFillNode", workTool.exactFillRootNode);
-- Make sure ve set the height position to the same as the realTurningNode
local x, y, z = getWorldTranslation(courseplay:getRealTurningNode(workTool));
local _,Height,_ = worldToLocal(workTool.exactFillRootNode, x, y, z);
setTranslation(node, 0, Height, 0);
if courseplay:isInvertedToolNode(workTool, node) then
setRotation(node, 0, rad(180), 0);
end;
workTool.cp.unloadOrFillNode = node;
-- NONE OF THE ABOVE
else
workTool.cp.unloadOrFillNode = false;
end;
end;
return workTool.cp.unloadOrFillNode;
end;
function courseplay:getHighestToolTurnDiameter(object)
local turnDiameter = 0;
-- Tool attached to Steerable
for _, implement in ipairs(object.attachedImplements) do
local workTool = implement.object;
if courseplay:isRearAttached(object, implement.jointDescIndex) then
local ttr = courseplay:getToolTurnRadius(workTool);
turnDiameter = ttr * 2;
courseplay:debug(('%s: toolTurnDiameter=%.2fm'):format(nameNum(workTool), turnDiameter), 6);
-- Check rear attached tools for turnDiameters
if workTool.attachedImplements and workTool.attachedImplements ~= {} then
local ttd = courseplay:getHighestToolTurnDiameter(workTool);
if ttd > turnDiameter then
turnDiameter = ttd;
end;
end;
end;
end;
return turnDiameter;
end;
function courseplay:getToolTurnRadius(workTool)
local turnRadius = 0; -- Default value if none is set
if workTool.cp.overwriteTurnRadius and type(workTool.cp.overwriteTurnRadius) == "number" then
turnRadius = workTool.cp.overwriteTurnRadius;
courseplay:debug(('%s -> TurnRadius: overwriteTurnRadius is set: turnRadius set to %.2fm'):format(nameNum(workTool), turnRadius), 6);
elseif courseplay:isWheeledWorkTool(workTool) then
local radiusMultiplier = 1.05; -- Used to add a little bit to the radius, for safer turns.
local wheelBase = 0;
local rotMax = 0;
local CPRatio = 0;
local type = "Tool";
local TR = 0;
local frontLength = 0;
--attacherJointOrPivotToTurningNode
local attacherVehicle = workTool.attacherVehicle;
local workToolDistances = workTool.cp.distances or courseplay:getDistances(workTool);
for i, attachedImplement in pairs(attacherVehicle.attachedImplements) do
if attachedImplement.object == workTool then
-- Check if AIVehicleUtil can calculate it for us
local AIMaxToolRadius = AIVehicleUtil.getMaxToolRadius(attachedImplement) * 0.5;
if AIMaxToolRadius > 0 then
if workToolDistances.attacherJointOrPivotToTurningNode > AIMaxToolRadius then
AIMaxToolRadius = workToolDistances.attacherJointOrPivotToTurningNode;
end;
courseplay:debug(('%s -> TurnRadius: AIVehicleUtil.getMaxToolRadius=%.2fm'):format(nameNum(workTool), AIMaxToolRadius), 6);
return AIMaxToolRadius;
end;
-- AIVehicleUtil could not calculate it, so we do it our self.
rotMax = attachedImplement.upperRotLimit[2];
break;
end;
end;
local attacherVehicleDistances = attacherVehicle.cp.distances or courseplay:getDistances(attacherVehicle);
if deg(rotMax) >= 30 and deg(rotMax) < 90 then
-- We have turningNodeToRearTrailerAttacherJoints value
if attacherVehicleDistances.turningNodeToRearTrailerAttacherJoints then
frontLength = attacherVehicleDistances.turningNodeToRearTrailerAttacherJoints[workTool.attacherJoint.jointType] or 0;
-- We have turningNodeToTrailerAttacherJoints value
elseif attacherVehicleDistances.turningNodeToTrailerAttacherJoints then
frontLength = attacherVehicleDistances.turningNodeToTrailerAttacherJoints[workTool.attacherJoint.jointType] or 0;
-- We have to go backwards to find the real front distance (attacherVehicle dont have wheels and might be a weight or something else)
else
frontLength = attacherVehicleDistances.attacherJointToRearTrailerAttacherJoints[workTool.attacherJoint.jointType] or 0;
local backTrackVehicle = attacherVehicle;
local oldBackTrackVehicle;
while true do
oldBackTrackVehicle = backTrackVehicle;
backTrackVehicle = oldBackTrackVehicle.attacherVehicle;
if backTrackVehicle and backTrackVehicle ~= {} then
local distances = backTrackVehicle.cp.distances or courseplay:getDistances(backTrackVehicle);
local jointType = oldBackTrackVehicle.attacherJoint.jointType;
if distances.turningNodeToRearTrailerAttacherJoints then
frontLength = frontLength + distances.turningNodeToRearTrailerAttacherJoints[jointType];
break;
elseif distances.turningNodeToTrailerAttacherJoints then
frontLength = frontLength + distances.turningNodeToTrailerAttacherJoints[jointType];
break;
else
frontLength = frontLength + (distances.attacherJointToRearTrailerAttacherJoints[jointType] or 0);
end;
else
break;
end;
end;
end;
courseplay:debug(('%s -> TurnRadius: rotMax=%d°, frontLength=%.2fm'):format(nameNum(workTool), deg(rotMax), frontLength), 6);
-- WE ARE A PIVOTED TRAILER / IMPLEMENT
if workToolDistances.attacherJointToPivot then
local pivotRotMax = 0;
local lastNode = courseplay:getLastComponentNodeWithWheels(workTool)
local _, _, rotLimits = courseplay:findJointNodeConnectingToNode(workTool, workTool.attacherJoint.rootNode, lastNode);
if rotLimits then
for _, rotLimit in pairs(rotLimits) do
if rotLimit[2] > pivotRotMax and rotLimit[2] > rad(15) then
pivotRotMax = rotLimit[2];
end;
end;
end;
courseplay:debug(('%s -> TurnRadius: pivotRotMax=%d° (Pivot trailer/implement)'):format(nameNum(workTool), deg(pivotRotMax)), 6);
-- We are an implement and should be handled a bit different
if workTool.attacherJoint.jointType == AttacherJoints.JOINTTYPE_IMPLEMENT then
-- We have a valid pivotRotMax, so calculate it normally.
if pivotRotMax > rad(15) then
frontLength = frontLength + workToolDistances.attacherJointToPivot;
wheelBase = frontLength + workToolDistances.attacherJointOrPivotToTurningNode;
CPRatio = courseplay:getCenterPivotRatio(nil, wheelBase, frontLength);
TR = courseplay:calculateTurnRadius(type, wheelBase, pivotRotMax, CPRatio);
-- If pivotRotMax is not greater than 15 degrees,
-- we will then use half of the length from attacherJoint to turningNode as the turnRadius instead.
else
TR = ceil((workToolDistances.attacherJointToPivot + workToolDistances.attacherJointOrPivotToTurningNode) / 2 * radiusMultiplier);
end;
courseplay:debug(('%s -> TurnRadius: turnRadius=%.2fm (Pivot implement)'):format(nameNum(workTool), TR), 6);
-- We are an pivoted trailer
else
-- We have a valid pivotRotMax, so calculate it normally.
if pivotRotMax > rad(15) then
-- Dolly part
wheelBase = frontLength + workToolDistances.attacherJointToPivot;
CPRatio = courseplay:getCenterPivotRatio(nil, wheelBase, frontLength);
local pivotTR = ceil(courseplay:calculateTurnRadius(type, wheelBase, rotMax, CPRatio) * radiusMultiplier);
-- Trailer part
wheelBase = workToolDistances.attacherJointOrPivotToTurningNode;
CPRatio = 0;
TR = ceil(courseplay:calculateTurnRadius(type, wheelBase, pivotRotMax, CPRatio) * radiusMultiplier);
-- Take the highest one
if pivotTR > TR then
TR = pivotTR;
end;
-- If pivotRotMax is not greater than 15 degrees,
-- we will then use half of the length from attacherJoint to turningNode as the turnRadius instead.
else
TR = ceil((workToolDistances.attacherJointToPivot + workToolDistances.attacherJointOrPivotToTurningNode) / 2 * radiusMultiplier);
end;
courseplay:debug(('%s -> TurnRadius: turnRadius=%.2fm (Pivot trailer)'):format(nameNum(workTool), TR), 6);
end;
-- WE ARE A NORMAL TRAILER OR IMPLEMENT
else
wheelBase = frontLength + (workToolDistances.attacherJointOrPivotToTurningNode or 0);
CPRatio = courseplay:getCenterPivotRatio(nil, wheelBase, frontLength);
TR = ceil(courseplay:calculateTurnRadius(type, wheelBase, rotMax, CPRatio) * radiusMultiplier);
courseplay:debug(('%s -> TurnRadius: turnRadius=%.2fm (Normal trailer/implement)'):format(nameNum(workTool), TR), 6);
end;
if TR > 0 then
turnRadius = TR;
end;
end;
-- If we are not an implement then check if half trailer length is bigger than the turnRadius and set it, if it is.
if ((deg(rotMax) < 30 and deg(rotMax) >= 90) or workTool.attacherJoint.jointType ~= AttacherJoints.JOINTTYPE_IMPLEMENT) and workToolDistances.attacherJointToRearWheel then
if (workToolDistances.attacherJointToRearWheel / 2) > turnRadius then
turnRadius = ceil(workToolDistances.attacherJointToRearWheel / 2 * radiusMultiplier);
courseplay:debug(('%s -> TurnRadius: Using half tool length = %.2fm'):format(nameNum(workTool), turnRadius), 6);
end;
end;
else
courseplay:debug(('%s -> TurnRadius: Have no wheels. turnRadius set to 0m'):format(nameNum(workTool)), 6);
end;
return turnRadius;
end;
function courseplay:getTotalLengthOnWheels(vehicle)
courseplay:debug(('%s: getTotalLengthOnWheels()'):format(nameNum(vehicle)), 6);
local totalLength = 0;
local directionNodeToFrontWheelOffset;
if not vehicle.cp.distances or (courseplay.debugChannels[6] ~= nil and courseplay.debugChannels[6] == true) then
vehicle.cp.distances = courseplay:getDistances(vehicle);
end;
-- STEERABLES
if vehicle.cp.DirectionNode then
directionNodeToFrontWheelOffset = vehicle.cp.distances.frontWheelToDirectionNodeOffset;
local _, y, _ = getWorldTranslation(vehicle.cp.DirectionNode);
local hasRearAttach = false;
local jointType = 0;
for _, implement in ipairs(vehicle.attachedImplements) do
-- Check if it's rear attached
if courseplay:isRearAttached(vehicle, implement.jointDescIndex) then
hasRearAttach = true;
local length, _ = courseplay:getTotalLengthOnWheels(implement.object);
if length > 0 then
jointType = implement.object.attacherJoint.jointType;
totalLength = length;
end;
end;
end;
if hasRearAttach and totalLength > 0 and jointType > 0 then
local length = vehicle.cp.distances.frontWheelToRearTrailerAttacherJoints[jointType];
if length then
totalLength = totalLength + length;
else
totalLength = 0;
directionNodeToFrontWheelOffset = 0;
end;
courseplay:debug(('%s: hasRearAttach: totalLength=%.2f'):format(nameNum(vehicle), totalLength), 6);
else
totalLength = vehicle.cp.distances.frontWheelToRearWheel;
courseplay:debug(('%s: Using frontWheelToRearWheel=%.2f'):format(nameNum(vehicle), totalLength), 6);
end;
cpPrintLine(6);
courseplay:debug(('%s: totalLength=%.2f, totalLengthOffset=%.2f'):format(nameNum(vehicle), totalLength, directionNodeToFrontWheelOffset), 6);
cpPrintLine(6);
-- IMPLEMENTS OR TRAILERS
else
local _, y, _ = getWorldTranslation(vehicle.attacherJoint.node);