From b7fbdfa2a9f85501ac63f191be8b371347b4e239 Mon Sep 17 00:00:00 2001 From: Tony Ferguson Date: Wed, 22 Jul 2026 11:13:14 +0100 Subject: [PATCH] Implement controller joystick deadzone (#5354) * Implement controller joystick deadzone * Triggers keep a fixed deadzone --- engine/Sandbox.Engine/Game/Preferences.cs | 6 ++++++ .../Systems/Input/Controller/Controller.Input.cs | 12 +++++++++--- .../menu/Code/Modals/Settings/InputSettings.razor | 9 +++++++++ 3 files changed, 24 insertions(+), 3 deletions(-) diff --git a/engine/Sandbox.Engine/Game/Preferences.cs b/engine/Sandbox.Engine/Game/Preferences.cs index 2a172415..ee746df3 100644 --- a/engine/Sandbox.Engine/Game/Preferences.cs +++ b/engine/Sandbox.Engine/Game/Preferences.cs @@ -36,6 +36,12 @@ public static class Preferences [ConVar( "controller_look_speed_pitch", ConVarFlags.Protected, Help = "How fast the camera turns vertically when the stick is pushed all the way up/down (deg/s)", Saved = true, Min = 0.1f, Max = 360.0f )] public static float ControllerLookPitchSpeed { get; internal set; } = 160.0f; + /// + /// The percentage of joystick travel ignored around the neutral position. + /// + [ConVar( "controller_joystick_deadzone", ConVarFlags.Protected, Help = "Percentage of joystick travel ignored around the neutral position", Saved = true, Min = 0.0f, Max = 50.0f )] + public static float ControllerJoystickDeadzone { get; internal set; } = 12.5f; + [ConVar( "controller_analog_speed", ConVarFlags.Protected, Help = "How fast the left joystick moves, for stuff like the virtual cursor in menus", Saved = true, Min = 0.1f, Max = 360.0f )] internal static float ControllerAnalogSpeed { get; set; } = 2.0f; diff --git a/engine/Sandbox.Engine/Systems/Input/Controller/Controller.Input.cs b/engine/Sandbox.Engine/Systems/Input/Controller/Controller.Input.cs index 70924795..e6b6b263 100644 --- a/engine/Sandbox.Engine/Systems/Input/Controller/Controller.Input.cs +++ b/engine/Sandbox.Engine/Systems/Input/Controller/Controller.Input.cs @@ -22,6 +22,11 @@ internal sealed partial class Controller List ControllerAxes { get; set; } = new(); + /// + /// Triggers always use a fixed deadzone, they're not affected by . + /// + const float TriggerDeadzone = 0.125f; + /// /// Get an axis /// @@ -44,9 +49,10 @@ internal sealed partial class Controller float flValue = inputValue; float normalizedAxis = flValue.Remap( Controller.AXIS_RANGE.x, Controller.AXIS_RANGE.y, -1, 1 ); - // 12.5% deadzone - // todo: make this modifiable - var deadzone = 0.125f; + var deadzone = axis < NativeEngine.GameControllerAxis.TriggerLeft + ? Preferences.ControllerJoystickDeadzone / 100.0f + : TriggerDeadzone; + if ( MathF.Abs( normalizedAxis ) <= deadzone ) normalizedAxis = 0f; if ( normalizedAxis > 0f ) diff --git a/game/addons/menu/Code/Modals/Settings/InputSettings.razor b/game/addons/menu/Code/Modals/Settings/InputSettings.razor index dc6f4164..3e6e0d46 100644 --- a/game/addons/menu/Code/Modals/Settings/InputSettings.razor +++ b/game/addons/menu/Code/Modals/Settings/InputSettings.razor @@ -37,6 +37,11 @@
Look Pitch Speed
+ + +
Joystick Deadzone (%)
+ +
@@ -50,6 +55,7 @@ public float ControllerLookSpeedYaw { get; set; } public float ControllerLookSpeedPitch { get; set; } + public float ControllerJoystickDeadzone { get; set; } public bool InvertMousePitch { get; set; } public bool InvertMouseYaw { get; set; } @@ -81,6 +87,7 @@ ConsoleSystem.SetValue("sensitivity", 5); ConsoleSystem.SetValue("controller_look_speed_yaw", 270); ConsoleSystem.SetValue("controller_look_speed_pitch", 160); + ConsoleSystem.SetValue("controller_joystick_deadzone", 12.5f); ConsoleSystem.SetValue("mouse_pitch_inverted", false); ConsoleSystem.SetValue("mouse_yaw_inverted", false); } @@ -90,6 +97,7 @@ MouseSensitivity = ConsoleSystem.GetValue("sensitivity").ToFloat(); ControllerLookSpeedYaw = ConsoleSystem.GetValue("controller_look_speed_yaw").ToFloat(); ControllerLookSpeedPitch = ConsoleSystem.GetValue("controller_look_speed_pitch").ToFloat(); + ControllerJoystickDeadzone = ConsoleSystem.GetValue("controller_joystick_deadzone").ToFloat(); InvertMousePitch = ConsoleSystem.GetValue("mouse_pitch_inverted").ToBool(); InvertMouseYaw = ConsoleSystem.GetValue("mouse_yaw_inverted").ToBool(); } @@ -99,6 +107,7 @@ ConsoleSystem.SetValue( "sensitivity", MouseSensitivity ); ConsoleSystem.SetValue( "controller_look_speed_yaw", ControllerLookSpeedYaw ); ConsoleSystem.SetValue( "controller_look_speed_pitch", ControllerLookSpeedPitch ); + ConsoleSystem.SetValue( "controller_joystick_deadzone", ControllerJoystickDeadzone ); ConsoleSystem.SetValue( "mouse_pitch_inverted", InvertMousePitch ); ConsoleSystem.SetValue( "mouse_yaw_inverted", InvertMouseYaw ); }