Skip to content

Commit

Permalink
Merge pull request #39 from StyledStrike/move-wheel-params-data
Browse files Browse the repository at this point in the history
Wheel parameters refactor
  • Loading branch information
StyledStrike authored Jan 2, 2025
2 parents 65b8aaa + 07f2c48 commit 4c52f29
Show file tree
Hide file tree
Showing 12 changed files with 304 additions and 224 deletions.
32 changes: 5 additions & 27 deletions lua/entities/base_glide/sv_wheels.lua
Original file line number Diff line number Diff line change
Expand Up @@ -4,28 +4,10 @@ function ENT:WheelInit()
self.wheelsEnabled = true
self.steerAngle = Angle()

-- Store these values on a table that wheels can access.
self.wheelParams = self.wheelParams or {
-- Suspension
suspensionLength = 10,
springStrength = 800,
springDamper = 3000,

-- Wheel mass
inertia = 10,

-- Brake force
brakePower = 3000,

-- Forward traction
forwardTractionMax = 2600,

-- Side traction
sideTractionMultiplier = 20,
sideTractionMaxAng = 25,
sideTractionMax = 2400,
sideTractionMin = 800
}
-- This was deprecated. Putting values here does nothing.
-- Wheel parameters are stored on each wheel now.
-- This will be removed in the future.
self.wheelParams = {}
end

function ENT:CreateWheel( offset, params )
Expand Down Expand Up @@ -74,7 +56,6 @@ end

local Clamp = math.Clamp
local ClampForce = Glide.ClampForce
local SetupTraction = Glide.SetupTraction

local linForce, angForce = Vector(), Vector()

Expand All @@ -95,12 +76,9 @@ function ENT:PhysicsSimulate( phys, dt )
-- Do wheel physics
if self.wheelCount > 0 and self.wheelsEnabled then
local traceData = self.traceData
local params = self.wheelParams

SetupTraction( params )

for _, w in ipairs( self.wheels ) do
w:DoPhysics( self, phys, params, traceData, linForce, angForce, dt )
w:DoPhysics( self, phys, traceData, linForce, angForce, dt )
end
end

Expand Down
58 changes: 34 additions & 24 deletions lua/entities/base_glide_aircraft/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -30,13 +30,6 @@ function ENT:OnPostInitialize()
-- Countermeasure system
self.countermeasureCD = 0

-- Update default wheel params
local params = self.wheelParams

params.forwardTractionMax = 50000
params.sideTractionMultiplier = 200
params.sideTractionMinAng = 70

-- Trigger wire outputs
if WireLib then
WireLib.TriggerOutput( self, "Power", 0 )
Expand Down Expand Up @@ -82,13 +75,38 @@ function ENT:Repair()
self.rotors = validRotors
end

function ENT:SetLandingGearState( state )
self.landingGearState = state
--- Override this base class function.
function ENT:CreateWheel( offset, params )
-- Tweak default wheel params
params = params or {}

params.forwardTractionMax = params.forwardTractionMax or 50000
params.sideTractionMultiplier = params.sideTractionMultiplier or 200
params.sideTractionMinAng = params.sideTractionMinAng or 70

-- Let the base class create the wheel
local wheel = BaseClass.CreateWheel( self, offset, params )

-- Apply a bit of brake by default
wheel.state.brake = 0.5

return wheel
end

function ENT:ChangeSuspensionLengthMultiplier( multiplier )
for _, w in ipairs( self.wheels ) do
w.state.suspensionLengthMult = multiplier
end

-- Remember the original suspension spring length
if not self.landingGearLength then
self.landingGearLength = self.wheelParams.suspensionLength
local phys = self:GetPhysicsObject()

if IsValid( phys ) then
phys:Wake()
end
end

function ENT:SetLandingGearState( state )
self.landingGearState = state

local anim = self.LandingGearAnims[state]

Expand All @@ -107,7 +125,7 @@ function ENT:SetLandingGearState( state )
-- Set the gear up now
self.landingGearExtend = 0
self.wheelsEnabled = false
self.wheelParams.suspensionLength = 0
self:ChangeSuspensionLengthMultiplier( 0 )

elseif state == 3 then
-- Move the gear down
Expand All @@ -118,7 +136,7 @@ function ENT:SetLandingGearState( state )
-- Set the gear down now
self.landingGearExtend = 1
self.wheelsEnabled = true
self.wheelParams.suspensionLength = self.landingGearLength
self:ChangeSuspensionLengthMultiplier( 1 )
end

local soundParams = self.LandingGearSounds[state]
Expand All @@ -135,6 +153,7 @@ function ENT:LandingGearThink( dt )

if state == 1 then -- Is it moving up?
self.landingGearExtend = self.landingGearExtend - dt / self.landingGearAnimLen
self:ChangeSuspensionLengthMultiplier( self.landingGearExtend )

if self.landingGearExtend < 0 then
self:SetLandingGearState( 2 ) -- Set fully up
Expand All @@ -143,22 +162,13 @@ function ENT:LandingGearThink( dt )

elseif state == 3 then -- Is it moving down?
self.landingGearExtend = self.landingGearExtend + dt / self.landingGearAnimLen
self:ChangeSuspensionLengthMultiplier( self.landingGearExtend )

if self.landingGearExtend > 1 then
self:SetLandingGearState( 0 ) -- Set fully down
return
end
end

if state == 1 or state == 3 then
self.wheelParams.suspensionLength = self.landingGearLength * self.landingGearExtend

local phys = self:GetPhysicsObject()

if IsValid( phys ) then
phys:Wake()
end
end
end

function ENT:FireCountermeasures()
Expand Down
72 changes: 52 additions & 20 deletions lua/entities/base_glide_car/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,24 @@ function ENT:OnPostInitialize()
self:SetWheelRadius( 15 )

-- Setup default NW wheel params
local params = self.wheelParams
local params = {
-- Suspension
suspensionLength = 10,
springStrength = 800,
springDamper = 3000,

-- Brake force
brakePower = 3000,

-- Forward traction
forwardTractionMax = 2600,

-- Side traction
sideTractionMultiplier = 20,
sideTractionMaxAng = 25,
sideTractionMax = 2400,
sideTractionMin = 800
}

-- Maximum length of the suspension
self:SetSuspensionLength( params.suspensionLength )
Expand Down Expand Up @@ -91,22 +108,36 @@ function ENT:OnPostInitialize()
end
end

--- Update the `wheelParams` table using values from our network variables.
--- Update the `params` table for each wheel,
--- using values from our network variables.
function ENT:UpdateWheelParameters()
self.shouldUpdateWheelParams = false

local p = self.wheelParams

p.suspensionLength = self:GetSuspensionLength()
p.springStrength = self:GetSpringStrength()
p.springDamper = self:GetSpringDamper()
p.brakePower = self:GetBrakePower()

p.forwardTractionMax = self:GetForwardTractionMax()
p.sideTractionMultiplier = self:GetSideTractionMultiplier()
p.sideTractionMaxAng = self:GetSideTractionMaxAng()
p.sideTractionMin = self:GetSideTractionMin()
p.sideTractionMax = self:GetSideTractionMax()
local suspensionLength = self:GetSuspensionLength()
local springStrength = self:GetSpringStrength()
local springDamper = self:GetSpringDamper()
local brakePower = self:GetBrakePower()
local forwardTractionMax = self:GetForwardTractionMax()

local sideTractionMultiplier = self:GetSideTractionMultiplier()
local sideTractionMaxAng = self:GetSideTractionMaxAng()
local sideTractionMax = self:GetSideTractionMax()
local sideTractionMin = self:GetSideTractionMin()

for _, w in ipairs( self.wheels ) do
local p = w.params

p.suspensionLength = suspensionLength
p.springStrength = springStrength
p.springDamper = springDamper
p.brakePower = brakePower
p.forwardTractionMax = forwardTractionMax

p.sideTractionMultiplier = sideTractionMultiplier
p.sideTractionMaxAng = sideTractionMaxAng
p.sideTractionMax = sideTractionMax
p.sideTractionMin = sideTractionMin
end
end

--- Implement this base class function.
Expand Down Expand Up @@ -522,7 +553,7 @@ end

local traction, tractionFront, tractionRear
local frontTorque, rearTorque, steerAngle, frontBrake, rearBrake
local groundedCount, rpm, avgRPM, totalSideSlip, totalForwardSlip
local groundedCount, rpm, avgRPM, totalSideSlip, totalForwardSlip, state

--- Implement this base class function.
function ENT:WheelThink( dt )
Expand Down Expand Up @@ -553,19 +584,20 @@ function ENT:WheelThink( dt )
rpm = w:GetRPM()
avgRPM = avgRPM + rpm * w.distributionFactor

w.torque = w.distributionFactor * ( w.isFrontWheel and frontTorque or rearTorque )
w.brake = w.isFrontWheel and frontBrake or rearBrake
w.forwardTractionMult = w.isFrontWheel and tractionFront or tractionRear
state = w.state
state.torque = w.distributionFactor * ( w.isFrontWheel and frontTorque or rearTorque )
state.brake = w.isFrontWheel and frontBrake or rearBrake
state.forwardTractionMult = w.isFrontWheel and tractionFront or tractionRear

if inputHandbrake and not w.isFrontWheel then
w.angularVelocity = 0
state.angularVelocity = 0
end

if rpm > maxRPM then
w:SetRPM( maxRPM )
end

if w.isOnGround then
if state.isOnGround then
groundedCount = groundedCount + 1
end
end
Expand Down
31 changes: 20 additions & 11 deletions lua/entities/base_glide_plane/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,6 @@ function ENT:OnPostInitialize()
self.isGrounded = false
self.brake = 0
self.divePitch = 0

-- Update default wheel params
local params = self.wheelParams

params.brakePower = 800
params.suspensionLength = 10
params.springStrength = 1000
params.springDamper = 4000
end

--- Override this base class function.
Expand All @@ -36,6 +28,20 @@ function ENT:Repair()
end
end

--- Override this base class function.
function ENT:CreateWheel( offset, params )
-- Tweak default wheel params
params = params or {}

params.brakePower = params.brakePower or 800
params.suspensionLength = params.suspensionLength or 10
params.springStrength = params.springStrength or 1000
params.springDamper = params.springDamper or 4000

-- Let the base class create the wheel
return BaseClass.CreateWheel( self, offset, params )
end

--- Creates and stores a new propeller entity.
---
--- `radius` is used for collision checking.
Expand Down Expand Up @@ -214,12 +220,15 @@ function ENT:OnPostThink( dt, selfTbl )

local isGrounded = false
local totalSideSlip = 0
local state

for _, w in ipairs( self.wheels ) do
w.brake = self.brake
w.torque = torque
state = w.state

state.brake = self.brake
state.torque = torque

if w.isOnGround then
if state.isOnGround then
isGrounded = true
totalSideSlip = totalSideSlip + w:GetSideSlip()
end
Expand Down
15 changes: 0 additions & 15 deletions lua/entities/base_glide_tank/cl_init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -89,21 +89,6 @@ function ENT:OnTurnOff()
end
end

--- Override this base class function.
function ENT:ActivateMisc()
BaseClass.ActivateMisc( self )

local wheels = self.wheels
if not wheels then return end

-- Reduce the number of wheels playing sounds
for i, w in ipairs( wheels ) do
if i == 2 or i == 5 then
w.enableSounds = false
end
end
end

--- Override this base class function.
function ENT:DeactivateMisc()
BaseClass.DeactivateMisc( self )
Expand Down
Loading

0 comments on commit 4c52f29

Please sign in to comment.