-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathVRControllerManager.cs
More file actions
129 lines (109 loc) · 5.25 KB
/
Copy pathVRControllerManager.cs
File metadata and controls
129 lines (109 loc) · 5.25 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
using Valve.VR;
using System;
using System.Threading.Tasks;
namespace InputFixer
{
public static class VRControllerManager
{
private static double threshold = ConfigManager.threshold;
private static double minActivation = 0.05; // Min activation to avoid load while stick is idle
private static bool isMenuOpen = false; // Track if SteamVR menu is open
private static CVRSystem vrSystem;
public static void Initialize()
{
EVRInitError initError = EVRInitError.None;
OpenVR.Init(ref initError, EVRApplicationType.VRApplication_Background);
if (initError != EVRInitError.None)
{
Console.WriteLine("Failed to initialize OpenVR: " + initError);
return;
}
Console.WriteLine("OpenVR initialized successfully.");
vrSystem = OpenVR.System;
if (vrSystem == null)
{
Console.WriteLine("Failed to get VR system.");
}
}
public static async Task ProcessControllerInput()
{
uint leftControllerIndex = OpenVR.System.GetTrackedDeviceIndexForControllerRole(ETrackedControllerRole.LeftHand);
while (leftControllerIndex == OpenVR.k_unTrackedDeviceIndexInvalid)
{
Console.WriteLine("Waiting for left controller...\n(Trying again in 10sec)");
await Task.Delay(10000); // Async sleep before looking for controller again
leftControllerIndex = OpenVR.System.GetTrackedDeviceIndexForControllerRole(ETrackedControllerRole.LeftHand);
}
Console.WriteLine($"Left controller found at index {leftControllerIndex}");
while (true)
{
if (vrSystem.IsTrackedDeviceConnected(leftControllerIndex))
{
VRControllerState_t controllerState = new VRControllerState_t();
try
{
if (vrSystem.GetControllerState(leftControllerIndex, ref controllerState, (uint)System.Runtime.InteropServices.Marshal.SizeOf(typeof(VRControllerState_t))))
{
if (isMenuOpen == true)
{
Console.WriteLine("SteamVR Menu closed, Controller State retrieved.");
isMenuOpen = false;
}
ProcessMovement(controllerState, controllerState.rAxis0.y, controllerState.rAxis0.x);
}
else
{
OSCManager.SendMovementInput("/input/Vertical", 0); // Stop movement if menu is opem
OSCManager.SendMovementInput("/input/Horizontal", 0);
if (isMenuOpen == false)
{
Console.WriteLine("Failed to get controller state, likely because SteamVR Menu is open");
}
isMenuOpen = true;
await Task.Delay(1000); // Retry after a delay
}
}
catch (Exception ex)
{
Console.WriteLine($"Error getting controller state: {ex.Message}");
}
}
else
{
Console.WriteLine("Left controller is not connected.");
break;
}
await Task.Delay(10); // Delay after each loop iteration
}
}
private static void ProcessMovement(VRControllerState_t controllerState, float originalY, float originalX)
{
// Tilt Detection Y-Axis
if (Math.Abs(originalY) > minActivation)
{
// Linearly scale the Y-axis input based on threshold, preserve sign
double verticalValue = Math.Sign(originalY) * (Math.Abs(originalY) / threshold);
// Ensure the value is capped between -1 and 1
verticalValue = Math.Clamp(verticalValue, -1, 1);
OSCManager.SendMovementInput("/input/Vertical", (float)verticalValue);
}
else if (Math.Abs(originalY) <= minActivation)
{
OSCManager.SendMovementInput("/input/Vertical", 0);
}
// Tilt Detection X-Axis
if (Math.Abs(originalX) > minActivation)
{
// Linearly scale the X-axis input based on threshold, preserve sign
double horizontalValue = Math.Sign(originalX) * (Math.Abs(originalX) / threshold);
// Ensure the value is capped between -1 and 1
horizontalValue = Math.Clamp(horizontalValue, -1, 1);
OSCManager.SendMovementInput("/input/Horizontal", (float)horizontalValue);
}
else if (Math.Abs(originalX) <= minActivation)
{
OSCManager.SendMovementInput("/input/Horizontal", 0);
}
}
}
}