62 lines
1.5 KiB
Plaintext
62 lines
1.5 KiB
Plaintext
-- @include ./wire_component.txt
|
|
-- @include ./enums/gearbox.txt
|
|
require('./wire_component.txt')
|
|
local gearboxTypes = require('./enums/gearbox.txt')
|
|
|
|
Clutch = class('Clutch', WireComponent)
|
|
|
|
function Clutch:initialize(config)
|
|
self.stiffness = config.Stiffness
|
|
self.damping = config.Damping
|
|
self.maxTorque = config.MaxTorque
|
|
|
|
self.press = 0
|
|
self.slip = 0
|
|
self.targetTorque = 0
|
|
self.torque = 0
|
|
|
|
self._engine = nil
|
|
self._gearbox = nil
|
|
end
|
|
|
|
function Clutch:linkEngine(eng)
|
|
self._engine = eng
|
|
end
|
|
|
|
function Clutch:linkGearbox(gbox)
|
|
self._gearbox = gbox
|
|
end
|
|
|
|
function Clutch:getInputs()
|
|
return {}
|
|
end
|
|
|
|
function Clutch:getOutputs()
|
|
return {
|
|
ClutchTorque = 'number',
|
|
ClutchSlip = 'number'
|
|
}
|
|
end
|
|
|
|
function Clutch:getPress()
|
|
if self._gearbox ~= nil and self._gearbox.type == gearboxTypes.MANUAL then
|
|
return wire.ports.Clutch
|
|
else
|
|
return self.press
|
|
end
|
|
end
|
|
|
|
function Clutch:update()
|
|
local someConversionCoeff = 0.10472
|
|
|
|
local engRPM = self._engine and self._engine.rpm or 0
|
|
local gboxRPM = self._gearbox and self._gearbox.rpm or 0
|
|
local gboxRatio = self._gearbox and self._gearbox.ratio or 0
|
|
local gboxRatioNotZero = gboxRatio ~= 0 and 1 or 0
|
|
|
|
self.slip = ((engRPM - gboxRPM) * someConversionCoeff) * gboxRatioNotZero / 2
|
|
self.targetTorque = math.clamp(self.slip * self.stiffness * (1 - self:getPress()), -self.maxTorque, self.maxTorque)
|
|
|
|
self.torque = math.lerp(self.damping, self.torque, self.targetTorque)
|
|
end
|