45 lines
1.2 KiB
Lua
45 lines
1.2 KiB
Lua
Clutch = class('Clutch')
|
|
|
|
function Clutch:initialize(options)
|
|
options = options or {}
|
|
|
|
self.stiffness = options.Stiffness or 10
|
|
self.capacity = options.Capacity or 2
|
|
self.damping = options.Damping or 0.3
|
|
self.maxTorque = options.MaxTorque or 150
|
|
|
|
self.press = 0
|
|
self.slip = 0
|
|
self.targetTorque = 0
|
|
self.torque = 0
|
|
|
|
self._engine = nil
|
|
self._gearbox = nil
|
|
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.press), -self.maxTorque, self.maxTorque)
|
|
|
|
self.torque = math.lerp(self.damping, self.torque, self.targetTorque)
|
|
end
|
|
|
|
function Clutch:linkEngine(engine)
|
|
self._engine = engine
|
|
end
|
|
|
|
function Clutch:linkGearbox(gbox)
|
|
self._gearbox = gbox
|
|
end
|
|
|
|
function Clutch:setPress(val)
|
|
self.press = math.clamp(val, 0, 1)
|
|
end
|