Насрал типами, но еще не до конца

This commit is contained in:
Никита Круглицкий 2025-05-16 07:11:48 +06:00
parent 9b50dbe33d
commit 84f591db59
18 changed files with 1629 additions and 1217 deletions

1
.gitattributes vendored Normal file
View File

@ -0,0 +1 @@
*.txt linguist-language=Lua

2
.gitignore vendored
View File

@ -3,4 +3,4 @@
!/koptilnya !/koptilnya
!README.md !README.md
!.gitignore !.gitignore
!.lua-format !.gitattributes

View File

@ -1,12 +0,0 @@
# Install vs-code extension: koihik.vscode-lua-format
# Then set the config file
column_limit: 280
line_breaks_after_function_body: 2
keep_simple_control_block_one_line: false
keep_simple_function_one_line: false
break_after_functioncall_lp: false
break_before_functioncall_rp: false
use_tab: false
continuation_indent_width: 1

View File

@ -17,19 +17,19 @@ local WheelConfig = {
B = 12.0, B = 12.0,
C = 1.3, C = 1.3,
D = 1.8, D = 1.8,
E = -1.8 E = -1.8,
}, },
LongitudinalFrictionPreset = { LongitudinalFrictionPreset = {
B = 18.0, B = 18.0,
C = 1.5, C = 1.5,
D = 1.5, D = 1.5,
E = 0.3 E = 0.3,
}, },
AligningFrictionPreset = { AligningFrictionPreset = {
B = 2.8, B = 2.8,
C = 2.1, C = 2.1,
D = 0.10, D = 0.10,
E = -2.5 E = -2.5,
}, },
-- AligningFrictionPreset = { -- AligningFrictionPreset = {
-- B = 13, -- B = 13,
@ -38,29 +38,29 @@ local WheelConfig = {
-- E = 0.48 -- E = 0.48
-- } -- }
}, },
Model = 'models/sprops/trans/wheel_a/wheel25.mdl' Model = 'models/sprops/trans/wheel_a/wheel25.mdl',
} }
local FrontWheelsConfig = table.merge( local FrontWheelsConfig = table.merge(table.copy(WheelConfig), {
table.copy(WheelConfig), BrakePower = 1600,
{
CustomWheel = { CustomWheel = {
CasterAngle = 7, CasterAngle = 7,
CamberAngle = -3, CamberAngle = -3,
ToeAngle = 0.5 },
} })
}
)
local RearWheelsConfig = table.merge( local RearWheelsConfig = table.merge(table.copy(WheelConfig), {
table.copy(WheelConfig), BrakePower = 600,
{
HandbrakePower = 2200, HandbrakePower = 2200,
CustomWheel = { CustomWheel = {
CamberAngle = -2, CamberAngle = -2,
ToeAngle = 0.5 -- LongitudinalFrictionPreset = {
} -- B = 0.1,
} -- C = 1,
) -- D = 0.9,
-- E = 0.9,
-- },
},
})
Vehicle:new({ Vehicle:new({
{ {
@ -74,15 +74,109 @@ Vehicle:new({
FrictionCoeff = 0.01, FrictionCoeff = 0.01,
LimiterDuration = 0.06, LimiterDuration = 0.06,
TorqueMap = { TorqueMap = {
118.89918444138, 122.0751393736, 125.25109430583, 128.42704923806, 131.60300417029, 134.77895910251, 137.95491403474, 141.13086896697, 144.3068238992, 147.48277883143, 150.65873376365, 153.83468869588, 157.01064362811, 160.18659856034, 163.36255349256, 118.89918444138,
166.53850842479, 169.71446335702, 172.89041828925, 176.06637322148, 179.2423281537, 182.41828308593, 185.59423801816, 188.77019295039, 191.94614788261, 195.12210281484, 198.29805774707, 201.4740126793, 204.64996761153, 207.82592254375, 211.00187747598, 122.0751393736,
214.17783240821, 217.35378734044, 220.52974227266, 223.70569720489, 226.88165213712, 227.9621903219, 229.04272850669, 230.12326669147, 231.20380487625, 232.28434306104, 233.36488124582, 234.4454194306, 235.52595761538, 236.60649580017, 237.68703398495, 125.25109430583,
238.76757216973, 239.84811035452, 240.9286485393, 242.00918672408, 243.08972490886, 244.17026309365, 245.25080127843, 246.33133946321, 247.411877648, 248.49241583278, 249.57295401756, 250.65349220234, 251.73403038713, 252.81456857191, 253.89510675669, 254.97564494148, 128.42704923806,
256.05618312626, 257.13672131104, 258.21725949582, 259.29779768061, 260.37833586539, 261.29278066787, 262.20722547034, 263.12167027282, 264.0361150753, 264.95055987777, 265.86500468025, 266.77944948273, 267.6938942852, 268.60833908768, 269.52278389016, 131.60300417029,
270.43722869263, 271.35167349511, 272.26611829758, 273.18056310006, 274.09500790254, 275.00945270501, 275.92389750749, 276.83834230997, 275.50329427594, 274.16824624192, 272.8331982079, 271.49815017388, 270.16310213985, 268.82805410583, 267.49300607181, 134.77895910251,
266.15795803778, 264.82291000376, 263.48786196974, 262.15281393572, 260.81776590169, 259.48271786767, 250.23203287321, 240.98134787874, 231.73066288428, 222.47997788981 137.95491403474,
} 141.13086896697,
} 144.3068238992,
147.48277883143,
150.65873376365,
153.83468869588,
157.01064362811,
160.18659856034,
163.36255349256,
166.53850842479,
169.71446335702,
172.89041828925,
176.06637322148,
179.2423281537,
182.41828308593,
185.59423801816,
188.77019295039,
191.94614788261,
195.12210281484,
198.29805774707,
201.4740126793,
204.64996761153,
207.82592254375,
211.00187747598,
214.17783240821,
217.35378734044,
220.52974227266,
223.70569720489,
226.88165213712,
227.9621903219,
229.04272850669,
230.12326669147,
231.20380487625,
232.28434306104,
233.36488124582,
234.4454194306,
235.52595761538,
236.60649580017,
237.68703398495,
238.76757216973,
239.84811035452,
240.9286485393,
242.00918672408,
243.08972490886,
244.17026309365,
245.25080127843,
246.33133946321,
247.411877648,
248.49241583278,
249.57295401756,
250.65349220234,
251.73403038713,
252.81456857191,
253.89510675669,
254.97564494148,
256.05618312626,
257.13672131104,
258.21725949582,
259.29779768061,
260.37833586539,
261.29278066787,
262.20722547034,
263.12167027282,
264.0361150753,
264.95055987777,
265.86500468025,
266.77944948273,
267.6938942852,
268.60833908768,
269.52278389016,
270.43722869263,
271.35167349511,
272.26611829758,
273.18056310006,
274.09500790254,
275.00945270501,
275.92389750749,
276.83834230997,
275.50329427594,
274.16824624192,
272.8331982079,
271.49815017388,
270.16310213985,
268.82805410583,
267.49300607181,
266.15795803778,
264.82291000376,
263.48786196974,
262.15281393572,
260.81776590169,
259.48271786767,
250.23203287321,
240.98134787874,
231.73066288428,
222.47997788981,
},
},
}, },
{ {
Name = 'Clutch', Name = 'Clutch',
@ -90,8 +184,8 @@ Vehicle:new({
Type = POWERTRAIN_COMPONENT.Clutch, Type = POWERTRAIN_COMPONENT.Clutch,
Config = { Config = {
Inertia = 0.002, Inertia = 0.002,
SlipTorque = 1000 SlipTorque = 1000,
} },
}, },
{ {
Name = 'Gearbox', Name = 'Gearbox',
@ -102,12 +196,12 @@ Vehicle:new({
ShiftTime = 3, ShiftTime = 3,
Inertia = 0.01, Inertia = 0.01,
Ratios = { 3.626, 2.200, 1.541, 1.213, 1.000, 0.767 }, Ratios = { 3.626, 2.200, 1.541, 1.213, 1.000, 0.767 },
Reverse = 3.437 Reverse = 3.437,
} },
}, },
{ {
Name = 'AxleFront', Name = 'AxleFront',
Input = 'Axle', -- Input = 'Axle',
Type = POWERTRAIN_COMPONENT.Differential, Type = POWERTRAIN_COMPONENT.Differential,
Config = { Config = {
Type = Differential.TYPES.Open, Type = Differential.TYPES.Open,
@ -118,24 +212,25 @@ Vehicle:new({
PowerRamp = 1, PowerRamp = 1,
Stiffness = 0.1, Stiffness = 0.1,
SlipTorque = 0, SlipTorque = 0,
SteerLock = 45 SteerLock = 50,
} ToeAngle = 0.5,
},
}, },
{ {
Name = 'WheelFL', Name = 'WheelFL',
Type = POWERTRAIN_COMPONENT.Wheel, Type = POWERTRAIN_COMPONENT.Wheel,
Input = 'AxleFront', Input = 'AxleFront',
Config = table.merge(table.copy(FrontWheelsConfig), { Offset = 0 }) Config = table.merge(table.copy(FrontWheelsConfig), { Offset = 0 }),
}, },
{ {
Name = 'WheelFR', Name = 'WheelFR',
Type = POWERTRAIN_COMPONENT.Wheel, Type = POWERTRAIN_COMPONENT.Wheel,
Input = 'AxleFront', Input = 'AxleFront',
Config = table.merge(table.copy(FrontWheelsConfig), { Offset = 180 }) Config = table.merge(table.copy(FrontWheelsConfig), { Offset = 180 }),
}, },
{ {
Name = 'AxleBack', Name = 'AxleBack',
Input = 'Axle', Input = 'Gearbox',
Type = POWERTRAIN_COMPONENT.Differential, Type = POWERTRAIN_COMPONENT.Differential,
Config = { Config = {
Type = Differential.TYPES.Open, Type = Differential.TYPES.Open,
@ -144,13 +239,14 @@ Vehicle:new({
BiasAB = 0.5, BiasAB = 0.5,
CoastRamp = 1, CoastRamp = 1,
PowerRamp = 1, PowerRamp = 1,
Stiffness = 0.1, Stiffness = 0.9,
SlipTorque = 0, SlipTorque = 1000,
} ToeAngle = 0.5,
},
}, },
{ {
Name = 'Axle', Name = 'Axle',
Input = 'Gearbox', -- Input = 'Gearbox',
Type = POWERTRAIN_COMPONENT.Differential, Type = POWERTRAIN_COMPONENT.Differential,
Config = { Config = {
Type = Differential.TYPES.Open, Type = Differential.TYPES.Open,
@ -160,19 +256,19 @@ Vehicle:new({
CoastRamp = 1, CoastRamp = 1,
PowerRamp = 1, PowerRamp = 1,
Stiffness = 1, Stiffness = 1,
SlipTorque = 0 SlipTorque = 1000,
} },
}, },
{ {
Name = 'WheelRL', Name = 'WheelRL',
Input = 'AxleBack', Input = 'AxleBack',
Type = POWERTRAIN_COMPONENT.Wheel, Type = POWERTRAIN_COMPONENT.Wheel,
Config = table.merge(table.copy(RearWheelsConfig), { Offset = 0 }) Config = table.merge(table.copy(RearWheelsConfig), { Offset = 0 }),
}, },
{ {
Name = 'WheelRR', Name = 'WheelRR',
Input = 'AxleBack', Input = 'AxleBack',
Type = POWERTRAIN_COMPONENT.Wheel, Type = POWERTRAIN_COMPONENT.Wheel,
Config = table.merge(table.copy(RearWheelsConfig), { Offset = 180 }) Config = table.merge(table.copy(RearWheelsConfig), { Offset = 180 }),
} },
}) })

View File

@ -1,18 +1,17 @@
--@client --@client
local fontArial92 = render.createFont("Arial", 92, 250, true, false, false, false, 0, false, 0) local fontArial92 = render.createFont('Arial', 92, 250, true, false, false, false, 0, false, 0)
local fontArial46 = render.createFont("Arial", 46, 250, true, false, false, false, 0, false, 0) local fontArial46 = render.createFont('Arial', 46, 250, true, false, false, false, 0, false, 0)
ENGINE_RPM, CAR_SPEED, GEARBOX_GEAR = 0, 123, 0 ENGINE_RPM, CAR_SPEED, GEARBOX_GEAR = 0, 'SPD', 0
local resx, resy = render.getGameResolution() local resx, resy = render.getGameResolution()
local linesmatrix = Matrix() local linesmatrix = Matrix()
local linesposx, linesposy = resx - 200, resy - 150 local linesposx, linesposy = resx - 200, resy - 150
linesmatrix:setTranslation(Vector(linesposx, linesposy, 0)) linesmatrix:setTranslation(Vector(linesposx, linesposy, 0))
linesmatrix:setAngles(Angle(0, 15, 0)) linesmatrix:setAngles(Angle(0, 15, 0))
linesmatrix:setScale(Vector(0.7)) linesmatrix:setScale(Vector(0.7))
hook.add("DrawHud", "CARHUD", function() hook.add('DrawHud', 'CARHUD', function()
render.pushMatrix(linesmatrix) render.pushMatrix(linesmatrix)
render.enableScissorRect(linesposx - 50, linesposy + 30, linesposx + 208, linesposy + 50) render.enableScissorRect(linesposx - 50, linesposy + 30, linesposx + 208, linesposy + 50)
for x = 0, 208, 4 do for x = 0, 208, 4 do
if ENGINE_RPM > x / 220 then if ENGINE_RPM > x / 220 then
col = 55 + x / 220 * 200 col = 55 + x / 220 * 200
render.setRGBA(col, col, col, 250) render.setRGBA(col, col, col, 250)
@ -25,7 +24,6 @@ hook.add("DrawHud", "CARHUD", function()
render.disableScissorRect() render.disableScissorRect()
render.enableScissorRect(linesposx - 50, linesposy + 20, linesposx + 212, linesposy + 50) render.enableScissorRect(linesposx - 50, linesposy + 20, linesposx + 212, linesposy + 50)
for x = 212, 220, 4 do for x = 212, 220, 4 do
if ENGINE_RPM > x / 220 then if ENGINE_RPM > x / 220 then
render.setRGBA(200, 71, 71, 200) render.setRGBA(200, 71, 71, 200)
else else
@ -42,42 +40,42 @@ hook.add("DrawHud", "CARHUD", function()
render.setRGBA(151, 151, 151, 220) render.setRGBA(151, 151, 151, 220)
render.setFont(fontArial92) render.setFont(fontArial92)
local str = string.rep("0", 3 - #tostring(CAR_SPEED)) .. CAR_SPEED local str = string.rep('0', 3 - #tostring(CAR_SPEED)) .. CAR_SPEED
render.setRGBA(51, 51, 51, 256) render.setRGBA(51, 51, 51, 256)
for k = 1, 3 do for k = 1, 3 do
local num = string.sub(str, k, k) local num = string.sub(str, k, k)
if num ~= "0" then if num ~= '0' then
render.setRGBA(255, 255, 255, 250) render.setRGBA(255, 255, 255, 250)
end end
render.drawText(linesposx - 60 + k * 46, resy - 130 - 80, num) render.drawText(linesposx - 60 + k * 46, resy - 130 - 80, num)
end end
render.setFont(fontArial46) render.setFont(fontArial46)
render.setRGBA(200, 51, 51, 256) render.setRGBA(200, 51, 51, 256)
local t = "N" local t = 'N'
if GEARBOX_GEAR == -1 then if GEARBOX_GEAR == -1 then
t = "R" t = 'R'
elseif GEARBOX_GEAR == 0 then elseif GEARBOX_GEAR == 0 then
t = "N" t = 'N'
else else
t = GEARBOX_GEAR t = GEARBOX_GEAR
end end
render.drawText(linesposx - 35 + 164, resy - 130 - 40, t) render.drawText(linesposx - 35 + 164, resy - 130 - 40, t)
end) end)
net.receive("ENGINE_RPM", function() net.receive('ENGINE_RPM', function()
local rpm = net.readFloat() local rpm = net.readFloat()
if rpm then if rpm then
ENGINE_RPM = rpm ENGINE_RPM = rpm
end end
end) end)
net.receive("CAR_SPEED", function() net.receive('CAR_SPEED', function()
local speed = net.readUInt(12) local speed = net.readUInt(12)
if speed then if speed then
CAR_SPEED = math.clamp(speed, 0, 999) CAR_SPEED = math.clamp(speed, 0, 999)
end end
end) end)
net.receive("GEARBOX_GEAR", function() net.receive('GEARBOX_GEAR', function()
local gear = net.readInt(5) local gear = net.readInt(5)
if gear then if gear then
GEARBOX_GEAR = gear GEARBOX_GEAR = gear

View File

@ -5,34 +5,48 @@ local PowertrainComponent = require('./powertrain_component.txt')
require('/koptilnya/libs/constants.txt') require('/koptilnya/libs/constants.txt')
---@class ClutchConfig: PowertrainComponentConfig
---@field SlipTorque? number
---@class Clutch: PowertrainComponent
---@field slipTorque number
local Clutch = class('Clutch', PowertrainComponent) local Clutch = class('Clutch', PowertrainComponent)
---@private
---@param vehicle KPTLVehicle
---@param name string
---@param config ClutchConfig
function Clutch:initialize(vehicle, name, config) function Clutch:initialize(vehicle, name, config)
PowertrainComponent.initialize(self, vehicle, name, config) PowertrainComponent.initialize(self, vehicle, name, config)
if CLIENT then return end if CLIENT then
return
end
self.wireInputs = { self.wireInputs = {
Clutch = 'number' Clutch = 'number',
} }
self.wireOutputs = { self.wireOutputs = {
Clutch_Torque = 'number' Clutch_Torque = 'number',
} }
self.inertia = config.Inertia or 0.1 self.inertia = config.Inertia or 0.1
self.slipTorque = config.SlipTorque or 1000 self.slipTorque = config.SlipTorque or 1000
end end
---@return nil
function Clutch:updateWireOutputs() function Clutch:updateWireOutputs()
PowertrainComponent.updateWireOutputs(self) PowertrainComponent.updateWireOutputs(self)
wire.ports.Clutch_Torque = self.torque wire.ports.Clutch_Torque = self.torque
end end
---@return number
function Clutch:getPress() function Clutch:getPress()
return 1 - wire.ports.Clutch return 1 - wire.ports.Clutch
end end
---@return number
function Clutch:queryInertia() function Clutch:queryInertia()
if self.output == nil then if self.output == nil then
return self.inertia return self.inertia
@ -41,6 +55,8 @@ function Clutch:queryInertia()
return self.inertia + self.output:queryInertia() * self:getPress() return self.inertia + self.output:queryInertia() * self:getPress()
end end
---@param angularVelocity number
---@return number
function Clutch:queryAngularVelocity(angularVelocity) function Clutch:queryAngularVelocity(angularVelocity)
self.angularVelocity = angularVelocity self.angularVelocity = angularVelocity
@ -54,6 +70,9 @@ function Clutch:queryAngularVelocity(angularVelocity)
return outputW + inputW return outputW + inputW
end end
---@param torque number
---@param inertia number
---@return number
function Clutch:forwardStep(torque, inertia) function Clutch:forwardStep(torque, inertia)
if self.output == nil then if self.output == nil then
return torque return torque

View File

@ -2,23 +2,53 @@
--@include ./powertrain_component.txt --@include ./powertrain_component.txt
local PowertrainComponent = require('./powertrain_component.txt') local PowertrainComponent = require('./powertrain_component.txt')
local WheelComponent = require('./wheel.txt')
require('/koptilnya/libs/constants.txt') require('/koptilnya/libs/constants.txt')
---@alias DifferentialSplitStrategyFn fun(tq: number, aW: number, bW: number, aI: number, bI: number, biasAB: number, preload: number, stiffness: number, powerRamp: number, coastRamp: number, slipTorque: number): number, number
---@class DifferentialConfig: PowertrainComponentConfig
---@field Type? string
---@field FinalDrive? number
---@field Bias? number
---@field CoastRamp? number
---@field PowerRamp? number
---@field Preload? number
---@field Stiffness? number
---@field SlipTorque? number
---@field SteerLock? number
---@field ToeAngle? number
---@class Differential: PowertrainComponent
---@field finalDrive number
---@field biasAB number
---@field coastRamp number
---@field powerRamp number
---@field preload number
---@field stiffness number
---@field slipTorque number
---@field private splitStrategy DifferentialSplitStrategyFn
---@field steerLock number
---@field toeAngle number
---@field steerAngle number
---@field private mzDiff number
local Differential = class('Differential', PowertrainComponent) local Differential = class('Differential', PowertrainComponent)
function Differential.getSplitStrategy(type) ---@private
return SPLIT_STRATEGIES[type] or SPLIT_STRATEGIES[Differential.TYPES.Open] ---@param vehicle KPTLVehicle
end ---@param name string
---@param config DifferentialConfig
function Differential:initialize(vehicle, name, config) function Differential:initialize(vehicle, name, config)
PowertrainComponent.initialize(self, vehicle, name, config) PowertrainComponent.initialize(self, vehicle, name, config)
if CLIENT then return end if CLIENT then
return
end
self.wireOutputs = { self.wireOutputs = {
[string.format('%s_Torque', self.name)] = 'number' [string.format('%s_Torque', self.name)] = 'number',
[string.format('%s_W', self.name)] = 'number',
[string.format('%s_MzDiff', self.name)] = 'number',
} }
self.finalDrive = config.FinalDrive or 4 self.finalDrive = config.FinalDrive or 4
@ -29,13 +59,19 @@ function Differential:initialize(vehicle, name, config)
self.preload = config.Preload or 10 self.preload = config.Preload or 10
self.stiffness = config.Stiffness or 0.1 self.stiffness = config.Stiffness or 0.1
self.slipTorque = config.SlipTorque or 1000 self.slipTorque = config.SlipTorque or 1000
self.splitStrategy = Differential.getSplitStrategy(config.Type or Differential.TYPES.Open) self.splitStrategy = Differential.getSplitStrategy(config.Type or DIFFERENTIAL_TYPES.Open)
self.steerLock = config.SteerLock or 0 self.steerLock = config.SteerLock or 0
self.toeAngle = config.ToeAngle or 0
self.steerAngle = 0 self.steerAngle = 0
self.mzDiff = 0
end end
---@param component PowertrainComponent
---@return nil
function Differential:linkComponent(component) function Differential:linkComponent(component)
---@diagnostic disable-next-line: undefined-field
if not component:isInstanceOf(PowertrainComponent) then if not component:isInstanceOf(PowertrainComponent) then
return return
end end
@ -49,10 +85,13 @@ function Differential:linkComponent(component)
end end
end end
---@return nil
function Differential:updateWireOutputs() function Differential:updateWireOutputs()
PowertrainComponent.updateWireOutputs(self) PowertrainComponent.updateWireOutputs(self)
wire.ports[string.format('%s_Torque', self.name)] = self.torque wire.ports[string.format('%s_Torque', self.name)] = self.torque
wire.ports[string.format('%s_W', self.name)] = self.angularVelocity
wire.ports[string.format('%s_MzDiff', self.name)] = self.mzDiff
if self.outputA ~= nil then if self.outputA ~= nil then
self.outputA:updateWireOutputs() self.outputA:updateWireOutputs()
@ -63,6 +102,7 @@ function Differential:updateWireOutputs()
end end
end end
---@return number
function Differential:queryInertia() function Differential:queryInertia()
if self.outputA == nil or self.outputB == nil then if self.outputA == nil or self.outputB == nil then
return self.inertia return self.inertia
@ -71,6 +111,7 @@ function Differential:queryInertia()
return self.inertia + (self.outputA:queryInertia() + self.outputB:queryInertia()) / math.pow(self.finalDrive, 2) return self.inertia + (self.outputA:queryInertia() + self.outputB:queryInertia()) / math.pow(self.finalDrive, 2)
end end
---@return number
function Differential:queryAngularVelocity(angularVelocity) function Differential:queryAngularVelocity(angularVelocity)
self.angularVelocity = angularVelocity self.angularVelocity = angularVelocity
@ -84,6 +125,9 @@ function Differential:queryAngularVelocity(angularVelocity)
return (aW + bW) * self.finalDrive * 0.5 return (aW + bW) * self.finalDrive * 0.5
end end
---@param torque number
---@param inertia number
---@return number
function Differential:forwardStep(torque, inertia) function Differential:forwardStep(torque, inertia)
if self.outputA == nil or self.outputB == nil then if self.outputA == nil or self.outputB == nil then
return torque return torque
@ -96,7 +140,7 @@ function Differential:forwardStep(torque, inertia)
self.torque = torque * self.finalDrive self.torque = torque * self.finalDrive
local tqA, tqB = self:splitStrategy( local tqA, tqB = self.splitStrategy(
self.torque, self.torque,
aW, aW,
bW, bW,
@ -115,36 +159,31 @@ function Differential:forwardStep(torque, inertia)
-- // REFACTOR -- // REFACTOR
if self.steerLock ~= 0 then if self.steerLock ~= 0 then
local outputA = self.outputA --[[@as Wheel]]
local outputB = self.outputB --[[@as Wheel]]
local steerInertia = (aI + bI) / 2 local steerInertia = (aI + bI) / 2
local driverInput = 5 local driverInput = 15
local localVelocityLength = chip():getVelocity():getLength() local localVelocityLength = chip():getVelocity():getLength()
local MPH = localVelocityLength * (15 / 352) local MPH = localVelocityLength * (15 / 352)
local KPH = MPH * 1.609 local KPH = MPH * 1.609
local assist = math.clamp(10.0 / math.sqrt(KPH / 3), 2.0, 10.0) local assist = math.clamp(10.0 / math.sqrt(KPH / 3), 2.0, 10.0)
local inputForce = driverInput * assist
local maxSteerSpeed = math.rad(1337) local inputForce = driverInput * assist
local inputTorque = self.vehicle.steer * inputForce local inputTorque = self.vehicle.steer * inputForce
local avgSteerAngle = (self.outputA.customWheel.steerAngle + self.outputB.customWheel.steerAngle) / 2 local mzDiff = outputA.customWheel.mz - outputB.customWheel.mz
local mz = self.outputA.customWheel.mz + self.outputB.customWheel.mz
local mzDiff = self.outputA.customWheel.mz - self.outputB.customWheel.mz
local avgMz = (self.outputA.customWheel.mz + self.outputB.customWheel.mz) / 2
local maxMz = math.max(self.outputA.customWheel.mz + self.outputB.customWheel.mz)
local minMz = math.min(self.outputA.customWheel.mz + self.outputB.customWheel.mz)
local steerTorque = mzDiff / 2 * -1 + inputTorque self.mzDiff = mzDiff
local steerTorque = mzDiff * -1 + inputTorque
local steerAngularAccel = steerTorque / steerInertia local steerAngularAccel = steerTorque / steerInertia
self.steerAngle = math.clamp( self.steerAngle = math.clamp(self.steerAngle + steerAngularAccel * TICK_INTERVAL, -self.steerLock, self.steerLock)
self.steerAngle + steerAngularAccel * TICK_INTERVAL,
-self.steerLock,
self.steerLock
)
local wheelbase = 2.05 local wheelbase = 2.05
local trackWidth = 1.124 local trackWidth = 1.124
@ -153,27 +192,35 @@ function Differential:forwardStep(torque, inertia)
local innerAngle = math.deg(math.atan(wheelbase / (radius - (trackWidth / 2)))) local innerAngle = math.deg(math.atan(wheelbase / (radius - (trackWidth / 2))))
local outerAngle = math.deg(math.atan(wheelbase / (radius + (trackWidth / 2)))) local outerAngle = math.deg(math.atan(wheelbase / (radius + (trackWidth / 2))))
self.outputA.customWheel.steerAngle = outerAngle outputA.customWheel.steerAngle = outerAngle + self.toeAngle * outputA.customWheel.direction
self.outputB.customWheel.steerAngle = innerAngle outputB.customWheel.steerAngle = innerAngle + self.toeAngle * outputB.customWheel.direction
else
local outputA = self.outputA --[[@as Wheel]]
local outputB = self.outputB --[[@as Wheel]]
outputA.customWheel.steerAngle = self.toeAngle * outputA.customWheel.direction
outputB.customWheel.steerAngle = self.toeAngle * outputB.customWheel.direction
end end
return tqA + tqB return tqA + tqB
end end
function Differential:_openDiffTorqueSplit(tq, aW, bW, aI, bI, biasAB, preload, stiffness, powerRamp, coastRamp, slipTorque) ---@return number, number
return tq * (1 - biasAB), tq * biasAB; local function _openDiffTorqueSplit(tq, aW, bW, aI, bI, biasAB, preload, stiffness, powerRamp, coastRamp, slipTorque)
return tq * (1 - biasAB), tq * biasAB
end end
function Differential:_lockingDiffTorqueSplit(tq, aW, bW, aI, bI, biasAB, preload, stiffness, powerRamp, coastRamp, slipTorque) ---@return number, number
Ta = tq * (1 - biasAB); local function _lockingDiffTorqueSplit(tq, aW, bW, aI, bI, biasAB, preload, stiffness, powerRamp, coastRamp, slipTorque)
Tb = tq * biasAB; aTq = tq * 0.5
bTq = tq * 0.5
local syncTorque = (aW - bW) * stiffness * (aI + bI) * 0.5 / TICK_INTERVAL; local syncTorque = (aW - bW) * stiffness * (aI + bI) * 0.5 / TICK_INTERVAL
Ta = Ta - syncTorque; aTq = aTq - syncTorque
Tb = Tb + syncTorque; bTq = bTq + syncTorque
return Ta, Tb return aTq, bTq
-- local sumI = aI + bI -- local sumI = aI + bI
-- local w = aI / sumI * aW + bI / sumI * bW -- local w = aI / sumI * aW + bI / sumI * bW
-- local aTqCorr = (w - aW) * aI -- / dt -- local aTqCorr = (w - aW) * aI -- / dt
@ -187,7 +234,8 @@ function Differential:_lockingDiffTorqueSplit(tq, aW, bW, aI, bI, biasAB, preloa
-- return tq * biasA + aTqCorr, tq * (1 - biasA) * bTqCorr -- return tq * biasA + aTqCorr, tq * (1 - biasA) * bTqCorr
end end
function Differential:_VLSDTorqueSplit(tq, aW, bW, aI, bI, biasAB, preload, stiffness, powerRamp, coastRamp, slipTorque) ---@return number, number
local function _VLSDTorqueSplit(tq, aW, bW, aI, bI, biasAB, preload, stiffness, powerRamp, coastRamp, slipTorque)
if aW < 0 or bW < 0 then if aW < 0 or bW < 0 then
return tq * (1 - biasAB), tq * biasAB return tq * (1 - biasAB), tq * biasAB
end end
@ -203,7 +251,8 @@ function Differential:_VLSDTorqueSplit(tq, aW, bW, aI, bI, biasAB, preload, stif
return tq * (1 - biasAB) - dTq, tq * biasAB + dTq return tq * (1 - biasAB) - dTq, tq * biasAB + dTq
end end
function Differential:_HLSDTorqueSplit(tq, aW, bW, aI, bI, biasAB, preload, stiffness, powerRamp, coastRamp, slipTorque) ---@return number, number
local function _HLSDTorqueSplit(tq, aW, bW, aI, bI, biasAB, preload, stiffness, powerRamp, coastRamp, slipTorque)
if aW < 0 or bW < 0 then if aW < 0 or bW < 0 then
return tq * (1 - biasAB), tq * biasAB return tq * (1 - biasAB), tq * biasAB
end end
@ -220,18 +269,28 @@ function Differential:_HLSDTorqueSplit(tq, aW, bW, aI, bI, biasAB, preload, stif
return tq * (1 - biasAB) - dTq, tq * biasAB + dTq return tq * (1 - biasAB) - dTq, tq * biasAB + dTq
end end
Differential.TYPES = { ---@enum DIFFERENTIAL_TYPE
DIFFERENTIAL_TYPES = {
Open = 'Open', Open = 'Open',
Locking = 'Locking', Locking = 'Locking',
VLSD = 'VLSD', VLSD = 'VLSD',
HLSD = 'HLSD', HLSD = 'HLSD',
} }
SPLIT_STRATEGIES = { ---@type { [DIFFERENTIAL_TYPE]: DifferentialSplitStrategyFn }
[Differential.TYPES.Open] = Differential._openDiffTorqueSplit, local SPLIT_STRATEGIES = {
[Differential.TYPES.Locking] = Differential._lockingDiffTorqueSplit, [DIFFERENTIAL_TYPES.Open] = _openDiffTorqueSplit,
[Differential.TYPES.VLSD] = Differential._VLSDTorqueSplit, [DIFFERENTIAL_TYPES.Locking] = _lockingDiffTorqueSplit,
[Differential.TYPES.HLSD] = Differential._HLSDTorqueSplit, [DIFFERENTIAL_TYPES.VLSD] = _VLSDTorqueSplit,
[DIFFERENTIAL_TYPES.HLSD] = _HLSDTorqueSplit,
} }
---@param type DIFFERENTIAL_TYPE
---@return DifferentialSplitStrategyFn
Differential.getSplitStrategy = function(type)
return SPLIT_STRATEGIES[type] or SPLIT_STRATEGIES[DIFFERENTIAL_TYPES.Open]
end
Differential.TYPES = DIFFERENTIAL_TYPES
return Differential return Differential

View File

@ -3,17 +3,40 @@
local PowertrainComponent = require('./powertrain_component.txt') local PowertrainComponent = require('./powertrain_component.txt')
require('/koptilnya/libs/constants.txt') require('/koptilnya/libs/constants.txt')
---@class EngineConfig: PowertrainComponentConfig
---@field IdleRPM? number
---@field MaxRPM? number
---@field StartFriction? number
---@field FrictionCoeff? number
---@field LimiterDuration? number
---@field TorqueMap? number[]
---@class Engine: PowertrainComponent
---@field idleRPM number
---@field maxRPM number
---@field startFriction number
---@field frictionCoeff number
---@field limiterDuration number
---@field torqueMap number[]
---@field friction number
---@field masterThrottle number
---@field finalTorque number
---@field reactTorque number
---@field returnedTorque number
---@field private limiterTimer number
local Engine = class('Engine', PowertrainComponent) local Engine = class('Engine', PowertrainComponent)
---@private
---@param vehicle KPTLVehicle
---@param name string
---@param config EngineConfig
function Engine:initialize(vehicle, name, config) function Engine:initialize(vehicle, name, config)
PowertrainComponent.initialize(self, vehicle, name, config) PowertrainComponent.initialize(self, vehicle, name, config)
self.wireInputs = { self.wireInputs = {
Throttle = 'number' Throttle = 'number',
} }
self.wireOutputs = { self.wireOutputs = {
Engine_RPM = 'number', Engine_RPM = 'number',
@ -21,7 +44,7 @@ function Engine:initialize(vehicle, name, config)
Engine_MasterThrottle = 'number', Engine_MasterThrottle = 'number',
Engine_ReactTorque = 'number', Engine_ReactTorque = 'number',
Engine_ReturnedTorque = 'number', Engine_ReturnedTorque = 'number',
Engine_FinalTorque = 'number' Engine_FinalTorque = 'number',
} }
self.idleRPM = config.IdleRPM or 900 self.idleRPM = config.IdleRPM or 900
@ -37,17 +60,19 @@ function Engine:initialize(vehicle, name, config)
self.limiterTimer = 0 self.limiterTimer = 0
self.finalTorque = 0
self.reactTorque = 0 self.reactTorque = 0
self.returnedTorque = 0 self.returnedTorque = 0
if CLIENT then if CLIENT then
--@include /koptilnya/engine_sound_2.txt -- --@include /koptilnya/engine_sound_2.txt
local Sound = require('/koptilnya/engine_sound_2.txt') -- local Sound = require('/koptilnya/engine_sound_2.txt')
Sound(config.MaxRPM or 7000, chip()) -- Sound(self.maxRPM, chip())
end end
end end
---@return nil
function Engine:updateWireOutputs() function Engine:updateWireOutputs()
PowertrainComponent.updateWireOutputs(self) PowertrainComponent.updateWireOutputs(self)
@ -59,11 +84,14 @@ function Engine:updateWireOutputs()
wire.ports.Engine_FinalTorque = self.finalTorque wire.ports.Engine_FinalTorque = self.finalTorque
end end
---@return number
function Engine:getThrottle() function Engine:getThrottle()
return wire.ports.Throttle return wire.ports.Throttle
end end
function Engine:_generateTorque() ---@private
---@return number
function Engine:generateTorque()
local throttle = self:getThrottle() local throttle = self:getThrottle()
local rpm = self:getRPM() local rpm = self:getRPM()
@ -92,12 +120,17 @@ function Engine:_generateTorque()
return self.torque return self.torque
end end
---@param torque number
---@param inertia number
---@return number
function Engine:forwardStep(torque, inertia) function Engine:forwardStep(torque, inertia)
if self.output == nil then if self.output == nil then
local generatedTorque = self:_generateTorque() local generatedTorque = self:generateTorque()
self.angularVelocity = self.angularVelocity + generatedTorque / self.inertia * TICK_INTERVAL self.angularVelocity = self.angularVelocity + generatedTorque / self.inertia * TICK_INTERVAL
self.angularVelocity = math.max(self.angularVelocity, 0) self.angularVelocity = math.max(self.angularVelocity, 0)
return 0
end end
local outputInertia = self.output:queryInertia() local outputInertia = self.output:queryInertia()
@ -105,9 +138,9 @@ function Engine:forwardStep(torque, inertia)
local outputW = self.output:queryAngularVelocity(self.angularVelocity) local outputW = self.output:queryAngularVelocity(self.angularVelocity)
local targetW = self.inertia / inertiaSum * self.angularVelocity + outputInertia / inertiaSum * outputW local targetW = self.inertia / inertiaSum * self.angularVelocity + outputInertia / inertiaSum * outputW
local generatedTorque = self:_generateTorque() local generatedTorque = self:generateTorque()
local reactTorque = (targetW - self.angularVelocity) * self.inertia / TICK_INTERVAL local reactTorque = (targetW - self.angularVelocity) * self.inertia / TICK_INTERVAL
local returnedTorque = self.output:forwardStep(torque + generatedTorque - reactTorque, inertia + self.inertia) local returnedTorque = self.output:forwardStep(generatedTorque - reactTorque, 0)
self.reactTorque = reactTorque self.reactTorque = reactTorque
self.returnedTorque = returnedTorque self.returnedTorque = returnedTorque
@ -119,14 +152,16 @@ function Engine:forwardStep(torque, inertia)
self.angularVelocity = self.angularVelocity + finalTorque / inertiaSum * TICK_INTERVAL self.angularVelocity = self.angularVelocity + finalTorque / inertiaSum * TICK_INTERVAL
self.angularVelocity = math.max(self.angularVelocity, 0) self.angularVelocity = math.max(self.angularVelocity, 0)
-- net.start("ENGINE_RPM") -- net.start('ENGINE_RPM')
-- net.writeFloat(self:getRPM() / self.maxRPM) -- net.writeFloat(self:getRPM() / self.maxRPM)
-- net.send(self.vehicle.playersConnectedToHUD, true) -- net.send(self.vehicle.playersConnectedToHUD, true)
-- net.start("ENGINE_FULLRPM") -- net.start('ENGINE_FULLRPM')
-- net.writeUInt(self:getRPM(), 16) -- net.writeUInt(self:getRPM(), 16)
-- net.writeFloat(self.masterThrottle) -- net.writeFloat(self.masterThrottle)
-- net.send(nil, true) -- net.send(nil, true)
return 0
end end
return Engine return Engine

View File

@ -7,16 +7,18 @@ local Gearbox = class('Gearbox', PowertrainComponent)
function Gearbox:initialize(vehicle, name, config) function Gearbox:initialize(vehicle, name, config)
PowertrainComponent.initialize(self, vehicle, name, config) PowertrainComponent.initialize(self, vehicle, name, config)
if CLIENT then return end if CLIENT then
return
end
self.wireInputs = { self.wireInputs = {
Upshift = 'number', Upshift = 'number',
Downshift = 'number' Downshift = 'number',
} }
self.wireOutputs = { self.wireOutputs = {
[string.format('%s_RPM', self.name)] = 'number', [string.format('%s_RPM', self.name)] = 'number',
[string.format('%s_Torque', self.name)] = 'number', [string.format('%s_Torque', self.name)] = 'number',
[string.format('%s_Ratio', self.name)] = 'number' [string.format('%s_Ratio', self.name)] = 'number',
} }
self.type = config.Type or 'MANUAL' self.type = config.Type or 'MANUAL'

View File

@ -12,19 +12,23 @@ local ManualGearbox = class('ManualGearbox', BaseGearbox)
function ManualGearbox:initialize(vehicle, name, config) function ManualGearbox:initialize(vehicle, name, config)
BaseGearbox.initialize(self, vehicle, name, config) BaseGearbox.initialize(self, vehicle, name, config)
if CLIENT then return end if CLIENT then
return
end
table.merge(self.wireOutputs, { table.merge(self.wireOutputs, {
[string.format('%s_Gear', self.name)] = 'number' [string.format('%s_Gear', self.name)] = 'number',
}) })
self.ratios = config.Ratios or { 3.6, 2.2, 1.5, 1.2, 1.0, 0.8} self.ratios = config.Ratios or { 3.6, 2.2, 1.5, 1.2, 1.0, 0.8 }
self.reverse = config.Reverse or 3.4 self.reverse = config.Reverse or 3.4
self.gear = 0 self.gear = 0
function shiftFunc() function shiftFunc()
if wire.ports.Clutch == 0 then return 0 end if wire.ports.Clutch == 0 then
return 0
end
local upshift = wire.ports.Upshift or 0 local upshift = wire.ports.Upshift or 0
local downshift = wire.ports.Downshift or 0 local downshift = wire.ports.Downshift or 0

View File

@ -1,14 +1,35 @@
--@include /koptilnya/libs/constants.txt --@include /koptilnya/libs/constants.txt
--@include /koptilnya/libs/utils.txt --@include /koptilnya/libs/utils.txt
--@include ../wire_component.txt --@include ../wire_component.txt
--@include ../vehicle.txt
local WireComponent = require('../wire_component.txt') local WireComponent = require('../wire_component.txt')
require('/koptilnya/libs/constants.txt') require('/koptilnya/libs/constants.txt')
require('/koptilnya/libs/utils.txt') require('/koptilnya/libs/utils.txt')
---@class PowertrainComponentConfig
---@field Name? string
---@field DEBUG? boolean
---@field Inertia? number
---@class PowertrainComponent: KPTLWireComponent
---@field vehicle KPTLVehicle
---@field name string
---@field CONFIG PowertrainComponentConfig
---@field DEBUG boolean
---@field input? PowertrainComponent
---@field output? PowertrainComponent
---@field inertia number
---@field angularVelocity number
---@field torque number
---@field DEBUG_DATA? table
local PowertrainComponent = class('PowertrainComponent', WireComponent) local PowertrainComponent = class('PowertrainComponent', WireComponent)
---@protected
---@param vehicle KPTLVehicle
---@param name? string
---@param config? PowertrainComponentConfig
function PowertrainComponent:initialize(vehicle, name, config) function PowertrainComponent:initialize(vehicle, name, config)
config = config or {} config = config or {}
@ -20,12 +41,10 @@ function PowertrainComponent:initialize(vehicle, name, config)
self.DEBUG = config.DEBUG or false self.DEBUG = config.DEBUG or false
self.input = nil self.input = nil
self.output = nil self.output = nil
self.inertia = 0.02 self.inertia = 0.02
self.angularVelocity = 0 self.angularVelocity = 0
self.torque = 0 self.torque = 0
if self.DEBUG then if self.DEBUG then
if CLIENT then if CLIENT then
self.DEBUG_DATA = {} self.DEBUG_DATA = {}
@ -36,17 +55,17 @@ function PowertrainComponent:initialize(vehicle, name, config)
end end
self.DEBUG_SEND_DATA_DEBOUNCED = debounce(function() self.DEBUG_SEND_DATA_DEBOUNCED = debounce(function()
net.start("DEBUG_" .. self.name) net.start('DEBUG_' .. self.name)
self:serializeDebugData() self:serializeDebugData()
net.send(self.vehicle.playersConnectedToHUD, true) net.send(self.vehicle.playersConnectedToHUD, true)
end, 1 / 10) end, 1 / 10)
end end
end end
function PowertrainComponent:start() ---@param component PowertrainComponent
end ---@return nil
function PowertrainComponent:linkComponent(component) function PowertrainComponent:linkComponent(component)
---@diagnostic disable-next-line: undefined-field
if not component:isInstanceOf(PowertrainComponent) then if not component:isInstanceOf(PowertrainComponent) then
return return
end end
@ -57,10 +76,12 @@ function PowertrainComponent:linkComponent(component)
end end
end end
---@return number
function PowertrainComponent:getRPM() function PowertrainComponent:getRPM()
return self.angularVelocity * RAD_TO_RPM return self.angularVelocity * RAD_TO_RPM
end end
---@return number
function PowertrainComponent:queryInertia() function PowertrainComponent:queryInertia()
if self.output == nil then if self.output == nil then
return self.inertia return self.inertia
@ -69,6 +90,8 @@ function PowertrainComponent:queryInertia()
return self.inertia + self.output:queryInertia() return self.inertia + self.output:queryInertia()
end end
---@param angularVelocity number
---@return number
function PowertrainComponent:queryAngularVelocity(angularVelocity) function PowertrainComponent:queryAngularVelocity(angularVelocity)
self.angularVelocity = angularVelocity self.angularVelocity = angularVelocity
@ -79,6 +102,9 @@ function PowertrainComponent:queryAngularVelocity(angularVelocity)
return self.output:queryAngularVelocity(angularVelocity) return self.output:queryAngularVelocity(angularVelocity)
end end
---@param torque number
---@param inertia number
---@return number
function PowertrainComponent:forwardStep(torque, inertia) function PowertrainComponent:forwardStep(torque, inertia)
if self.output == nil then if self.output == nil then
return self.torque return self.torque
@ -87,6 +113,7 @@ function PowertrainComponent:forwardStep(torque, inertia)
return self.output:forwardStep(self.torque, self.inertia + inertia) return self.output:forwardStep(self.torque, self.inertia + inertia)
end end
---@return nil
function PowertrainComponent:updateWireOutputs() function PowertrainComponent:updateWireOutputs()
WireComponent.updateWireOutputs(self) WireComponent.updateWireOutputs(self)
@ -95,10 +122,11 @@ function PowertrainComponent:updateWireOutputs()
end end
end end
function PowertrainComponent:serializeDebugData() ---@return nil
end function PowertrainComponent:serializeDebugData() end
function PowertrainComponent:deserializeDebugData(result) ---@param result table
end ---@return nil
function PowertrainComponent:deserializeDebugData(result) end
return PowertrainComponent return PowertrainComponent

View File

@ -9,26 +9,48 @@ local CustomWheel = require('../wheel/wheel.txt')
require('/koptilnya/libs/constants.txt') require('/koptilnya/libs/constants.txt')
require('/koptilnya/libs/entity.txt') require('/koptilnya/libs/entity.txt')
---@class WheelConfig: PowertrainComponentConfig
---@field Entity? Entity
---@field BrakePower? number
---@field HandbrakePower? number
---@field Model? string
---@field Offset? number In degrees
---@field CustomWheel? CustomWheelConfig
---@class Wheel: PowertrainComponent
---@field entity Entity
---@field brakePower number
---@field handbrakePower number
---@field direction integer
---@field private rot number
---@field model string
---@field holo Hologram | Entity
---@field customWheel CustomWheel
local Wheel = class('Wheel', PowertrainComponent) local Wheel = class('Wheel', PowertrainComponent)
local DEBUG = true ---@private
---@param vehicle KPTLVehicle
---@param name string
---@param config WheelConfig
function Wheel:initialize(vehicle, name, config) function Wheel:initialize(vehicle, name, config)
PowertrainComponent.initialize(self, vehicle, name, config) PowertrainComponent.initialize(self, vehicle, name, config)
if CLIENT and self.DEBUG then if CLIENT and self.DEBUG then
local scale = 0.1
local font = render.createFont("Roboto", 256, 400, true)
local mat = render.createMaterial('models/debug/debugwhite') local mat = render.createMaterial('models/debug/debugwhite')
local lerpLoad = 0 local lerpLoad = 0
local lerpForwardFrictionForce = 0 local lerpForwardFrictionForce = 0
local lerpSideFrictionForce = 0 local lerpSideFrictionForce = 0
hook.add("PostDrawTranslucentRenderables", "DEBUG_RENDER_" .. self.name, function()
if next(self.DEBUG_DATA) == nil then return end
if not isValid(self.DEBUG_DATA.entity) then return end
local pos = self.DEBUG_DATA.entity:getPos() hook.add('PostDrawTranslucentRenderables', 'DEBUG_RENDER_' .. self.name, function()
if next(self.DEBUG_DATA) == nil then
return
end
if not isValid(self.DEBUG_DATA.entity) then
return
end
local pos = self.DEBUG_DATA.entity:localToWorld(Vector(0, 0, -self.DEBUG_DATA.radius * UNITS_PER_METER))
render.setMaterial(mat) render.setMaterial(mat)
render.setColor(Color(255, 0, 0, 200)) render.setColor(Color(255, 0, 0, 200))
@ -41,39 +63,38 @@ function Wheel:initialize(vehicle, name, config)
render.setColor(Color(0, 0, 255, 200)) render.setColor(Color(0, 0, 255, 200))
lerpLoad = math.lerp(0.1, lerpLoad, self.DEBUG_DATA.load) lerpLoad = math.lerp(0.1, lerpLoad, self.DEBUG_DATA.load)
render.draw3DBeam(pos, pos + ((lerpLoad * 4 + 16) * self.DEBUG_DATA.up), 1, 0, 0) render.draw3DBeam(pos, pos + ((lerpLoad / 1000 * 4 + 16) * self.DEBUG_DATA.up), 1, 0, 0)
end) end)
if player() == owner() and not render.isHUDActive() then if player() == owner() and not render.isHUDActive() then
enableHud(nil, true) enableHud(owner(), true)
end end
return return
end end
self.steerLock = config.SteerLock or 0 self.entity = config.Entity or NULL_ENTITY
self.brakePower = config.BrakePower or 0 self.brakePower = config.BrakePower or 0
self.handbrakePower = config.HandbrakePower or 0 self.handbrakePower = config.HandbrakePower or 0
self.model = config.Model or ''
self.direction = math.sign(math.cos(math.rad(config.Offset or 0))) self.direction = math.sign(math.cos(math.rad(config.Offset or 0)))
self.wireInputs = { self.wireInputs = {
[self.name] = 'entity' [self.name] = 'entity',
} }
self.wireOutputs = { self.wireOutputs = {
[string.format('%s_RPM', self.name)] = 'number', [string.format('%s_RPM', self.name)] = 'number',
[string.format('%s_W', self.name)] = 'number' [string.format('%s_Fx', self.name)] = 'number',
[string.format('%s_CTq', self.name)] = 'number',
} }
self.rot = 0 self.rot = 0
self.entity = config.Entity or NULL_ENTITY
self.holo = self:createHolo(self.entity) self.holo = self:createHolo(self.entity)
self.customWheel = CustomWheel:new(table.merge(table.copy(config.CustomWheel), { Direction = self.direction })) self.customWheel = CustomWheel:new(table.merge(table.copy(config.CustomWheel), { Direction = self.direction, Name = self.name }))
hook.add('input', 'vehicle_wheel_update' .. self.name, function(name, value) hook.add('Input', 'vehicle_wheel_update' .. self.name, function(name, value)
if name == self.name then if name == self.name then
self.entity = value self.entity = value
self.customWheel:setEntity(value) self.customWheel:setEntity(value)
@ -83,11 +104,8 @@ function Wheel:initialize(vehicle, name, config)
end end
self.holo = self:createHolo(self.entity) self.holo = self:createHolo(self.entity)
if not config.Radius then
self.customWheel.radius = self:getEntityRadius() self.customWheel.radius = self:getEntityRadius()
end end
end
end) end)
self.steerVelocity = 0 self.steerVelocity = 0
@ -101,39 +119,47 @@ function Wheel:getEntityRadius()
return self.entity:getModelRadius() * UNITS_TO_METERS return self.entity:getModelRadius() * UNITS_TO_METERS
end end
---@return Hologram | Entity
function Wheel:createHolo(entity) function Wheel:createHolo(entity)
if not isValid(entity) then if not isValid(entity) then
return NULL_ENTITY return NULL_ENTITY
end end
local holo = holograms.create( local holo = hologram.create(entity:getPos(), entity:getAngles(), self.model or '')
entity:getPos(),
entity:getAngles(), if holo == nil or isValid(holo) then
self.CONFIG.Model or '' return NULL_ENTITY
) end
holo:setParent(entity) holo:setParent(entity)
entity:setColor(Color(0,0,0,0)) entity:setColor(Color(0, 0, 0, 0))
return holo return holo
end end
---@return nil
function Wheel:updateWireOutputs() function Wheel:updateWireOutputs()
PowertrainComponent.updateWireOutputs(self) PowertrainComponent.updateWireOutputs(self)
wire.ports[string.format('%s_RPM', self.name)] = self.customWheel:getRPM() wire.ports[string.format('%s_RPM', self.name)] = self.customWheel:getRPM()
wire.ports[string.format('%s_W', self.name)] = self.customWheel.angularVelocity wire.ports[string.format('%s_Fx', self.name)] = self.customWheel.forwardFriction.force
wire.ports[string.format('%s_CTq', self.name)] = self.customWheel.counterTorque
end end
---@return number
function Wheel:queryInertia() function Wheel:queryInertia()
return self.customWheel.inertia return self.customWheel.inertia
end end
---@return number
function Wheel:queryAngularVelocity() function Wheel:queryAngularVelocity()
return self.angularVelocity return self.angularVelocity
end end
---@param torque number
---@param inertia number
---@return number
function Wheel:forwardStep(torque, inertia) function Wheel:forwardStep(torque, inertia)
if not isValid(self.customWheel) then if not isValid(self.customWheel) then
return 0 return 0
@ -147,28 +173,38 @@ function Wheel:forwardStep(torque, inertia)
self.angularVelocity = self.customWheel.angularVelocity self.angularVelocity = self.customWheel.angularVelocity
if self.customWheel.hasHit and isValid(self.vehicle.basePhysObject) then if self.customWheel.hasHit and isValid(self.vehicle.basePhysObject) then
local surfaceForceVector = self.customWheel.right * self.customWheel.sideFriction.force + self.customWheel.forward * self.customWheel.forwardFriction.force local surfaceForceVector = self.customWheel.right * self.customWheel.sideFriction.force
+ self.customWheel.forward * self.customWheel.forwardFriction.force
self.vehicle.basePhysObject:applyForceOffset(surfaceForceVector, self.entity:localToWorld(Vector(0, 0, -self.entity:getModelRadius()))) self.vehicle.basePhysObject:applyForceOffset(
surfaceForceVector,
self.entity:localToWorld(Vector(0, 0, -self.customWheel.radius * UNITS_PER_METER))
)
end end
if isValid(self.holo) then if isValid(self.holo) then
local entityAngles = self.entity:getAngles() -- Step 1: Update rotational state
self.rot = self.rot + self.angularVelocity * TICK_INTERVAL * self.direction
self.rot = self.rot % (2 * math.pi)
self.rot = self.rot + self.angularVelocity * TICK_INTERVAL -- Step 2: Apply spin (rolling) to forward and up directions
local spunFwd = self.customWheel.forward:rotateAroundAxis(self.customWheel.right, nil, self.rot)
local spunUp = self.customWheel.up:rotateAroundAxis(self.customWheel.right, nil, self.rot)
local steerRotated = entityAngles:rotateAroundAxis(self.customWheel.up, nil, math.rad(-self.customWheel.steerAngle) + self.customWheel.toeAngle) -- Step 3: Get quaternion from spun directions
local camberRotated = steerRotated:rotateAroundAxis(self.customWheel.forward, nil, -self.customWheel.camberAngle * self.direction) local visualQuat = spunFwd:getQuaternion(spunUp)
local angularVelocityRotated = camberRotated:rotateAroundAxis(self.customWheel.right, nil, self.rot)
self.holo:setAngles(angularVelocityRotated) -- Step 4: Apply orientation to holo
self.holo:setAngles(visualQuat:getEulerAngle())
end end
return self.customWheel.counterTorque return self.customWheel.counterTorque
end end
---@return nil
function Wheel:serializeDebugData() function Wheel:serializeDebugData()
net.writeEntity(self.entity) net.writeEntity(self.entity)
net.writeFloat(self.customWheel.radius)
net.writeVector(self.customWheel.forward) net.writeVector(self.customWheel.forward)
net.writeVector(self.customWheel.right) net.writeVector(self.customWheel.right)
net.writeVector(self.customWheel.up) net.writeVector(self.customWheel.up)
@ -177,10 +213,13 @@ function Wheel:serializeDebugData()
net.writeFloat(self.customWheel.sideFriction.force) net.writeFloat(self.customWheel.sideFriction.force)
end end
---@param result table
---@return nil
function Wheel:deserializeDebugData(result) function Wheel:deserializeDebugData(result)
net.readEntity(function(ent) net.readEntity(function(ent)
result.entity = ent result.entity = ent
end) end)
result.radius = net.readFloat()
result.forward = net.readVector() result.forward = net.readVector()
result.right = net.readVector() result.right = net.readVector()
result.up = net.readVector() result.up = net.readVector()

View File

@ -6,42 +6,53 @@
local Task = require('/libs/task.txt') local Task = require('/libs/task.txt')
local PowertrainComponentFactory = require('./factories/powertrain_component.txt') local PowertrainComponentFactory = require('./factories/powertrain_component.txt')
local CustomWheel = require('./wheel/wheel.txt')
local POWERTRAIN_COMPONENT = require('./enums/powertrain_component.txt') local POWERTRAIN_COMPONENT = require('./enums/powertrain_component.txt')
require('/koptilnya/libs/table.txt') require('/koptilnya/libs/table.txt')
require('/koptilnya/libs/constants.txt') require('/koptilnya/libs/constants.txt')
---@class KPTLVehicle
---@field config { [string]: any }
---@field components PowertrainComponent[]
---@field headComponents PowertrainComponent[]
---@field steer number
---@field brake number
---@field handbrake number
---@field playersConnectedToHUD Player[]
---@field base? Entity
---@field basePhysObject? PhysObj
---@field initialized boolean
local Vehicle = class('Vehicle') local Vehicle = class('Vehicle')
---@param config { [string]: any }
function Vehicle:initialize(config) function Vehicle:initialize(config)
if not Vehicle:validateConfig(config) then if not Vehicle:validateConfig(config) then
throw('Please check your vehicle configuration!') throw('Please check your vehicle configuration!')
end end
self.initialized = false
self.config = config self.config = config
self.components = {} self.components = {}
self.headComponents = {} self.headComponents = {}
self:createComponents()
self:linkComponents()
if SERVER then
self:createIO()
end
if SERVER then
self.steer = 0 self.steer = 0
self.brake = 0 self.brake = 0
self.handbrake = 0 self.handbrake = 0
self.playersConnectedToHUD = {} self.playersConnectedToHUD = {}
hook.add('input', 'vehicle_wire_input', function(name, value) if SERVER then
self.playersConnectedToHUD = find.allPlayers(function(ply)
return isValid(ply) and ply:isHUDActive()
end)
hook.add('Input', 'vehicle_wire_input', function(name, value)
self:handleWireInput(name, value) self:handleWireInput(name, value)
end) end)
hook.add('tick', 'vehicle_update', function() ---@diagnostic disable-next-line: param-type-mismatch
hook.add('Tick', 'vehicle_update', function()
self:update() self:update()
end) end)
@ -49,26 +60,65 @@ function Vehicle:initialize(config)
table.insert(self.playersConnectedToHUD, ply) table.insert(self.playersConnectedToHUD, ply)
end) end)
hook.add('HUDDisconnected', 'vehicle_huddisconnected', function(ent, ply) hook.add('HUDDisconnected', 'vehicle_huddisconnected', function(ent, ply)
table.removeByValue(self.playersConnectedToHUD, ply) table.removeByValue(self.playersConnectedToHUD, ply)
end) end)
hook.add('PlayerDisconnected', 'vehicle_huddisconnected', function(ply)
table.removeByValue(self.playersConnectedToHUD, ply)
end)
else else
--@include ./hud.txt --@include ./hud.txt
require("./hud.txt") require('./hud.txt')
net.receive('VEHICLE_READY', function()
self:start()
end)
end end
end end
function Vehicle.static:validateConfig(config) ---@return nil
function Vehicle:start()
self:createComponents()
self:linkComponents()
if SERVER then
self:createIO()
end
self.initialized = true
print('VEHICLE READY')
if SERVER then
net.start('VEHICLE_READY')
net.send(find.allPlayers(), true)
end
end
---@param config any
---@return boolean
function Vehicle:validateConfig(config)
return type(config) == 'table' return type(config) == 'table'
end end
---@param name string
---@return PowertrainComponent
function Vehicle:getComponentByName(name) function Vehicle:getComponentByName(name)
return table.find(self.components, function(component) return table.find(self.components, function(component)
return component.name == name return component.name == name
end) end)
end end
-- ---@param type string
-- ---@return PowertrainComponent[]
-- function Vehicle:getComponentsByType(type)
-- return table.find(self.components, function(component)
-- return component.name == name
-- end)
-- end
---@return nil
function Vehicle:createComponents() function Vehicle:createComponents()
for _, componentConfig in pairs(self.config) do for _, componentConfig in pairs(self.config) do
local component = PowertrainComponentFactory:create(self, componentConfig.Type, componentConfig.Name, componentConfig.Config) local component = PowertrainComponentFactory:create(self, componentConfig.Type, componentConfig.Name, componentConfig.Config)
@ -77,6 +127,7 @@ function Vehicle:createComponents()
end end
end end
---@return nil
function Vehicle:linkComponents() function Vehicle:linkComponents()
for _, componentConfig in pairs(self.config) do for _, componentConfig in pairs(self.config) do
local component = self:getComponentByName(componentConfig.Name) local component = self:getComponentByName(componentConfig.Name)
@ -91,14 +142,21 @@ function Vehicle:linkComponents()
end end
end end
end end
if SERVER then
for _, component in pairs(self.headComponents) do
self:printComponentTree(component)
end
end
end end
---@return nil
function Vehicle:createIO() function Vehicle:createIO()
local inputs = { local inputs = {
Base = 'entity', Base = 'entity',
Steer = 'number', Steer = 'number',
Brake = 'number', Brake = 'number',
Handbrake = 'number' Handbrake = 'number',
} }
local outputs = {} local outputs = {}
@ -110,12 +168,7 @@ function Vehicle:createIO()
wire.adjustPorts(inputs, outputs) wire.adjustPorts(inputs, outputs)
end end
function Vehicle:getRootComponent() ---@return nil
return table.find(self.components, function(component)
return component.input == nil
end)
end
function Vehicle:handleWireInput(name, value) function Vehicle:handleWireInput(name, value)
if name == 'Base' then if name == 'Base' then
self.base = value self.base = value
@ -127,8 +180,13 @@ function Vehicle:handleWireInput(name, value)
elseif name == 'Handbrake' then elseif name == 'Handbrake' then
self.handbrake = value self.handbrake = value
end end
if not self.initialized and self.base ~= nil and self.basePhysObject ~= nil then
self:start()
end
end end
---@return nil
function Vehicle:update() function Vehicle:update()
if SERVER then if SERVER then
local base = wire.ports.Base local base = wire.ports.Base
@ -154,7 +212,21 @@ function Vehicle:update()
--end --end
end end
---@param component PowertrainComponent
---@param result? string
---@return nil
function Vehicle:printComponentTree(component, result)
-- result = result or component.name
-- if component.output then
-- result = result .. component.name
-- return self:printComponentTree(component.output, result .. ' -> ')
-- else
-- print(result)
-- end
end
return { return {
Vehicle, Vehicle,
POWERTRAIN_COMPONENT POWERTRAIN_COMPONENT,
} }

View File

@ -1,26 +1,34 @@
--@include ./friction.txt --@include ./friction.txt
--@include ./friction_preset.txt --@include ./friction_preset.txt
--@include /koptilnya/libs/constants.txt --@include /koptilnya/libs/constants.txt
--@include /koptilnya/libs/utils.txt
local Friction = require('./friction.txt') local Friction = require('./friction.txt')
local FrictionPreset = require('./friction_preset.txt') local FrictionPreset = require('./friction_preset.txt')
require('/koptilnya/libs/constants.txt') require('/koptilnya/libs/constants.txt')
require('/koptilnya/libs/utils.txt')
---@class CustomWheelConfig
---@field Name? string
---@field Direction? integer
---@class CustomWheel
local Wheel = class('Wheel') local Wheel = class('Wheel')
function Wheel:initialize(config) function Wheel:initialize(config)
config = config or {} config = config or {}
self.name = config.Name
self.direction = config.Direction or 1 self.direction = config.Direction or 1
self.mass = config.Mass or 20 self.mass = config.Mass or 20
self.radius = config.Radius or 0.27 self.radius = config.Radius or 0.27
self.rollingResistance = config.RollingResistance or 20 self.rollingResistance = config.RollingResistance or 20
self.squat = config.Squat or 0.1 self.squat = config.Squat or 0.1
self.slipCircleShape = config.SlipCircleShape or 1.05 self.slipCircleShape = config.SlipCircleShape or 1.05
self.casterAngle = math.rad(config.CasterAngle or 0) * self.direction self.casterAngle = math.rad(config.CasterAngle or 0)
self.camberAngle = math.rad(config.CamberAngle or 0) self.camberAngle = math.rad(config.CamberAngle or 0)
self.toeAngle = math.rad(config.ToeAngle or 0) * -self.direction self.toeAngle = math.rad(config.ToeAngle or 0)
self.forwardFriction = Friction:new(config.ForwardFriction) self.forwardFriction = Friction:new(config.ForwardFriction)
self.sideFriction = Friction:new(config.SideFriction) self.sideFriction = Friction:new(config.SideFriction)
@ -46,6 +54,10 @@ function Wheel:initialize(config)
self.physObj = nil self.physObj = nil
self.baseInertia = 0.5 * self.mass * math.pow(self.radius, 2) self.baseInertia = 0.5 * self.mass * math.pow(self.radius, 2)
self.inertia = self.baseInertia self.inertia = self.baseInertia
self.printDebounced = debounce(function(...)
print(...)
end, 1)
end end
function Wheel:setEntity(entity) function Wheel:setEntity(entity)
@ -80,25 +92,26 @@ function Wheel:getRPM()
end end
function Wheel:getLongitudinalLoadCoefficient(load) function Wheel:getLongitudinalLoadCoefficient(load)
return 11000 * (1 - math.exp(-0.00014 * load)); return 11000 * (1 - math.exp(-0.00014 * load))
end end
function Wheel:getLateralLoadCoefficient(load) function Wheel:getLateralLoadCoefficient(load)
return 18000 * (1 - math.exp(-0.0001 * load)); return 18000 * (1 - math.exp(-0.0001 * load))
end end
function Wheel:stepLongitudinal(Tm, Tb, Vx, W, Lc, R, I, kFx, kSx) function Wheel:stepLongitudinal(Tm, Tb, Vx, W, Lc, R, I)
local Winit = W local Winit = W
local VxAbs = math.abs(Vx) local VxAbs = math.abs(Vx)
local Sx = 0 local Sx = 0
if VxAbs >= 0.1 then if Lc / 1000 < 0.01 then
Sx = (Vx - W * R) / VxAbs Sx = 0
elseif VxAbs >= 0.01 then
Sx = (W * R - Vx) / VxAbs
else else
Sx = (Vx - W * R) * 0.6 Sx = (W * R - Vx) * 0.6
end end
Sx = Sx * kSx;
Sx = math.clamp(Sx, -1, 1) Sx = math.clamp(Sx, -1, 1)
W = W + Tm / I * TICK_INTERVAL W = W + Tm / I * TICK_INTERVAL
@ -110,10 +123,15 @@ function Wheel:stepLongitudinal(Tm, Tb, Vx, W, Lc, R, I, kFx, kSx)
Tb = math.clamp(Tb, -TbCap, TbCap) Tb = math.clamp(Tb, -TbCap, TbCap)
W = W + Tb / I * TICK_INTERVAL W = W + Tb / I * TICK_INTERVAL
local maxTorque = self.longitudinalFrictionPreset:evaluate(math.abs(Sx)) * Lc * kFx * R local maxTorque = self.longitudinalFrictionPreset:evaluate(math.abs(Sx)) * Lc * R
local errorTorque = (W - Vx / R) * I / TICK_INTERVAL local errorTorque = (W - Vx / R) * I / TICK_INTERVAL
local surfaceTorque = math.clamp(errorTorque, -maxTorque, maxTorque) local surfaceTorque = math.clamp(errorTorque, -maxTorque, maxTorque)
if self.name == 'WheelFL' or self.name == 'WheelFR' then
surfaceTorque = surfaceTorque
end
W = W - surfaceTorque / I * TICK_INTERVAL W = W - surfaceTorque / I * TICK_INTERVAL
local Fx = surfaceTorque / R local Fx = surfaceTorque / R
@ -125,32 +143,78 @@ function Wheel:stepLongitudinal(Tm, Tb, Vx, W, Lc, R, I, kFx, kSx)
local deltaOmegaTorque = (W - Winit) * I / TICK_INTERVAL local deltaOmegaTorque = (W - Winit) * I / TICK_INTERVAL
local Tcnt = -surfaceTorque + Tb + Tb2 - deltaOmegaTorque local Tcnt = -surfaceTorque + Tb + Tb2 - deltaOmegaTorque
if Lc < 0.001 then -- self.printDebounced(
Sx = 0 -- Color(255, 255, 0),
end -- string.format('[%s] ', self.name),
-- Color(255, 255, 255),
-- string.format('%s | %s | %s', math.round(Fx), math.round(W), math.round(R))
-- )
return W, Sx, Fx, Tcnt return W, Sx, Fx, Tcnt
end end
function Wheel:stepLateral(Vx, Vy, Lc, kFy, kSy) function Wheel:stepLongitudinal2(Tm, Tb, Vx, W, Lc, R, I)
-- Параметры Pacejka (примерные значения, можно калибровать под задачу)
local B = 10.0 -- stiffness factor
local C = 1.9 -- shape factor
local D = Lc -- peak factor (максимальное сцепление зависит от нагрузки)
local E = 0.97 -- curvature factor
-- Угловая скорость колеса и радиус
local wheelSpeed = W * R
-- Slip ratio (проверка на деление на 0)
local slip
if math.abs(Vx) < 0.1 then
slip = 0.0
else
slip = (wheelSpeed - Vx) / math.abs(Vx)
end
-- Формула Pacejka для расчета силы тяги (longitudinal force)
local Fx = D * math.sin(C * math.atan(B * slip - E * (B * slip - math.atan(B * slip))))
-- Добавим моторный момент и торможение, сопротивление качению
local driveForce = Tm / R
local brakeForce = Tb / R
local rollingResistanceTorque = self.rollingResistance * R
local tractionTorque = Fx * R
-- Суммарный момент, действующий на колесо
local netTorque = Tm - Tb - rollingResistanceTorque - tractionTorque
-- Угловое ускорение
local angularAcceleration = netTorque / I
-- Обновляем угловую скорость
local newAngularVelocity = W + angularAcceleration * TICK_INTERVAL
-- Суммарная продольная сила (на автомобиль)
local totalLongitudinalForce = Fx + (Tm - Tb - rollingResistanceTorque) / R
-- print(newAngularVelocity, slip, totalLongitudinalForce, -tractionTorque)
return newAngularVelocity, slip, totalLongitudinalForce, tractionTorque
end
function Wheel:stepLateral(Vx, Vy, Lc)
local VxAbs = math.abs(Vx) local VxAbs = math.abs(Vx)
local Sy = 0 local Sy = 0
if VxAbs > 0.1 then if Lc / 1000 < 0.01 then
Sy = 0
elseif VxAbs > 0.1 then
Sy = math.deg(math.atan(Vy / VxAbs)) Sy = math.deg(math.atan(Vy / VxAbs))
Sy = Sy / 50 Sy = Sy / 50
else else
Sy = Vy * (0.003 / TICK_INTERVAL) Sy = Vy * (0.003 / TICK_INTERVAL)
end end
Sy = Sy * kSy * 0.95 Sy = Sy -- * 0.95
Sy = math.clamp(Sy, -1, 1) Sy = math.clamp(Sy, -1, 1)
local slipSign = Sy < 0 and -1 or 1 local slipSign = Sy < 0 and -1 or 1
local Fy = -slipSign * self.lateralFrictionPreset:evaluate(math.abs(Sy)) * Lc * kFy local Fy = -slipSign * self.lateralFrictionPreset:evaluate(math.abs(Sy)) * Lc
if Lc < 0.0001 then
Sy = 0
end
return Sy, Fy return Sy, Fy
end end
@ -161,10 +225,7 @@ function Wheel:slipCircle(Sx, Sy, Fx, Fy, slipCircleShape)
if SxAbs > 0.01 then if SxAbs > 0.01 then
local SxClamped = math.clamp(Sx, -1, 1) local SxClamped = math.clamp(Sx, -1, 1)
local SyClamped = math.clamp(Sy, -1, 1) local SyClamped = math.clamp(Sy, -1, 1)
local combinedSlip = Vector2( local combinedSlip = Vector2(SxClamped * slipCircleShape, SyClamped)
SxClamped * slipCircleShape,
SyClamped
)
local slipDir = combinedSlip:getNormalized() local slipDir = combinedSlip:getNormalized()
local F = math.sqrt(Fx * Fx + Fy * Fy) local F = math.sqrt(Fx * Fx + Fy * Fy)
@ -186,32 +247,53 @@ function Wheel:selfAligningTorque(Sy, load)
local D = self.aligningFrictionPreset.D local D = self.aligningFrictionPreset.D
local E = self.aligningFrictionPreset.E local E = self.aligningFrictionPreset.E
local loadScale = load * 1000 local entityWheelDown = self.entity:localToWorld(Vector(0, 0, -self.radius))
local mechanicalTrail = 0.15 local physWheelDown = self.entity:localToWorld(-self.up * self.radius)
local casterEffect = math.tan(self.casterAngle) local mechanicalTrail = entityWheelDown:getDistance(physWheelDown) * 10
local effectiveTrail = mechanicalTrail + casterEffect * self.radius
local D_scaled = D * loadScale * effectiveTrail
local camberEffect = 1 + 0.5 * math.abs(self.camberAngle) local casterEffect = math.tan(self.casterAngle) * self.direction
local toeEffect = 1 + 0.3 * math.abs(self.toeAngle) local camberTrailEffect = self.camberAngle * 0.005 * self.direction
-- local toeSyOffset = math.tan(self.toeAngle * self.direction) * 0.1
local effectiveTrail = mechanicalTrail * self.radius
local D_scaled = D * load * effectiveTrail
-- Sy = Sy + toeSyOffset
local term = B * Sy - E * (B * Sy - math.atan(B * Sy)) local term = B * Sy - E * (B * Sy - math.atan(B * Sy))
local Mz = D_scaled * camberEffect * toeEffect * math.sin(C * math.atan(term)) local Mz = D_scaled * math.sin(C * math.atan(term))
return Mz return Mz * self.direction -- / TICK_INTERVAL
end end
function Wheel:rotateVector(vector, jopa) function Wheel:rotateAxes()
local ang = self.entity:getAngles() local ang = self.entity:getAngles()
local baseForward = ang:getForward()
local baseUp = ang:getUp()
local baseRight = -ang:getRight()
local steerRotated = vector:rotateAroundAxis(baseUp, nil, math.rad(-self.steerAngle) + self.toeAngle) -- Base directions
local camberRotated = steerRotated:rotateAroundAxis(baseForward, nil, -self.camberAngle) local forward = ang:getForward() * self.direction
local casterRotated = camberRotated:rotateAroundAxis(baseRight, nil, -self.casterAngle) local up = ang:getUp()
local right = -ang:getRight() * self.direction
return casterRotated:getNormalized() -- Step 1: Steer + Toe (rotate forward/right around up)
local steerAngle = -self.steerAngle
forward = forward:rotateAroundAxis(up, steerAngle)
right = right:rotateAroundAxis(up, steerAngle)
-- Step 2: Caster (rotate forward/up around right)
local casterAngle = -self.casterAngle -- * self.direction
forward = forward:rotateAroundAxis(right, nil, casterAngle)
up = up:rotateAroundAxis(right, nil, casterAngle)
-- Step 3: Camber (rotate up/right around forward)
local camberAngle = -self.camberAngle * self.direction
up = up:rotateAroundAxis(forward, nil, camberAngle)
right = right:rotateAroundAxis(forward, nil, camberAngle)
-- Normalize final vectors
self.forward = forward:getNormalized()
self.right = right:getNormalized() * self.direction
self.up = up:getNormalized()
end end
function Wheel:update() function Wheel:update()
@ -223,19 +305,12 @@ function Wheel:update()
local velocity = self.physObj:getVelocity() * UNITS_TO_METERS local velocity = self.physObj:getVelocity() * UNITS_TO_METERS
self.hasHit = externalStress ~= 0 self.hasHit = externalStress ~= 0
self.load = externalStress * KG_TO_KN self.load = externalStress * KG_TO_KN * 1000
self.longitudinalLoadCoefficient = self:getLongitudinalLoadCoefficient(self.load * 1000) self.longitudinalLoadCoefficient = self:getLongitudinalLoadCoefficient(self.load)
self.lateralLoadCoefficient = self:getLateralLoadCoefficient(self.load * 1000) self.lateralLoadCoefficient = self:getLateralLoadCoefficient(self.load)
local ang = self.entity:getAngles() self:rotateAxes()
local baseForward = ang:getForward() * self.direction
local baseUp = ang:getUp()
local baseRight = -ang:getRight() * self.direction
self.forward = self:rotateVector(baseForward)
self.right = self:rotateVector(baseRight)
self.up = self:rotateVector(baseUp)
local forwardSpeed = 0 local forwardSpeed = 0
local sideSpeed = 0 local sideSpeed = 0
@ -252,32 +327,17 @@ function Wheel:update()
self.angularVelocity, self.angularVelocity,
self.longitudinalLoadCoefficient, self.longitudinalLoadCoefficient,
self.radius, self.radius,
self.inertia, self.inertia
0.95,
0.9
) )
local Sy, Fy = self:stepLateral( local Sy, Fy = self:stepLateral(forwardSpeed, sideSpeed, self.lateralLoadCoefficient)
forwardSpeed,
sideSpeed,
self.lateralLoadCoefficient,
0.95,
0.9
) Sx, Sy, Fx, Fy = self:slipCircle(Sx, Sy, Fx, Fy, 1.05)
Sx, Sy, Fx, Fy = self:slipCircle(
Sx,
Sy,
Fx,
Fy,
1.05
)
self.mz = self:selfAligningTorque(Sy, self.load) self.mz = self:selfAligningTorque(Sy, self.load)
self.angularVelocity = W self.angularVelocity = W
self.counterTorque = CounterTq self.counterTorque = CounterTq * self.direction
self.forwardFriction.slip = Sx self.forwardFriction.slip = Sx
self.forwardFriction.force = Fx self.forwardFriction.force = Fx
self.forwardFriction.speed = forwardSpeed self.forwardFriction.speed = forwardSpeed

View File

@ -1,13 +1,25 @@
---@alias KPTLWirePortType
---| "number"
---| "normal"
---| "string"
---| "entity"
---@class KPTLWireComponent
---@field wireInputs { [string]: KPTLWirePortType }
---@field wireOutputs { [string]: KPTLWirePortType }
local WireComponent = class('WireComponent') local WireComponent = class('WireComponent')
---@return nil
function WireComponent:initialize() function WireComponent:initialize()
if CLIENT then return end if CLIENT then
return
end
self.wireInputs = {} self.wireInputs = {}
self.wireOutputs = {} self.wireOutputs = {}
end end
function WireComponent:updateWireOutputs() ---@return nil
end function WireComponent:updateWireOutputs() end
return WireComponent return WireComponent

View File

@ -2,7 +2,7 @@
--@include /libs/task.txt --@include /libs/task.txt
local Task = require('/libs/task.txt') local Task = require('/libs/task.txt')
local Sound = class("Sound") local Sound = class('Sound')
local function map(x, a, b, c, d) local function map(x, a, b, c, d)
return (x - a) / (b - a) * (d - c) + c return (x - a) / (b - a) * (d - c) + c
@ -21,12 +21,13 @@ local function fade(n, min, mid, max)
end end
function Sound:initialize(redline, parent, sounds) function Sound:initialize(redline, parent, sounds)
local sounds = sounds or { local sounds = sounds
[900] = "https://raw.githubusercontent.com/koptilnya/gmod-data/main/engine_sounds/bmw_s54/ext_e30s54_idle.ogg", or {
[2500] = "https://raw.githubusercontent.com/koptilnya/gmod-data/main/engine_sounds/bmw_s54/ext_e30s54_on_2500.ogg", [900] = 'https://raw.githubusercontent.com/koptilnya/gmod-data/main/engine_sounds/bmw_s54/ext_e30s54_idle.ogg',
[4000] = "https://raw.githubusercontent.com/koptilnya/gmod-data/main/engine_sounds/bmw_s54/ext_e30s54_on_4000.ogg", [2500] = 'https://raw.githubusercontent.com/koptilnya/gmod-data/main/engine_sounds/bmw_s54/ext_e30s54_on_2500.ogg',
[6750] = "https://raw.githubusercontent.com/koptilnya/gmod-data/main/engine_sounds/bmw_s54/ext_e30s54_on_6750.ogg", [4000] = 'https://raw.githubusercontent.com/koptilnya/gmod-data/main/engine_sounds/bmw_s54/ext_e30s54_on_4000.ogg',
[8500] = "https://raw.githubusercontent.com/koptilnya/gmod-data/main/engine_sounds/bmw_s54/ext_e30s54_on_8500.ogg" [6750] = 'https://raw.githubusercontent.com/koptilnya/gmod-data/main/engine_sounds/bmw_s54/ext_e30s54_on_6750.ogg',
[8500] = 'https://raw.githubusercontent.com/koptilnya/gmod-data/main/engine_sounds/bmw_s54/ext_e30s54_on_8500.ogg',
} }
local redline = redline or 7000 local redline = redline or 7000
self.active = false self.active = false
@ -40,9 +41,9 @@ function Sound:initialize(redline, parent, sounds)
Task.run(function() Task.run(function()
for soundRpm, soundPath in pairs(sounds) do for soundRpm, soundPath in pairs(sounds) do
local sound = await* soundLoad(soundPath, "3d noblock noplay") local sound = await * soundLoad(soundPath, '3d noblock noplay')
soundObjects[soundRpm] = sound soundObjects[soundRpm] = sound
table.insert(soundRpms,soundRpm) table.insert(soundRpms, soundRpm)
if maxValue < soundRpm then if maxValue < soundRpm then
maxValue = soundRpm maxValue = soundRpm
end end
@ -50,7 +51,7 @@ function Sound:initialize(redline, parent, sounds)
table.sort(soundRpms) table.sort(soundRpms)
hook.add("think", table.address({}), function() hook.add('think', table.address({}), function()
if not self.active then if not self.active then
return return
end end
@ -77,7 +78,7 @@ function Sound:initialize(redline, parent, sounds)
end) end)
end) end)
net.receive("ENGINE_FULLRPM", function() net.receive('ENGINE_FULLRPM', function()
local rpm = net.readUInt(16) local rpm = net.readUInt(16)
engineRpm = rpm * (maxValue / redline) engineRpm = rpm * (maxValue / redline)
throttle = math.max(net.readFloat(), 0) throttle = math.max(net.readFloat(), 0)
@ -85,5 +86,3 @@ function Sound:initialize(redline, parent, sounds)
end end
return Sound return Sound

View File

@ -1,13 +1,13 @@
--@name koptilnya/libs/constants --@name koptilnya/libs/constants
NULL_ENTITY = entity(0) _G.NULL_ENTITY = entity(0)
CURRENT_PLAYER = player() _G.CURRENT_PLAYER = player()
OWNER = owner() _G.OWNER = owner()
IS_ME = CURRENT_PLAYER == OWNER _G.IS_ME = CURRENT_PLAYER == OWNER
TICK_INTERVAL = game.getTickInterval() _G.TICK_INTERVAL = game.getTickInterval()
RAD_TO_RPM = 9.5493 _G.RAD_TO_RPM = 9.5493
RPM_TO_RAD = 0.10472 _G.RPM_TO_RAD = 0.10472
UNITS_PER_METER = 39.37 _G.UNITS_PER_METER = 39.37
UNITS_TO_METERS = 0.01905 _G.UNITS_TO_METERS = 0.01905
KG_TO_N = 9.80665 _G.KG_TO_N = 9.80665
KG_TO_KN = 0.00980665 _G.KG_TO_KN = 0.00980665

View File

@ -1,16 +1,16 @@
-- @name koptilnya/libs/utils -- @name koptilnya/libs/utils
function checkVarClass(var, class, message) function checkVarClass(var, class, message)
if type(var) ~= "table" or var["class"] == nil or not var:isInstanceOf(class) then if type(var) ~= 'table' or var['class'] == nil or not var:isInstanceOf(class) then
throw(message == nil and "Wrong variable class." or message, 1, true) throw(message == nil and 'Wrong variable class.' or message, 1, true)
end end
end end
function accessorFunc(tbl, varName, name, defaultValue) function accessorFunc(tbl, varName, name, defaultValue)
tbl[varName] = defaultValue tbl[varName] = defaultValue
tbl["get" .. name] = function(self) tbl['get' .. name] = function(self)
return self[varName] return self[varName]
end end
tbl["set" .. name] = function(self, value) tbl['set' .. name] = function(self, value)
self[varName] = value self[varName] = value
end end
end end
@ -25,23 +25,23 @@ function rotateAround(entity, pivot, angles)
end end
function tobase(number, base) function tobase(number, base)
local ret = "" local ret = ''
if base < 2 or base > 36 or number == 0 then if base < 2 or base > 36 or number == 0 then
return "0" return '0'
end end
if base == 10 then if base == 10 then
return tostring(number) return tostring(number)
end end
local chars = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ" local chars = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ'
local loops = 0 local loops = 0
while number > 0 do while number > 0 do
loops = loops + 1 loops = loops + 1
number, d = math.floor(number / base), (number % base) + 1 number, d = math.floor(number / base), (number % base) + 1
ret = string.sub(chars, d, d) .. ret ret = string.sub(chars, d, d) .. ret
if (loops > 32000) then if loops > 32000 then
break break
end end
end end
@ -49,7 +49,7 @@ function tobase(number, base)
end end
function bytesToHex(bytes) function bytesToHex(bytes)
local hex = "0x" local hex = '0x'
for _, v in pairs(bytes) do for _, v in pairs(bytes) do
hex = hex .. bit.tohex(v, 2) hex = hex .. bit.tohex(v, 2)
@ -69,9 +69,9 @@ function byteTable(str, start, length)
end end
function isURL(str) function isURL(str)
local _1, _2, prefix = str:find("^(%w-):") local _1, _2, prefix = str:find('^(%w-):')
return prefix == "http" or prefix == "https" or prefix == "data" return prefix == 'http' or prefix == 'https' or prefix == 'data'
end end
function debounce(func, delay) function debounce(func, delay)