From 1fc77c5537b394c27954e4bc426176fd54cccfc6 Mon Sep 17 00:00:00 2001 From: Travis Nickles Date: Sat, 10 Jun 2017 17:48:34 -0700 Subject: [PATCH] Take current stick direction into account for mouse offset --- DS4Windows/DS4Control/Mapping.cs | 44 ++++++++++++++++++++++++------- DS4Windows/DS4Library/DS4State.cs | 30 +++++++++++++++++++++ 2 files changed, 65 insertions(+), 9 deletions(-) diff --git a/DS4Windows/DS4Control/Mapping.cs b/DS4Windows/DS4Control/Mapping.cs index d088907..1b4a2a2 100644 --- a/DS4Windows/DS4Control/Mapping.cs +++ b/DS4Windows/DS4Control/Mapping.cs @@ -141,7 +141,9 @@ namespace DS4Windows public static int prevmouseaccel = 0; private static double horizontalRemainder = 0.0, verticalRemainder = 0.0; private const int MOUSESPEEDFACTOR = 40; - private const double MOUSESTICKOFFSET = 0.025; + private const double MOUSESTICKOFFSET = 0.03; + private static double tempMouseOffsetX = 0.0; + private static double tempMouseOffsetY = 0.0; public static void Commit(int device) { @@ -2339,7 +2341,10 @@ namespace DS4Windows if (cState.LX < 127 - deadzoneL) { double diff = -(cState.LX - 127 - deadzoneL) / (double)(0 - 127 - deadzoneL); - value = ((speed * MOUSESPEEDFACTOR * (timeElapsed * 0.001)) - MOUSESTICKOFFSET) * diff + MOUSESTICKOFFSET; + //tempMouseOffsetX = Math.Abs(Math.Cos(cState.LSAngleRad)) * MOUSESTICKOFFSET; + //tempMouseOffsetX = MOUSESTICKOFFSET; + tempMouseOffsetX = cState.LXUnit * MOUSESTICKOFFSET; + value = ((speed * MOUSESPEEDFACTOR * (timeElapsed * 0.001)) - tempMouseOffsetX) * diff + tempMouseOffsetX; //value = diff * MOUSESPEEDFACTOR * (timeElapsed * 0.001) * speed; //value = -(cState.LX - 127 - deadzoneL) / 2550d * speed; } @@ -2351,7 +2356,10 @@ namespace DS4Windows if (cState.LX > 127 + deadzoneL) { double diff = (cState.LX - 127 + deadzoneL) / (double)(255 - 127 + deadzoneL); - value = ((speed * MOUSESPEEDFACTOR * (timeElapsed * 0.001)) - MOUSESTICKOFFSET) * diff + MOUSESTICKOFFSET; + tempMouseOffsetX = cState.LXUnit * MOUSESTICKOFFSET; + //tempMouseOffsetX = Math.Abs(Math.Cos(cState.LSAngleRad)) * MOUSESTICKOFFSET; + //tempMouseOffsetX = MOUSESTICKOFFSET; + value = ((speed * MOUSESPEEDFACTOR * (timeElapsed * 0.001)) - tempMouseOffsetX) * diff + tempMouseOffsetX; //value = diff * MOUSESPEEDFACTOR * (timeElapsed * 0.001) * speed; //value = (cState.LX - 127 + deadzoneL) / 2550d * speed; } @@ -2363,7 +2371,10 @@ namespace DS4Windows if (cState.RX < 127 - deadzoneR) { double diff = -(cState.RX - 127 - deadzoneR) / (double)(0 - 127 - deadzoneR); - value = ((speed * MOUSESPEEDFACTOR * (timeElapsed * 0.001)) - MOUSESTICKOFFSET) * diff + MOUSESTICKOFFSET; + tempMouseOffsetX = cState.RXUnit * MOUSESTICKOFFSET; + //tempMouseOffsetX = MOUSESTICKOFFSET; + //tempMouseOffsetX = Math.Abs(Math.Cos(cState.RSAngleRad)) * MOUSESTICKOFFSET; + value = ((speed * MOUSESPEEDFACTOR * (timeElapsed * 0.001)) - tempMouseOffsetX) * diff + tempMouseOffsetX; //value = diff * MOUSESPEEDFACTOR * (timeElapsed * 0.001) * speed; //value = -(cState.RX - 127 - deadzoneR) / 2550d * speed; } @@ -2375,7 +2386,10 @@ namespace DS4Windows if (cState.RX > 127 + deadzoneR) { double diff = (cState.RX - 127 + deadzoneR) / (double)(255 - 127 + deadzoneR); - value = ((speed * MOUSESPEEDFACTOR * (timeElapsed * 0.001)) - MOUSESTICKOFFSET) * diff + MOUSESTICKOFFSET; + tempMouseOffsetX = cState.RXUnit * MOUSESTICKOFFSET; + //tempMouseOffsetX = MOUSESTICKOFFSET; + //tempMouseOffsetX = Math.Abs(Math.Cos(cState.RSAngleRad)) * MOUSESTICKOFFSET; + value = ((speed * MOUSESPEEDFACTOR * (timeElapsed * 0.001)) - tempMouseOffsetX) * diff + tempMouseOffsetX; //value = diff * MOUSESPEEDFACTOR * (timeElapsed * 0.001) * speed; //value = (cState.RX - 127 + deadzoneR) / 2550d * speed; } @@ -2387,7 +2401,10 @@ namespace DS4Windows if (cState.LY < 127 - deadzoneL) { double diff = -(cState.LY - 127 - deadzoneL) / (double)(0 - 127 - deadzoneL); - value = ((speed * MOUSESPEEDFACTOR * (timeElapsed * 0.001)) - MOUSESTICKOFFSET) * diff + MOUSESTICKOFFSET; + tempMouseOffsetY = cState.LYUnit * MOUSESTICKOFFSET; + //tempMouseOffsetY = MOUSESTICKOFFSET; + //tempMouseOffsetY = Math.Abs(Math.Sin(cState.LSAngleRad)) * MOUSESTICKOFFSET; + value = ((speed * MOUSESPEEDFACTOR * (timeElapsed * 0.001)) - tempMouseOffsetY) * diff + tempMouseOffsetY; //value = diff * MOUSESPEEDFACTOR * (timeElapsed * 0.001) * speed; //value = -(cState.LY - 127 - deadzoneL) / 2550d * speed; } @@ -2399,7 +2416,10 @@ namespace DS4Windows if (cState.LY > 127 + deadzoneL) { double diff = (cState.LY - 127 + deadzoneL) / (double)(255 - 127 + deadzoneL); - value = ((speed * MOUSESPEEDFACTOR * (timeElapsed * 0.001)) - MOUSESTICKOFFSET) * diff + MOUSESTICKOFFSET; + tempMouseOffsetY = cState.LYUnit * MOUSESTICKOFFSET; + //tempMouseOffsetY = MOUSESTICKOFFSET; + //tempMouseOffsetY = Math.Abs(Math.Sin(cState.LSAngleRad)) * MOUSESTICKOFFSET; + value = ((speed * MOUSESPEEDFACTOR * (timeElapsed * 0.001)) - tempMouseOffsetY) * diff + tempMouseOffsetY; //value = diff * MOUSESPEEDFACTOR * (timeElapsed * 0.001) * speed; //value = (cState.LY - 127 + deadzoneL) / 2550d * speed; } @@ -2411,7 +2431,10 @@ namespace DS4Windows if (cState.RY < 127 - deadzoneR) { double diff = -(cState.RY - 127 - deadzoneR) / (double)(0 - 127 - deadzoneR); - value = ((speed * MOUSESPEEDFACTOR * (timeElapsed * 0.001)) - MOUSESTICKOFFSET) * diff + MOUSESTICKOFFSET; + tempMouseOffsetY = cState.RYUnit * MOUSESTICKOFFSET; + //tempMouseOffsetY = MOUSESTICKOFFSET; + //tempMouseOffsetY = Math.Abs(Math.Sin(cState.RSAngleRad)) * MOUSESTICKOFFSET; + value = ((speed * MOUSESPEEDFACTOR * (timeElapsed * 0.001)) - tempMouseOffsetY) * diff + tempMouseOffsetY; //value = diff * MOUSESPEEDFACTOR * (timeElapsed * 0.001) * speed; //value = -(cState.RY - 127 - deadzoneR) / 2550d * speed; } @@ -2423,7 +2446,10 @@ namespace DS4Windows if (cState.RY > 127 + deadzoneR) { double diff = (cState.RY - 127 + deadzoneR) / (double)(255 - 127 + deadzoneR); - value = ((speed * MOUSESPEEDFACTOR * (timeElapsed * 0.001)) - MOUSESTICKOFFSET) * diff + MOUSESTICKOFFSET; + tempMouseOffsetY = cState.RYUnit * MOUSESTICKOFFSET; + //tempMouseOffsetY = MOUSESTICKOFFSET; + //tempMouseOffsetY = Math.Abs(Math.Sin(cState.RSAngleRad)) * MOUSESTICKOFFSET; + value = ((speed * MOUSESPEEDFACTOR * (timeElapsed * 0.001)) - tempMouseOffsetY) * diff + tempMouseOffsetY; //value = diff * MOUSESPEEDFACTOR * (timeElapsed * 0.001) * speed; //value = (cState.RY - 127 + deadzoneR) / 2550d * speed; } diff --git a/DS4Windows/DS4Library/DS4State.cs b/DS4Windows/DS4Library/DS4State.cs index 2661dad..d62810b 100644 --- a/DS4Windows/DS4Library/DS4State.cs +++ b/DS4Windows/DS4Library/DS4State.cs @@ -16,6 +16,12 @@ namespace DS4Windows public byte Battery; // 0 for charging, 10/20/30/40/50/60/70/80/90/100 for percentage of full public double LSAngle; // Calculated bearing of the LS X,Y coordinates public double RSAngle; // Calculated bearing of the RS X,Y coordinates + public double LSAngleRad; // Calculated bearing of the LS X,Y coordinates (in radians) + public double RSAngleRad; // Calculated bearing of the RS X,Y coordinates (in radians) + public double LXUnit; + public double LYUnit; + public double RXUnit; + public double RYUnit; public static readonly int DEFAULT_AXISDIR_VALUE = 127; public DS4State() @@ -30,7 +36,13 @@ namespace DS4Windows TouchPacketCounter = 255; // 8 bits, no great junk value Battery = 0; LSAngle = 0.0; + LSAngleRad = 0.0; RSAngle = 0.0; + RSAngleRad = 0.0; + LXUnit = 0.0; + LYUnit = 0.0; + RXUnit = 0.0; + RYUnit = 0.0; } public DS4State(DS4State state) @@ -68,7 +80,13 @@ namespace DS4Windows FrameCounter = state.FrameCounter; Battery = state.Battery; LSAngle = state.LSAngle; + LSAngleRad = state.LSAngleRad; RSAngle = state.RSAngle; + RSAngleRad = state.RSAngleRad; + LXUnit = state.LXUnit; + LYUnit = state.LYUnit; + RXUnit = state.RXUnit; + RYUnit = state.RYUnit; } public DS4State Clone() @@ -111,18 +129,30 @@ namespace DS4Windows state.FrameCounter = FrameCounter; state.Battery = Battery; state.LSAngle = LSAngle; + state.LSAngleRad = LSAngleRad; state.RSAngle = RSAngle; + state.RSAngleRad = RSAngleRad; + state.LXUnit = LXUnit; + state.LYUnit = LYUnit; + state.RXUnit = RXUnit; + state.RYUnit = RYUnit; } public void calculateStickAngles() { double lsangle = Math.Atan2((LX - 127), -(LY - 127)); + LSAngleRad = lsangle; lsangle = (lsangle >= 0 ? lsangle : (2 * Math.PI + lsangle)) * 180 / Math.PI; LSAngle = lsangle; + LXUnit = Math.Abs(Math.Cos(LSAngleRad)); + LYUnit = Math.Abs(Math.Sin(LSAngleRad)); double rsangle = Math.Atan2((RX - 127), -(RY - 127)); + RSAngleRad = rsangle; rsangle = (rsangle >= 0 ? rsangle : (2 * Math.PI + rsangle)) * 180 / Math.PI; RSAngle = rsangle; + RXUnit = Math.Abs(Math.Cos(RSAngleRad)); + RYUnit = Math.Abs(Math.Sin(LSAngleRad)); } } }