Skip to content

Commit

Permalink
UObjectHook: Fix "Attach HMD" breaking on save
Browse files Browse the repository at this point in the history
  • Loading branch information
praydog committed Jul 21, 2024
1 parent c185d4e commit 0958bb7
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 7 deletions.
13 changes: 7 additions & 6 deletions src/mods/UObjectHook.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ void UObjectHook::MotionControllerStateBase::from_json(const nlohmann::json& dat

if (data.contains("hand")) {
hand = data["hand"].get<uint8_t>();
hand = hand % 2;
hand = hand % (uint8_t)MotionControllerStateBase::Hand::LAST;
}

if (data.contains("permanent") && data["permanent"].is_boolean()) {
Expand Down Expand Up @@ -615,13 +615,13 @@ void UObjectHook::tick_attachments(Rotator<float>* view_rotation, const float wo
for (auto& it : comps) {
auto& state = *it.second;

if (state.hand == 0) {
if (state.hand == (uint8_t)MotionControllerStateBase::Hand::LEFT) {
if (is_a_down_raw_left) {
state.adjusting = true;
} else if (!is_a_down_raw_left) {
state.adjusting = false;
}
} else if (state.hand == 1) {
} else if (state.hand == (uint8_t)MotionControllerStateBase::Hand::RIGHT) {
if (is_a_down_raw_right) {
state.adjusting = true;
} else if (!is_a_down_raw_right) {
Expand Down Expand Up @@ -718,9 +718,10 @@ void UObjectHook::tick_attachments(Rotator<float>* view_rotation, const float wo
glm::radians(-orig_rotation.z));
const auto orig_rotation_quat = glm::quat{orig_rotation_mat};

const auto& hand_rotation = state.hand != 2 ? (state.hand == 1 ? right_hand_rotation : left_hand_rotation) : head_rotation;
const auto& hand_position = state.hand != 2 ? (state.hand == 1 ? right_hand_position : left_hand_position) : final_position;
const auto& hand_euler = state.hand != 2 ? (state.hand == 1 ? right_hand_euler : left_hand_euler) : head_euler;
using Hand = MotionControllerStateBase::Hand;
const auto& hand_rotation = state.hand != Hand::HMD ? (state.hand == Hand::RIGHT ? right_hand_rotation : left_hand_rotation) : head_rotation;
const auto& hand_position = state.hand != Hand::HMD ? (state.hand == Hand::RIGHT ? right_hand_position : left_hand_position) : final_position;
const auto& hand_euler = state.hand != Hand::HMD ? (state.hand == Hand::RIGHT ? right_hand_euler : left_hand_euler) : head_euler;

const auto adjusted_rotation = hand_rotation * glm::inverse(state.rotation_offset);
const auto adjusted_euler = glm::degrees(utility::math::euler_angles_from_steamvr(adjusted_rotation));
Expand Down
9 changes: 8 additions & 1 deletion src/mods/UObjectHook.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,13 @@ class UObjectHook : public Mod {

public:
struct MotionControllerStateBase {
enum Hand : uint8_t {
LEFT = 0,
RIGHT,
HMD,
LAST
};

nlohmann::json to_json() const;
void from_json(const nlohmann::json& data);

Expand All @@ -95,7 +102,7 @@ class UObjectHook : public Mod {
// State that can be parsed from disk
glm::quat rotation_offset{glm::identity<glm::quat>()};
glm::vec3 location_offset{0.0f, 0.0f, 0.0f};
uint8_t hand{1}; // 2 == HMD
uint8_t hand{(uint8_t)Hand::RIGHT}; // 2 == HMD
bool permanent{false};
};

Expand Down

0 comments on commit 0958bb7

Please sign in to comment.