Script v1.7.1.0
- AI
- Animals
- Contracts
- Debug
- Economy
- Effects
- Events
- Farms
- GUI
- Handtools
- I3d
- Materials
- Misc
- Objects
- Placeables
- Player
- Shop
- Sounds
- Specializations
- AIConveyorBelt
- AIImplement
- AIVehicle
- AnimatedVehicle
- ArticulatedAxis
- Attachable
- AttacherJointControl
- AttacherJoints
- BaleGrab
- BaleLoader
- Baler
- BaleWrapper
- BaseMaterial
- BunkerSiloCompacter
- BunkerSiloInteractor
- BuyableBale
- CCTDrivable
- Combine
- ConnectionHoses
- ConveyorBelt
- Cover
- CrabSteering
- Crawlers
- Cultivator
- Cutter
- Cylindered
- CylinderedFoldable
- Dashboard
- Dischargeable
- Drivable
- DynamicallyLoadedParts
- DynamicMountAttacher
- Enterable
- FertilizingCultivator
- FertilizingSowingMachine
- FillTriggerVehicle
- FillUnit
- FillVolume
- Foldable
- FoliageBending
- ForageWagon
- FrontloaderAttacher
- FruitPreparer
- GroundAdjustedNodes
- GroundReference
- Honk
- HookLiftContainer
- HookLiftTrailer
- IKChains
- JigglingParts
- Leveler
- Lights
- LivestockTrailer
- Locomotive
- LogGrab
- ManureBarrel
- MixerWagon
- Motorized
- Mountable
- Mower
- Pickup
- Pipe
- Plow
- PowerConsumer
- PowerTakeOffs
- RandomlyMovingParts
- ReceivingHopper
- ReverseDriving
- Rideable
- RidgeMarker
- Roller
- Ropes
- SemiTrailerFront
- Shovel
- SlopeCompensation
- SmartAttach
- SowingMachine
- SpeedRotatingParts
- SplineVehicle
- Sprayer
- StrawBlower
- StumpCutter
- Suspensions
- Tedder
- TensionBeltObject
- TensionBelts
- TipOccluder
- Trailer
- TreePlanter
- TreeSaw
- TurnOnVehicle
- Washable
- WaterTrailer
- Wearable
- Weeder
- Wheels
- Windrower
- Wipers
- WoodCrusher
- WoodHarvester
- WorkArea
- WorkMode
- WorkParticles
- Triggers
- Utils
- Vehicles
- Weather
Engine v1.7.1.0
- AI
- Animation
- Camera
- Entity
- Fillplanes
- General
- I3D
- Input
- Lighting
- Math
- Network
- Node
- Overlays
- Particle System
- Physics
- Rendering
- Scenegraph
- Shape
- Sound
- Spline
- String
- Terrain Detail
- Text Rendering
- Tire Track
- XML
- general
Foundation Reference
SlopeCompensation
DescriptionSpecialization for automatic slope compensation in vehicles based on the angle between two wheel nodesFunctions
- getCompensationAngleScale
- getCompensationGroundPosition
- loadCompensationNodeFromXML
- onPostLoad
- onUpdateTick
- prerequisitesPresent
- registerEventListeners
- registerFunctions
- slopeDetectionCallback
getCompensationAngleScale
DescriptionDefinitiongetCompensationAngleScale()Code
125 | function SlopeCompensation:getCompensationAngleScale(compensationNode) |
126 | return 1 |
127 | end |
getCompensationGroundPosition
DescriptionDefinitiongetCompensationGroundPosition()Code
131 | function SlopeCompensation:getCompensationGroundPosition(compensationNode, wheelId) |
132 | local spec = self.spec_slopeCompensation |
133 | local x, y, z = getWorldTranslation(compensationNode["wheel"..wheelId.."Node"]) |
134 | |
135 | spec.lastRaycastDistance = 0 |
136 | raycastAll(x, y, z, 0, -1, 0, "slopeDetectionCallback", compensationNode.raycastDistance, self, SlopeCompensation.SLOPE_COLLISION_MASK) |
137 | local distance = spec.lastRaycastDistance |
138 | if distance == 0 then |
139 | distance = compensationNode["lastDistance"..wheelId] |
140 | else |
141 | compensationNode["lastDistance"..wheelId] = spec.lastRaycastDistance |
142 | end |
143 | |
144 | return x, y - distance, z, distance ~= 0 |
145 | end |
loadCompensationNodeFromXML
DescriptionDefinitionloadCompensationNodeFromXML()Code
91 | function SlopeCompensation:loadCompensationNodeFromXML(compensationNode, xmlFile, key) |
92 | compensationNode.raycastDistance = 0 |
93 | compensationNode.lastDistance1 = 0 |
94 | compensationNode.lastDistance2 = 0 |
95 | for _, name in ipairs({"wheel1", "wheel2"}) do |
96 | local wheelId = getXMLInt(self.xmlFile, key.."#"..name) |
97 | if wheelId == nil then |
98 | g_logManager:xmlWarning(self.configFileName, "Missing %s for compensation node '%s'", name, key) |
99 | return false |
100 | end |
101 | |
102 | local wheel = self:getWheels()[wheelId] |
103 | if wheel ~= nil then |
104 | compensationNode[name.."Node"] = wheel.repr |
105 | compensationNode.raycastDistance = math.max(compensationNode.raycastDistance, wheel.radius+1) |
106 | else |
107 | g_logManager:xmlWarning(self.configFileName, "Unable to find wheel index '%d' for compensation node '%s'", wheelId, key) |
108 | return false |
109 | end |
110 | end |
111 | |
112 | compensationNode.maxAngle = Utils.getNoNilRad(getXMLFloat(self.xmlFile, key.."#maxAngle"), math.rad(5)) |
113 | compensationNode.minAngle = Utils.getNoNilRad(getXMLFloat(self.xmlFile, key.."#minAngle"), -compensationNode.maxAngle) |
114 | |
115 | compensationNode.animationName = getXMLString(self.xmlFile, key.."#animationName") |
116 | if compensationNode.animationName ~= nil then |
117 | self:setAnimationTime(compensationNode.animationName, 0.5, true) |
118 | end |
119 | |
120 | return true |
121 | end |
onPostLoad
DescriptionDefinitiononPostLoad()Code
38 | function SlopeCompensation:onPostLoad(savegame) |
39 | local spec = self.spec_slopeCompensation |
40 | |
41 | spec.lastRaycastDistance = 0 |
42 | spec.nodes = {} |
43 | local i = 0 |
44 | while true do |
45 | local key = string.format("vehicle.slopeCompensation.compensationNode(%d)", i) |
46 | if not hasXMLProperty(self.xmlFile, key) then |
47 | break |
48 | end |
49 | |
50 | local compensationNode = {} |
51 | if self:loadCompensationNodeFromXML(compensationNode, self.xmlFile, key) then |
52 | table.insert(spec.nodes, compensationNode) |
53 | end |
54 | |
55 | i = i + 1 |
56 | end |
57 | |
58 | spec.lastPos = -1 |
59 | spec.threshold = getXMLFloat(self.xmlFile, "vehicle.slopeCompensation#threshold") or 0.002 |
60 | end |
onUpdateTick
DescriptionDefinitiononUpdateTick()Code
64 | function SlopeCompensation:onUpdateTick(dt, isActiveForInput, isActiveForInputIgnoreSelection, isSelected) |
65 | local spec = self.spec_slopeCompensation |
66 | |
67 | for _, compensationNode in ipairs(spec.nodes) do |
68 | local x1, y1, z1, valid1 = self:getCompensationGroundPosition(compensationNode, 1) |
69 | local x2, y2, z2, valid2 = self:getCompensationGroundPosition(compensationNode, 2) |
70 | |
71 | if valid1 and valid2 then |
72 | local h = y1 - y2 |
73 | local l = MathUtil.vector2Length(x1-x2, z1-z2) |
74 | local angle = math.tan(h/l) * self:getCompensationAngleScale(compensationNode) |
75 | local pos = MathUtil.clamp((angle - compensationNode.minAngle) / (compensationNode.maxAngle - compensationNode.minAngle), 0, 1) |
76 | |
77 | if math.abs(spec.lastPos - pos) > spec.threshold then |
78 | spec.lastPos = pos |
79 | if self.setAnimationTime ~= nil then |
80 | if compensationNode.animationName ~= nil then |
81 | self:setAnimationTime(compensationNode.animationName, pos, true) |
82 | end |
83 | end |
84 | end |
85 | end |
86 | end |
87 | end |
prerequisitesPresent
DescriptionDefinitionprerequisitesPresent()Code
16 | function SlopeCompensation.prerequisitesPresent(specializations) |
17 | return SpecializationUtil.hasSpecialization(Wheels, specializations) |
18 | end |
registerEventListeners
DescriptionDefinitionregisterEventListeners()Code
31 | function SlopeCompensation.registerEventListeners(vehicleType) |
32 | SpecializationUtil.registerEventListener(vehicleType, "onPostLoad", SlopeCompensation) |
33 | SpecializationUtil.registerEventListener(vehicleType, "onUpdateTick", SlopeCompensation) |
34 | end |
registerFunctions
DescriptionDefinitionregisterFunctions()Code
22 | function SlopeCompensation.registerFunctions(vehicleType) |
23 | SpecializationUtil.registerFunction(vehicleType, "loadCompensationNodeFromXML", SlopeCompensation.loadCompensationNodeFromXML) |
24 | SpecializationUtil.registerFunction(vehicleType, "getCompensationAngleScale", SlopeCompensation.getCompensationAngleScale) |
25 | SpecializationUtil.registerFunction(vehicleType, "getCompensationGroundPosition", SlopeCompensation.getCompensationGroundPosition) |
26 | SpecializationUtil.registerFunction(vehicleType, "slopeDetectionCallback", SlopeCompensation.slopeDetectionCallback) |
27 | end |
slopeDetectionCallback
DescriptionDefinitionslopeDetectionCallback()Code
149 | function SlopeCompensation:slopeDetectionCallback(hitObjectId, x, y, z, distance) |
150 | if getRigidBodyType(hitObjectId) ~= "Static" then |
151 | return true |
152 | end |
153 | |
154 | self.spec_slopeCompensation.lastRaycastDistance = distance |
155 | |
156 | return false |
157 | end |