2022-07-29 22:06:02 +03:00

77 lines
2.0 KiB
Plaintext

--@include ./powertrain_component.txt
--@include /koptilnya/libs/constants.txt
--@include /koptilnya/libs/entity.txt
require('./powertrain_component.txt')
require('/koptilnya/libs/constants.txt')
require('/koptilnya/libs/entity.txt')
Wheel = class('Wheel', PowertrainComponent)
function Wheel:initialize(config, prefix)
PowertrainComponent.initialize(self)
self.prefix = prefix
self.direction = config.Direction or 1
self.wireInputs = {
[prefix .. 'Wheel'] = 'entity'
}
self.wireOutputs = {
[prefix .. 'WheelRPM'] = 'number',
[prefix .. 'WheelTorque'] = 'number'
}
self.entity = NULL_ENTITY
hook.add('input', 'vehicle_wheel_update' .. prefix, function(key, val)
if key == prefix .. 'Wheel' then
self.entity = val
if not isValid(val) then
return
end
if config.CalculateInertia then
self.entity:setInertia(calculateWheelInertia(val))
end
self.inertia = self.entity:getInertia().y
end
end)
end
function Wheel:getRadius()
if not isValid(self.entity) then
return 0
end
return self.entity:getModelRadius() * UNITS_TO_METERS
end
function Wheel:updateWireOutputs()
wire.ports[self.prefix .. 'WheelRPM'] = self:getRPM()
wire.ports[self.prefix .. 'WheelTorque'] = self.torque
end
function Wheel:queryAngularVelocity()
return self.angularVelocity
end
function Wheel:forwardStep(torque, inertia)
if not isValid(self.entity) then
return 0
end
local initialAngularVelocity = self.angularVelocity
--self.entity:setInertia(self.entity:getInertia():setY(inertia))
self.torque = math.deg(torque) * 1.33 * -self.direction
self.entity:applyTorque(self.torque * TICK_INTERVAL * self.entity:getRight())
self.angularVelocity = math.rad(self.entity:getAngleVelocity().y) * self.direction
local deltaOmegaTorque = (self.angularVelocity - initialAngularVelocity) * self.inertia / TICK_INTERVAL
return torque
end