71 lines
1.8 KiB
Plaintext
71 lines
1.8 KiB
Plaintext
--@include ../powertrain_component.txt
|
|
|
|
local PowertrainComponent = require('../powertrain_component.txt')
|
|
|
|
local Gearbox = class('Gearbox', PowertrainComponent)
|
|
|
|
function Gearbox:initialize(vehicle, name, config)
|
|
PowertrainComponent.initialize(self, vehicle, name, config)
|
|
|
|
if CLIENT then return end
|
|
|
|
self.wireInputs = {
|
|
Upshift = 'number',
|
|
Downshift = 'number'
|
|
}
|
|
self.wireOutputs = {
|
|
[string.format('%s_RPM', self.name)] = 'number',
|
|
[string.format('%s_Torque', self.name)] = 'number',
|
|
[string.format('%s_Ratio', self.name)] = 'number'
|
|
}
|
|
|
|
self.type = config.Type or 'MANUAL'
|
|
self.inertia = config.Inertia or 1000
|
|
|
|
self.ratio = 0
|
|
end
|
|
|
|
function Gearbox:updateWireOutputs()
|
|
PowertrainComponent.updateWireOutputs(self)
|
|
|
|
wire.ports[string.format('%s_RPM', self.name)] = self:getRPM()
|
|
wire.ports[string.format('%s_Torque', self.name)] = self.torque
|
|
wire.ports[string.format('%s_Ratio', self.name)] = self.ratio
|
|
end
|
|
|
|
function Gearbox:queryInertia()
|
|
if self.output == nil or self.ratio == 0 then
|
|
return self.inertia
|
|
end
|
|
|
|
return self.inertia + self.output:queryInertia() / math.pow(self.ratio, 2)
|
|
end
|
|
|
|
function Gearbox:queryAngularVelocity(angularVelocity)
|
|
self.angularVelocity = angularVelocity
|
|
|
|
if self.output == nil or self.ratio == 0 then
|
|
return angularVelocity
|
|
end
|
|
|
|
return self.output:queryAngularVelocity(angularVelocity) * self.ratio
|
|
end
|
|
|
|
function Gearbox:forwardStep(torque, inertia)
|
|
self.torque = torque * self.ratio
|
|
|
|
if self.output == nil then
|
|
return torque
|
|
end
|
|
|
|
if self.ratio == 0 then
|
|
self.output:forwardStep(0, self.inertia * 0.5)
|
|
|
|
return torque
|
|
end
|
|
|
|
return self.output:forwardStep(self.torque, (inertia + self.inertia) * math.pow(self.ratio, 2)) / self.ratio
|
|
end
|
|
|
|
return Gearbox
|