Skip to content

Commit

Permalink
reworked brake logic
Browse files Browse the repository at this point in the history
  • Loading branch information
SvetlozarValchev committed Nov 21, 2024
1 parent d812a74 commit ab57f71
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 24 deletions.
13 changes: 4 additions & 9 deletions lua/config.lua
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,11 @@ return {
BRAKE_SLIP_THRESHOLD = 7, -- wheel slip threshold for brake slip vibration (0-100)
BRAKE_SLIP_VIBRATION_FREQUENCY = 255, -- frequency of brake slip vibration (0-255)
BRAKE_VALUE_MIN = 0.1, -- minimum brake value for brake resistance calculation (0-1)
BRAKE_VALUE_MAX = 0.6, -- maximum brake value for brake resistance calculation (0-1)
BRAKE_RESISTANCE_MIN = 10, -- minimum brake resistance (0-255)
BRAKE_RESISTANCE_MAX = 130, -- maximum brake resistance (0-255)
BRAKE_AIRSPEED_MAX = 60, -- maximum airspeed for brake force calculation (kmh/h)
BRAKE_FORCE_MIN = 100, -- minimum brake force (0-255)
BRAKE_VALUE_MAX = 0.5, -- maximum brake value for brake resistance calculation (0-1)
BRAKE_AIRSPEED_MAX = 120, -- maximum airspeed for brake force calculation (kmh/h)
BRAKE_FORCE_MIN = 50, -- minimum brake force (0-255)
BRAKE_FORCE_MAX = 255, -- maximum brake force (0-255)
THROTTLE_VALUE_MIN = 0.4, -- minimum throttle value for throttle resistance calculation (0-1)
THROTTLE_VALUE_MAX = 1, -- maximum throttle value for throttle resistance calculation (0-1)
THROTTLE_RESISTANCE_MIN = 25, -- minimum throttle resistance (0-255)
THROTTLE_RESISTANCE_MAX = 100, -- maximum throttle resistance (0-255)
THROTTLE_RESISTANCE_START = 110, -- minimum throttle resistance (0-255)
DAMAGE_LED_COLOR = {
r = 255, -- red component (0-255)
g = 0, -- green component (0-255)
Expand Down
31 changes: 16 additions & 15 deletions lua/vehicle/extensions/auto/dsx.lua
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,16 @@ function inverseLerp(a, b, v)
return 0
end

local function calculateCombinedForce(brake, speed)
local brakeNormalized = math.max(0, math.min(1, (brake - CONFIG.BRAKE_VALUE_MIN) /
(CONFIG.BRAKE_VALUE_MAX - CONFIG.BRAKE_VALUE_MIN)))
local speedNormalized = math.min(1.0, speed / CONFIG.BRAKE_AIRSPEED_MAX)

local combinedFactor = brakeNormalized * speedNormalized

return math.min(255, lerp(CONFIG.BRAKE_FORCE_MIN, CONFIG.BRAKE_FORCE_MAX, combinedFactor))
end

local function handleLeftTrigger()
local absActive = state.isABSActive and (socket.gettime() - state.absActivationTime) > CONFIG.ABS_WAIT_TIME

Expand All @@ -133,17 +143,11 @@ local function handleLeftTrigger()
1, 0, 0, 0, 0, 0}
}
else
local brakeIntensity = lerp(CONFIG.BRAKE_VALUE_MIN, CONFIG.BRAKE_VALUE_MAX, state.brake)
local startOfResistance = lerp(CONFIG.BRAKE_RESISTANCE_MIN, CONFIG.BRAKE_RESISTANCE_MAX, brakeIntensity)
local airSpeedFactor = math.min(1.0, state.airSpeedKmh / CONFIG.BRAKE_AIRSPEED_MAX)
local amountOfForceExcerted = math.min(255,
lerp(CONFIG.BRAKE_FORCE_MIN, CONFIG.BRAKE_FORCE_MAX, airSpeedFactor))

return {
type = Triggers.InstructionType.TriggerUpdate,
parameters = {CONFIG.CONTROLLER_INDEX, Triggers.Trigger.Left, Triggers.TriggerMode.CustomTriggerValue,
Triggers.CustomTriggerValueMode.Rigid, startOfResistance, amountOfForceExcerted, 255, 0,
0, 0, 0}
Triggers.CustomTriggerValueMode.Rigid, CONFIG.BRAKE_VALUE_MIN * 255,
calculateCombinedForce(state.brake, state.airSpeedKmh), 0, 0, 0, 0, 0}
}
end
end
Expand All @@ -159,14 +163,10 @@ local function handleRightTrigger()
Triggers.CustomTriggerValueMode.VibratePulse, 255, 255, 3, 50, 0, 0, 0}
}
else
local startOfResistanceThrottle = 1 * lerp(CONFIG.THROTTLE_VALUE_MIN, CONFIG.THROTTLE_VALUE_MIN, state.throttle)
local startOfResistance = lerp(CONFIG.THROTTLE_RESISTANCE_MIN, CONFIG.THROTTLE_RESISTANCE_MAX,
startOfResistanceThrottle)

return {
type = Triggers.InstructionType.TriggerUpdate,
parameters = {CONFIG.CONTROLLER_INDEX, Triggers.Trigger.Right, Triggers.TriggerMode.CustomTriggerValue,
Triggers.CustomTriggerValueMode.Rigid, startOfResistance, 0, 0, 0, 0, 0, 0}
Triggers.CustomTriggerValueMode.Rigid, CONFIG.THROTTLE_RESISTANCE_START, 0, 0, 0, 0, 0, 0}
}
end
end
Expand Down Expand Up @@ -289,15 +289,16 @@ local function updateGFX(dt)
maxRPM = engines[1]:getTorqueData().maxRPM
end

local isAbsActive = electrics.values.absActive == 1
local rpm = electrics.values.rpm or 0

if isABSActive and not state.isABSActive then
if isAbsActive and not state.isABSActive then
state.absActivationTime = socket.gettime()
end

state.throttle = input.state.throttle.val
state.brake = input.state.brake.val
state.isABSActive = electrics.values.absActive == 1
state.isABSActive = isAbsActive
state.airSpeedKmh = electrics.values.airspeed * 3.6
state.beamDamage = beamstate.damage
state.wheelSlip = wheelSlip
Expand Down

0 comments on commit ab57f71

Please sign in to comment.