From 8cc2479825a818f126c53939dacba8007a36125b Mon Sep 17 00:00:00 2001
From: skrek <liam@tannock.net>
Date: Wed, 16 Feb 2022 02:06:52 -0800
Subject: [PATCH] Adjusting how deadzones are calculated (#3079)

* Making deadzones feel nice and smooth + adding rider files to .gitignore

* removing unnecessary parentheses and fixing possibility of divide by 0

* formatting :)

* fixing up ClampAxis

* fixing up ClampAxis
---
 .gitignore                          |  3 +++
 Ryujinx.Input/HLE/NpadController.cs | 23 ++++++++++++++---------
 2 files changed, 17 insertions(+), 9 deletions(-)

diff --git a/.gitignore b/.gitignore
index c0af26097..9432a953a 100644
--- a/.gitignore
+++ b/.gitignore
@@ -74,6 +74,9 @@ _TeamCity*
 # DotCover is a Code Coverage Tool
 *.dotCover
 
+# Rider is a Visual Studio alternative
+.idea/*
+
 # NCrunch
 *.ncrunch*
 .*crunch*.local.xml
diff --git a/Ryujinx.Input/HLE/NpadController.cs b/Ryujinx.Input/HLE/NpadController.cs
index 2af114d21..eb9989b03 100644
--- a/Ryujinx.Input/HLE/NpadController.cs
+++ b/Ryujinx.Input/HLE/NpadController.cs
@@ -391,24 +391,29 @@ namespace Ryujinx.Input.HLE
         [MethodImpl(MethodImplOptions.AggressiveInlining)]
         private static JoystickPosition ApplyDeadzone(float x, float y, float deadzone)
         {
-            return new JoystickPosition
+            float magnitudeClamped = Math.Min(MathF.Sqrt(x * x + y * y), 1f);
+            
+            if (magnitudeClamped <= deadzone)
             {
-                Dx = ClampAxis(MathF.Abs(x) > deadzone ? x : 0.0f),
-                Dy = ClampAxis(MathF.Abs(y) > deadzone ? y : 0.0f)
+                return new JoystickPosition() {Dx = 0, Dy = 0};
+            }
+            
+            return new JoystickPosition()
+            {
+                Dx = ClampAxis((x / magnitudeClamped) * ((magnitudeClamped - deadzone) / (1 - deadzone))),
+                Dy = ClampAxis((y / magnitudeClamped) * ((magnitudeClamped - deadzone) / (1 - deadzone)))
             };
         }
 
         [MethodImpl(MethodImplOptions.AggressiveInlining)]
         private static short ClampAxis(float value)
         {
-            if (value <= -short.MaxValue)
+            if (Math.Sign(value) < 0)
             {
-                return -short.MaxValue;
-            }
-            else
-            {
-                return (short)(value * short.MaxValue);
+                return (short)Math.Max(value * -short.MinValue, short.MinValue);
             }
+
+            return (short)Math.Min(value * short.MaxValue, short.MaxValue);
         }
 
         [MethodImpl(MethodImplOptions.AggressiveInlining)]