From b29cb675a0888a1dea81373e8b84f7ad637a928d Mon Sep 17 00:00:00 2001 From: praydog Date: Wed, 25 Oct 2023 21:33:48 -0700 Subject: [PATCH] UObjectHook: Fix bad rotation math for attachments --- src/mods/UObjectHook.cpp | 37 +++++++++++++++++++++++++++++++++---- src/mods/UObjectHook.hpp | 3 +++ src/mods/VR.hpp | 15 ++++++++++++--- 3 files changed, 48 insertions(+), 7 deletions(-) diff --git a/src/mods/UObjectHook.cpp b/src/mods/UObjectHook.cpp index 172a9ffa..aa81caf9 100644 --- a/src/mods/UObjectHook.cpp +++ b/src/mods/UObjectHook.cpp @@ -310,7 +310,15 @@ void UObjectHook::on_pre_calculate_stereo_view_offset(void* stereo_device, const const auto orig_position = comp->get_world_location(); const auto orig_rotation = comp->get_world_rotation(); - const auto adjusted_rotation = glm::normalize(view_quat_inverse_flat * ((rotation_offset * original_right_hand_rotation) * state.rotation_offset)); + // Convert orig_rotation to quat + const auto orig_rotation_mat = glm::yawPitchRoll( + glm::radians(-orig_rotation.y), + glm::radians(orig_rotation.x), + glm::radians(-orig_rotation.z)); + const auto orig_rotation_quat = glm::quat{orig_rotation_mat}; + + //const auto adjusted_rotation = glm::normalize(view_quat_inverse_flat * ((rotation_offset * original_right_hand_rotation) * state.rotation_offset)); + const auto adjusted_rotation = right_hand_rotation * glm::inverse(state.rotation_offset); const auto adjusted_euler = glm::degrees(utility::math::euler_angles_from_steamvr(adjusted_rotation)); const auto adjusted_location = right_hand_position + (quat_converter * (adjusted_rotation * state.location_offset)); @@ -318,7 +326,6 @@ void UObjectHook::on_pre_calculate_stereo_view_offset(void* stereo_device, const //const auto adjusted_location = final_position - (quat_converter * new_r); if (state.adjusting) { - //comp->set_world_rotation(right_hand_euler, false, false); // Create a temporary actor that visualizes how we're adjusting the component if (state.adjustment_visualizer == nullptr) { auto ugs = sdk::UGameplayStatics::get(); @@ -397,8 +404,12 @@ void UObjectHook::on_pre_calculate_stereo_view_offset(void* stereo_device, const glm::radians(orig_rotation.x), glm::radians(-orig_rotation.z)); - m_motion_controller_attached_components[comp].rotation_offset = glm::inverse(rotation_offset * original_right_hand_rotation); - m_motion_controller_attached_components[comp].location_offset = glm::inverse(glm::quat{mat_inverse}) * utility::math::ue4_to_glm(right_hand_position - orig_position); + const auto mq = glm::quat{mat_inverse}; + const auto mqi = glm::inverse(mq); + + //m_motion_controller_attached_components[comp].rotation_offset = glm::inverse(rotation_offset * original_right_hand_rotation); + m_motion_controller_attached_components[comp].rotation_offset = mqi * right_hand_rotation; + m_motion_controller_attached_components[comp].location_offset = mqi * utility::math::ue4_to_glm(right_hand_position - orig_position); //m_motion_controller_attached_components[comp].location_offset = rotation_offset * original_right_hand_position; } else { if (state.adjustment_visualizer != nullptr) { @@ -430,7 +441,25 @@ void UObjectHook::on_pre_calculate_stereo_view_offset(void* stereo_device, const } } } +} + +void UObjectHook::on_post_calculate_stereo_view_offset(void* stereo_device, const int32_t view_index, Rotator* view_rotation, + const float world_to_meters, Vector3f* view_location, bool is_double) +{ + if (!VR::get()->is_hmd_active()) { + return; + } + + std::shared_lock _{m_mutex}; + bool any_adjusting = false; + for (auto it : m_motion_controller_attached_components) { + if (it.second.adjusting) { + any_adjusting = true; + break; + } + } + VR::get()->set_aim_allowed(!any_adjusting); } std::future> sorting_task{}; diff --git a/src/mods/UObjectHook.hpp b/src/mods/UObjectHook.hpp index 216145f1..e4e97ffb 100644 --- a/src/mods/UObjectHook.hpp +++ b/src/mods/UObjectHook.hpp @@ -47,6 +47,9 @@ class UObjectHook : public Mod { void on_pre_calculate_stereo_view_offset(void* stereo_device, const int32_t view_index, Rotator* view_rotation, const float world_to_meters, Vector3f* view_location, bool is_double) override; + void on_post_calculate_stereo_view_offset(void* stereo_device, const int32_t view_index, Rotator* view_rotation, + const float world_to_meters, Vector3f* view_location, bool is_double) override; + private: bool exists_unsafe(sdk::UObjectBase* object) const { return m_objects.contains(object); diff --git a/src/mods/VR.hpp b/src/mods/VR.hpp index 156dd847..2eb14d64 100644 --- a/src/mods/VR.hpp +++ b/src/mods/VR.hpp @@ -381,7 +381,15 @@ class VR : public Mod { m_decoupled_pitch->value() = value; } + void set_aim_allowed(bool value) { + m_aim_temp_disabled = !value; + } + AimMethod get_aim_method() const { + if (m_aim_temp_disabled) { + return AimMethod::GAME; + } + return (AimMethod)m_aim_method->value(); } @@ -398,16 +406,16 @@ class VR : public Mod { } bool is_any_aim_method_active() const { - return m_aim_method->value() > AimMethod::GAME; + return m_aim_method->value() > AimMethod::GAME && !m_aim_temp_disabled; } bool is_headlocked_aim_enabled() const { - return m_aim_method->value() == AimMethod::HEAD; + return m_aim_method->value() == AimMethod::HEAD && !m_aim_temp_disabled; } bool is_controller_aim_enabled() const { const auto value = m_aim_method->value(); - return value == AimMethod::LEFT_CONTROLLER || value == AimMethod::RIGHT_CONTROLLER || value == AimMethod::TWO_HANDED_LEFT || value == AimMethod::TWO_HANDED_RIGHT; + return !m_aim_temp_disabled && (value == AimMethod::LEFT_CONTROLLER || value == AimMethod::RIGHT_CONTROLLER || value == AimMethod::TWO_HANDED_LEFT || value == AimMethod::TWO_HANDED_RIGHT); } bool is_controller_movement_enabled() const { @@ -789,6 +797,7 @@ class VR : public Mod { bool m_init_finished{false}; bool m_has_hw_scheduling{false}; // hardware accelerated GPU scheduling bool m_spoofed_gamepad_connection{false}; + bool m_aim_temp_disabled{false}; struct { bool draw{false};