From ec65efcafc5813b906e3e63dbc8a2abbe2a1c8ce Mon Sep 17 00:00:00 2001 From: Nikita Kruglickiy Date: Fri, 29 Jul 2022 22:06:02 +0300 Subject: [PATCH] Attempts to fix new vehicle system --- koptilnya/engine_remastered/differential.txt | 23 ++++++++++------ koptilnya/engine_remastered/wheel.txt | 28 +++++++++++++++----- koptilnya/libs/constants.txt | 1 + koptilnya/libs/entity.txt | 8 +++--- 4 files changed, 42 insertions(+), 18 deletions(-) diff --git a/koptilnya/engine_remastered/differential.txt b/koptilnya/engine_remastered/differential.txt index 02f02c1..48d1464 100644 --- a/koptilnya/engine_remastered/differential.txt +++ b/koptilnya/engine_remastered/differential.txt @@ -11,13 +11,19 @@ Differential = class('Differential', PowertrainComponent) function Differential:initialize(config, order) PowertrainComponent.initialize(self, 'Axle') + self.wireOutputs = { + Diff_Torque = 'number' + } + local prefix = 'Axle' .. (order or '') .. '_' self.finalDrive = config.FinalDrive or 4 + self.inertia = config.Inertia or 0.2 self.biasAB = config.Bias or 0.5 self.coastRamp = config.CoastRamp or 0.5 self.powerRamp = config.PowerRamp or 1 self.preload = config.Preload or 10 + self.stiffness = config.Stiffness or 0.1 self.slipTorque = config.SlipTorque or 1000 local wheelConfig = { @@ -33,6 +39,8 @@ function Differential:initialize(config, order) end function Differential:updateWireOutputs() + wire.ports.Diff_Torque = self.torque + if self.outputA ~= nil then self.outputA:updateWireOutputs() end @@ -60,7 +68,7 @@ function Differential:queryAngularVelocity(angularVelocity) local aW = self.outputA:queryAngularVelocity(angularVelocity) local bW = self.outputB:queryAngularVelocity(angularVelocity) - return (aW - bW) * self.finalDrive * 0.5 + return (aW + bW) * self.finalDrive * 0.5 end function Differential:forwardStep(torque, inertia) @@ -69,12 +77,14 @@ function Differential:forwardStep(torque, inertia) end local aW = self.outputA:queryAngularVelocity(self.angularVelocity) - local bW = -self.outputB:queryAngularVelocity(self.angularVelocity) + local bW = self.outputB:queryAngularVelocity(self.angularVelocity) local aI = self.outputA:queryInertia() local bI = self.outputB:queryInertia() + self.torque = torque * self.finalDrive + local tqA, tqB = self:_openDiffTorqueSplit( - torque * self.finalDrive, + self.torque, aW, bW, aI, @@ -87,13 +97,10 @@ function Differential:forwardStep(torque, inertia) self.slipTorque ) - --tqA = tqA * aI * TICK_INTERVAL - --tqB = tqB * bI * TICK_INTERVAL - tqA = self.outputA:forwardStep(tqA, inertia * 0.5 + aI) tqB = self.outputB:forwardStep(tqB, inertia * 0.5 + bI) - return tqA + tqB + return (tqA + tqB) / self.finalDrive end function Differential:_openDiffTorqueSplit(tq, aW, bW, aI, bI, biasAB, preload, stiffness, powerRamp, coastRamp, slipTorque) @@ -109,7 +116,7 @@ function Differential:_lockingDiffTorqueSplit(tq, aW, bW, aI, bI, biasAB, preloa local bTqCorr = (w - bW) * bI -- / dt bTqCorr = bTqCorr * stiffness - local biasA = clamp(0.5 + (bW - aW) * 0.1 * stiffness, 0, 1) + local biasA = math.clamp(0.5 + (bW - aW) * 0.1 * stiffness, 0, 1) return tq * biasA + aTqCorr, tq * (1 - biasA) * bTqCorr end diff --git a/koptilnya/engine_remastered/wheel.txt b/koptilnya/engine_remastered/wheel.txt index 50f2491..ed92581 100644 --- a/koptilnya/engine_remastered/wheel.txt +++ b/koptilnya/engine_remastered/wheel.txt @@ -18,7 +18,8 @@ function Wheel:initialize(config, prefix) [prefix .. 'Wheel'] = 'entity' } self.wireOutputs = { - [prefix .. 'WheelRPM'] = 'number' + [prefix .. 'WheelRPM'] = 'number', + [prefix .. 'WheelTorque'] = 'number' } self.entity = NULL_ENTITY @@ -32,8 +33,7 @@ function Wheel:initialize(config, prefix) end if config.CalculateInertia then - --self.entity:setInertia(calculateWheelInertia(val)) - --print(calculateWheelInertia(val)) + self.entity:setInertia(calculateWheelInertia(val)) end self.inertia = self.entity:getInertia().y @@ -41,8 +41,17 @@ function Wheel:initialize(config, prefix) 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() @@ -54,9 +63,14 @@ function Wheel:forwardStep(torque, inertia) return 0 end - --self.entity:setInertia(self.entity:getInertia():setY(inertia)) - self.entity:applyTorque(torque * self.direction * self.entity:getRight()) - self.angularVelocity = self.entity:getAngleVelocity().y * self.direction * TICK_INTERVAL + local initialAngularVelocity = self.angularVelocity - return self.angularVelocity * self.inertia + --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 diff --git a/koptilnya/libs/constants.txt b/koptilnya/libs/constants.txt index 20abe70..8124b78 100644 --- a/koptilnya/libs/constants.txt +++ b/koptilnya/libs/constants.txt @@ -6,3 +6,4 @@ TICK_INTERVAL = game.getTickInterval() RAD_TO_RPM = 9.5493 RPM_TO_RAD = 0.10472 UNITS_PER_METER = 39.37 +UNITS_TO_METERS = 0.01905 diff --git a/koptilnya/libs/entity.txt b/koptilnya/libs/entity.txt index b7a08db..a6ec779 100644 --- a/koptilnya/libs/entity.txt +++ b/koptilnya/libs/entity.txt @@ -1,3 +1,7 @@ +--@include ./constants.txt + +require('./constants.txt') + function getLocalVelocity(entity) if not entity:isValid() then return @@ -11,7 +15,5 @@ function calculateWheelInertia(entity) return Vector(0) end - local originalInertia = entity:getInertia() - - return Vector(originalInertia) + return entity:getInertia():setY(0.5 * entity:getMass() * math.pow(entity:getModelRadius() * UNITS_TO_METERS, 2)) end