From 2975abe5318b77b9f2b52c9ed3dea903bbb602e7 Mon Sep 17 00:00:00 2001 From: OneUp Date: Tue, 16 Jul 2024 20:59:45 -0500 Subject: [PATCH] Add configurable Pose reset key --- README.md | 3 +- vrto3d/src/hmd_device_driver.cpp | 56 ++++++++++++++++++- vrto3d/src/hmd_device_driver.h | 4 ++ .../resources/settings/default.vrsettings | 1 + 4 files changed, 62 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index f70b3a0..db044b5 100644 --- a/README.md +++ b/README.md @@ -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` | diff --git a/vrto3d/src/hmd_device_driver.cpp b/vrto3d/src/hmd_device_driver.cpp index 406424b..bdd54b2 100644 --- a/vrto3d/src/hmd_device_driver.cpp +++ b/vrto3d/src/hmd_device_driver.cpp @@ -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()) { @@ -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; @@ -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); @@ -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)); @@ -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 @@ -873,3 +919,11 @@ void StereoDisplayComponent::SetHeight() else config_.hmd_height = user_height; } + +//----------------------------------------------------------------------------- +// Purpose: Toggle Reset off +//----------------------------------------------------------------------------- +void StereoDisplayComponent::SetReset() +{ + config_.pose_reset = false; +} diff --git a/vrto3d/src/hmd_device_driver.h b/vrto3d/src/hmd_device_driver.h index b3543d1..aafdb5b 100644 --- a/vrto3d/src/hmd_device_driver.h +++ b/vrto3d/src/hmd_device_driver.h @@ -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; @@ -98,6 +101,7 @@ class StereoDisplayComponent : public vr::IVRDisplayComponent void AdjustPitch(float& currentPitch); void AdjustYaw(float& currentYaw); void SetHeight(); + void SetReset(); private: StereoDisplayDriverConfiguration config_; diff --git a/vrto3d/vrto3d/resources/settings/default.vrsettings b/vrto3d/vrto3d/resources/settings/default.vrsettings index 3fc8d85..121e8e4 100644 --- a/vrto3d/vrto3d/resources/settings/default.vrsettings +++ b/vrto3d/vrto3d/resources/settings/default.vrsettings @@ -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,