Skip to content

Commit

Permalink
UObjectHook: Fix bad rotation math for attachments
Browse files Browse the repository at this point in the history
  • Loading branch information
praydog committed Oct 26, 2023
1 parent ee33489 commit b29cb67
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 7 deletions.
37 changes: 33 additions & 4 deletions src/mods/UObjectHook.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -310,15 +310,22 @@ 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));
//const auto new_r = (glm::normalize(view_quat_inverse_flat) * (((rotation_offset * original_right_hand_position) - (rotation_offset * state.location_offset)) * world_to_meters));
//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();
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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<float>* 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<std::vector<sdk::UClass*>> sorting_task{};
Expand Down
3 changes: 3 additions & 0 deletions src/mods/UObjectHook.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,9 @@ class UObjectHook : public Mod {
void on_pre_calculate_stereo_view_offset(void* stereo_device, const int32_t view_index, Rotator<float>* 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<float>* 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);
Expand Down
15 changes: 12 additions & 3 deletions src/mods/VR.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}

Expand All @@ -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 {
Expand Down Expand Up @@ -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};
Expand Down

0 comments on commit b29cb67

Please sign in to comment.