Skip to content

Commit

Permalink
Improved aiming
Browse files Browse the repository at this point in the history
  • Loading branch information
r57zone committed Jun 5, 2022
1 parent 3813046 commit 1956798
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 47 deletions.
33 changes: 24 additions & 9 deletions X360AdvanceApp/X360Advance.cpp
Original file line number Diff line number Diff line change
@@ -1,15 +1,20 @@
#include <windows.h>
#include <cmath>
#include <algorithm>
#include "XInput.h"
#include "IniReader\IniReader.h"
#pragma comment(lib, "XInput.lib")

float SensX, SensY;
float ArduinoData[4] = { 0, 0, 0, 0 }; // Mode, Yaw, Pitch, Roll
float LastArduinoData[4] = { 0, 0, 0, 0 };
float YRPOffset[3] = { 0, 0, 0 };
float DeltaYRP[3] = { 0, 0, 0 };
float LastYRP[3] = { 0, 0, 0 };
float accumulatedX = 0, accumulatedY = 0;
int GameMode = 2;
bool ArduinoWork = false;
XINPUT_STATE pState;

double OffsetYPR(double f, double f2)
{
Expand All @@ -36,8 +41,8 @@ bool CorrectAngleValue(float Value)
}
}

//Implementation from https://github.com/JibbSmart/JoyShockMapper/blob/master/JoyShockMapper/src/win32/InputHelpers.cpp
void MouseMove(float x, float y) {
// Implementation from https://github.com/JibbSmart/JoyShockMapper/blob/master/JoyShockMapper/src/win32/InputHelpers.cpp
void MoveMouse(float x, float y) {
accumulatedX += x;
accumulatedY += y;

Expand All @@ -64,6 +69,7 @@ int main()
CIniReader IniFile("X360Advance.ini");
SensX = IniFile.ReadFloat("Main", "SensX", 35);
SensY = IniFile.ReadFloat("Main", "SensY", 30);
bool OnlyTrigger = IniFile.ReadBoolean("Main", "OnlyTrigger", false);
HANDLE hSerial;

char sPortName[8];
Expand Down Expand Up @@ -111,7 +117,7 @@ int main()

ReadFile(hSerial, &ArduinoData, sizeof(ArduinoData), &bytesRead, 0);

//Filter incorrect values
// Filter incorrect values
if (CorrectAngleValue(ArduinoData[1]) == false || CorrectAngleValue(ArduinoData[2]) == false || CorrectAngleValue(ArduinoData[3]) == false)
{
//Last correct values
Expand All @@ -123,7 +129,7 @@ int main()
PurgeComm(hSerial, PURGE_TXCLEAR | PURGE_RXCLEAR);
}

//Save last correct values
// Save last correct values
if (CorrectAngleValue(ArduinoData[1]) && CorrectAngleValue(ArduinoData[2]) && CorrectAngleValue(ArduinoData[3]))
{
LastArduinoData[0] = ArduinoData[0];
Expand All @@ -148,13 +154,20 @@ int main()
}

if (GameMode == 2) {
float NewX = OffsetYPR(ArduinoData[1], YRPOffset[0]) * -1;
float NewY = OffsetYPR(ArduinoData[3], YRPOffset[2]);
DeltaYRP[0] = OffsetYPR(ArduinoData[1], LastYRP[0]) * -1;
DeltaYRP[2] = OffsetYPR(ArduinoData[3], LastYRP[2]);

MouseMove(NewX * SensX, NewY * SensY);
XInputGetState(0, &pState);

YRPOffset[0] = LastArduinoData[1];
YRPOffset[2] = LastArduinoData[3];
if (pState.Gamepad.bLeftTrigger == 0) {
if (OnlyTrigger == false)
MoveMouse(DeltaYRP[0] * SensX, DeltaYRP[2] * SensY);
}
else
MoveMouse(DeltaYRP[0] * SensX, DeltaYRP[2] * SensY);

LastYRP[0] = ArduinoData[1];
LastYRP[2] = ArduinoData[3];
}

if ((GetAsyncKeyState(VK_NUMPAD5) & 0x8000) != 0) {
Expand Down Expand Up @@ -206,6 +219,8 @@ int main()
}

}

if (bytesRead == 0) Sleep(1); // Don't overload CPU
}

CloseHandle(hSerial);
Expand Down
65 changes: 27 additions & 38 deletions XInputInject/XInputInject.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#include <math.h>
#include <atlstr.h>
#include "MinHook.h"
#include <iostream>

#if defined _M_X64
#pragma comment(lib, "libMinHook-x64-v141-md.lib")
Expand Down Expand Up @@ -54,8 +55,10 @@ HANDLE hSerial;
float ArduinoData[4] = { 0, 0, 0, 0 }; //Mode, Yaw, Pitch, Roll
float LastArduinoData[4] = { 0, 0, 0, 0 };
float YRPOffset[3] = { 0, 0, 0 };
float DeltaYRP[3] = { 0, 0, 0 };
float LastYRP[3] = { 0, 0, 0 };
BYTE GameMode = 0;
DWORD WheelAngle, SensX, SensY, TriggerSens, OnlyTrigger;
DWORD WheelAngle, SensX, SensY, TriggerSens, OnlyTrigger; //, JoyMouse;
float accumulatedX = 0, accumulatedY = 0;
DWORD WorkStatus = 0;

Expand All @@ -81,7 +84,7 @@ void ArduinoRead()
while (ArduinoWork) {
ReadFile(hSerial, &ArduinoData, sizeof(ArduinoData), &bytesRead, 0);

//Filter incorrect values
// Filter incorrect values
if (CorrectAngleValue(ArduinoData[1]) == false || CorrectAngleValue(ArduinoData[2]) == false || CorrectAngleValue(ArduinoData[3]) == false)
{
//Last correct values
Expand All @@ -93,7 +96,7 @@ void ArduinoRead()
PurgeComm(hSerial, PURGE_TXCLEAR | PURGE_RXCLEAR);
}

//Save last correct values
// Save last correct values
if (CorrectAngleValue(ArduinoData[1]) && CorrectAngleValue(ArduinoData[2]) && CorrectAngleValue(ArduinoData[3]))
{
LastArduinoData[0] = ArduinoData[0];
Expand Down Expand Up @@ -122,7 +125,7 @@ void ArduinoRead()
Centering();
}

if (bytesRead == 0) Sleep(1); //Don't overload CPU
if (bytesRead == 0) Sleep(1); // Don't overload CPU
}
}

Expand Down Expand Up @@ -150,6 +153,7 @@ void ArduinoStart()
key.QueryDWORDValue(_T("TriggerSens"), TriggerSens);
TriggerSens = TriggerSens / 10.0f;
key.QueryDWORDValue(_T("OnlyTrigger"), OnlyTrigger);
//key.QueryDWORDValue(_T("JoyMouse"), JoyMouse);
}
key.Close();

Expand Down Expand Up @@ -217,7 +221,7 @@ double OffsetYPR(float f, float f2)
return f;
}

//Implementation from https://github.com/JibbSmart/JoyShockMapper/blob/master/JoyShockMapper/src/win32/InputHelpers.cpp
// Implementation from https://github.com/JibbSmart/JoyShockMapper/blob/master/JoyShockMapper/src/win32/InputHelpers.cpp
void MoveMouse(float x, float y) {
accumulatedX += x;
accumulatedY += y;
Expand Down Expand Up @@ -246,51 +250,36 @@ DWORD WINAPI detourXInputGetState(DWORD dwUserIndex, XINPUT_STATE* pState)
ArduinoStart();
}

//ZeroMemory(pState, sizeof(XINPUT_STATE));
// ZeroMemory(pState, sizeof(XINPUT_STATE));

// first call the original function
DWORD toReturn = hookedXInputGetState(dwUserIndex, pState);

//Crysis 2 reads the state incorrectly, so there is a separate library for it.
// Crysis 2 reads the state incorrectly, so there is a separate library for it.

if (toReturn == ERROR_SUCCESS && ArduinoWork)
switch (GameMode)
{
case 1: //Wheel
{
if (GameMode == 1)
pState->Gamepad.sThumbLX = ToLeftStick(OffsetYPR(ArduinoData[1], YRPOffset[0])) * -1;
break;
}
case 2: //FPS
{
float NewX = OffsetYPR(ArduinoData[1], YRPOffset[0]) * -1;
float NewY = OffsetYPR(ArduinoData[3], YRPOffset[2]);

else if (GameMode == 2) {
DeltaYRP[0] = OffsetYPR(ArduinoData[1], LastYRP[0]) * -1;
//DeltaYRP[1] = OffsetYPR(ArduinoData[2], LastYRP[1]);
DeltaYRP[2] = OffsetYPR(ArduinoData[3], LastYRP[2]);

if (pState->Gamepad.bLeftTrigger == 0) {
if (OnlyTrigger == false)
MoveMouse(NewX * SensX, NewY * SensY);
} else
MoveMouse(NewX * SensX * TriggerSens, NewY * SensY * TriggerSens);

Centering();

break;
}
case 3: //Fully emulation, experimental
{
pState->Gamepad.sThumbRX = ThumbFix(OffsetYPR(ArduinoData[1], YRPOffset[0]) * -750);
pState->Gamepad.sThumbRY = ThumbFix(OffsetYPR(ArduinoData[3], YRPOffset[2]) * -750);

Centering();
MoveMouse(DeltaYRP[0] * SensX, DeltaYRP[2] * SensY);
}
else
MoveMouse(DeltaYRP[0] * SensX * TriggerSens, DeltaYRP[2] * SensY * TriggerSens);

break;
}
/*case 4: //FPS, gyroscope offset, experimental
{
pState->Gamepad.sThumbRX = ThumbFix(myPState.Gamepad.sThumbRX + OffsetYPR(ArduinoData[1], YRPOffset[0]) * -182 * StickSensX); //StickSensX - 9
pState->Gamepad.sThumbRY = ThumbFix(myPState.Gamepad.sThumbRY + OffsetYPR(ArduinoData[3], YRPOffset[2]) * -182 * StickSensY); //StickSensX - 7
}*/
LastYRP[0] = ArduinoData[1];
//LastYRP[1] = ArduinoData[2];
LastYRP[2] = ArduinoData[3];

//pState->Gamepad.sThumbRX = ThumbFix(OffsetYPR(ArduinoData[1], YRPOffset[0]) * -750 + pState->Gamepad.sThumbRX);
//pState->Gamepad.sThumbRY = ThumbFix(OffsetYPR(ArduinoData[3], YRPOffset[2]) * -750 + pState->Gamepad.sThumbRY);
//Centering();
}

return toReturn;
Expand Down

0 comments on commit 1956798

Please sign in to comment.