Skip to content

Commit

Permalink
Add configurable Pose reset key
Browse files Browse the repository at this point in the history
  • Loading branch information
oneup03 committed Jul 17, 2024
1 parent 7534715 commit 2975abe
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 2 deletions.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@ Windows-only solution, but there are other solutions on Linux like MonadoVR.
| `display_frequency` | `float` | The display refresh rate, in Hz. | `60.0` |
| `pitch_enable` | `bool` | Enables or disables Controller right stick y-axis mapped to HMD Pitch | `false` |
| `yaw_enable` | `bool` | Enables or disables Controller right stick x-axis mapped to HMD Yaw | `false` |
| `ctrl_toggle_key` | `string`| The [Virtual-Key Code](https://github.com/oneup03/VRto3D/blob/main/vrto3d/src/key_mappings.h) to toggle Pitch and Yaw emulation on/off when they are enabled | `XINPUT_GAMEPAD_RIGHT_THUMB` |
| `pose_reset_key` | `string`| The [Virtual-Key Code](https://github.com/oneup03/VRto3D/blob/main/vrto3d/src/key_mappings.h) to reset the HMD position and orientation | `"VK_NUMPAD7"` |
| `ctrl_toggle_key` | `string`| The Virtual-Key Code to toggle Pitch and Yaw emulation on/off when they are enabled | `"XINPUT_GAMEPAD_RIGHT_THUMB"` |
| `pitch_radius` | `float` | Radius of curvature for the HMD to move along. Useful in 3rd person games | `0.0` |
| `ctrl_deadzone` | `float` | Controller Deadzone | `0.05` |
| `ctrl_sensitivity` | `float` | Controller Sensitivity | `1.0` |
Expand Down
56 changes: 55 additions & 1 deletion vrto3d/src/hmd_device_driver.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,17 @@ MockControllerDeviceDriver::MockControllerDeviceDriver()
// Controller settings
display_configuration.pitch_enable = vrs->GetBool(stereo_display_settings_section, "pitch_enable");
display_configuration.yaw_enable = vrs->GetBool(stereo_display_settings_section, "yaw_enable");
char pose_reset_key[1024];
vrs->GetString(stereo_display_settings_section, "pose_reset_key", pose_reset_key, sizeof(pose_reset_key));
if (VirtualKeyMappings.find(pose_reset_key) != VirtualKeyMappings.end()) {
display_configuration.pose_reset_key = VirtualKeyMappings[pose_reset_key];
display_configuration.reset_xinput = false;
}
else if (XInputMappings.find(pose_reset_key) != XInputMappings.end()) {
display_configuration.pose_reset_key = XInputMappings[pose_reset_key];
display_configuration.reset_xinput = true;
}
display_configuration.pose_reset = false;
char ctrl_toggle_key[1024];
vrs->GetString(stereo_display_settings_section, "ctrl_toggle_key", ctrl_toggle_key, sizeof(ctrl_toggle_key));
if (VirtualKeyMappings.find(ctrl_toggle_key) != VirtualKeyMappings.end()) {
Expand Down Expand Up @@ -314,6 +325,7 @@ void MockControllerDeviceDriver::DebugRequest( const char *pchRequest, char *pch
vr::DriverPose_t MockControllerDeviceDriver::GetPose()
{
static const float updateInterval = 1.0f / stereo_display_component_->GetConfig().display_frequency; // Update interval in seconds
static const float radius = stereo_display_component_->GetConfig().pitch_radius; // Configurable radius for pitch
static float currentPitch = 0.0f; // Keep track of the current pitch
static float currentYaw = 0.0f; // Keep track of the current yaw
static float lastPitch = 0.0f;
Expand All @@ -340,9 +352,26 @@ vr::DriverPose_t MockControllerDeviceDriver::GetPose()
stereo_display_component_->AdjustYaw(currentYaw);
}

// Reset Pose to origin
if (stereo_display_component_->GetConfig().pose_reset)
{
currentPitch = 0.0f;
currentYaw = 0.0f;
lastPitch = 0.0f;
lastYaw = 0.0f;
lastPos[0] = 0.0f;
lastPos[1] = 0.0f;
lastPos[2] = 0.0f;
lastVel[0] = 0.0f;
lastVel[1] = 0.0f;
lastVel[2] = 0.0f;
lastAngVel[0] = 0.0f;
lastAngVel[1] = 0.0f;
stereo_display_component_->SetReset();
}

float pitchRadians = DEG_TO_RAD(currentPitch);
float yawRadians = currentYaw;
float radius = stereo_display_component_->GetConfig().pitch_radius; // Configurable radius for pitch

// Recompose the rotation quaternion from pitch and yaw
vr::HmdQuaternion_t pitchQuaternion = HmdQuaternion_FromEulerAngles(0, pitchRadians, 0);
Expand Down Expand Up @@ -694,6 +723,7 @@ void StereoDisplayComponent::CheckUserSettings(uint32_t device_index)
static bool pitch_set = config_.pitch_enable;
static bool yaw_set = config_.yaw_enable;
static int sleep_ctrl = 0;
static int sleep_rest = 0;

XINPUT_STATE state;
ZeroMemory(&state, sizeof(XINPUT_STATE));
Expand All @@ -719,6 +749,22 @@ void StereoDisplayComponent::CheckUserSettings(uint32_t device_index)
sleep_ctrl--;
}

if (((config_.reset_xinput && dwResult == ERROR_SUCCESS &&
((config_.pose_reset_key == XINPUT_GAMEPAD_LEFT_TRIGGER && state.Gamepad.bLeftTrigger > XINPUT_GAMEPAD_TRIGGER_THRESHOLD)
|| (config_.pose_reset_key == XINPUT_GAMEPAD_RIGHT_TRIGGER && state.Gamepad.bRightTrigger > XINPUT_GAMEPAD_TRIGGER_THRESHOLD)
|| (state.Gamepad.wButtons & config_.pose_reset_key)))
|| (!config_.reset_xinput && (GetAsyncKeyState(config_.pose_reset_key) & 0x8000)))
&& sleep_rest == 0)
{
sleep_rest = config_.sleep_count_max;
if (!config_.pose_reset) {
config_.pose_reset = true;
}
}
else if (sleep_rest > 0) {
sleep_rest--;
}

for (int i = 0; i < config_.num_user_settings; i++)
{
// Decrement the sleep count if it's greater than zero
Expand Down Expand Up @@ -873,3 +919,11 @@ void StereoDisplayComponent::SetHeight()
else
config_.hmd_height = user_height;
}

//-----------------------------------------------------------------------------
// Purpose: Toggle Reset off
//-----------------------------------------------------------------------------
void StereoDisplayComponent::SetReset()
{
config_.pose_reset = false;
}
4 changes: 4 additions & 0 deletions vrto3d/src/hmd_device_driver.h
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,9 @@ struct StereoDisplayDriverConfiguration

bool pitch_enable;
bool yaw_enable;
int32_t pose_reset_key;
bool reset_xinput;
bool pose_reset;
int32_t ctrl_toggle_key;
bool ctrl_xinput;
float pitch_radius;
Expand Down Expand Up @@ -98,6 +101,7 @@ class StereoDisplayComponent : public vr::IVRDisplayComponent
void AdjustPitch(float& currentPitch);
void AdjustYaw(float& currentYaw);
void SetHeight();
void SetReset();

private:
StereoDisplayDriverConfiguration config_;
Expand Down
1 change: 1 addition & 0 deletions vrto3d/vrto3d/resources/settings/default.vrsettings
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
"display_frequency": 60.0,
"pitch_enable": false,
"yaw_enable": false,
"pose_reset_key": "VK_NUMPAD7",
"ctrl_toggle_key": "XINPUT_GAMEPAD_RIGHT_THUMB",
"pitch_radius": 0.0,
"ctrl_deadzone": 0.05,
Expand Down

0 comments on commit 2975abe

Please sign in to comment.