Skip to content

Commit

Permalink
Add universal roomscale movement option
Browse files Browse the repository at this point in the history
  • Loading branch information
praydog committed Jul 10, 2023
1 parent 8846582 commit 1e633a8
Show file tree
Hide file tree
Showing 8 changed files with 139 additions and 1 deletion.
8 changes: 8 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -314,6 +314,8 @@ set(CMKR_TARGET sdk)
set(sdk_SOURCES "")

list(APPEND sdk_SOURCES
"shared/sdk/AActor.cpp"
"shared/sdk/APawn.cpp"
"shared/sdk/APlayerController.cpp"
"shared/sdk/CVar.cpp"
"shared/sdk/ConsoleManager.cpp"
Expand All @@ -339,6 +341,8 @@ list(APPEND sdk_SOURCES
"shared/sdk/UObjectBase.cpp"
"shared/sdk/UProperty.cpp"
"shared/sdk/Utility.cpp"
"shared/sdk/AActor.hpp"
"shared/sdk/APawn.hpp"
"shared/sdk/APlayerController.hpp"
"shared/sdk/CVar.hpp"
"shared/sdk/ConsoleManager.hpp"
Expand Down Expand Up @@ -419,6 +423,8 @@ set(CMKR_TARGET sdk-nolog)
set(sdk-nolog_SOURCES "")

list(APPEND sdk-nolog_SOURCES
"shared/sdk/AActor.cpp"
"shared/sdk/APawn.cpp"
"shared/sdk/APlayerController.cpp"
"shared/sdk/CVar.cpp"
"shared/sdk/ConsoleManager.cpp"
Expand All @@ -444,6 +450,8 @@ list(APPEND sdk-nolog_SOURCES
"shared/sdk/UObjectBase.cpp"
"shared/sdk/UProperty.cpp"
"shared/sdk/Utility.cpp"
"shared/sdk/AActor.hpp"
"shared/sdk/APawn.hpp"
"shared/sdk/APlayerController.hpp"
"shared/sdk/CVar.hpp"
"shared/sdk/ConsoleManager.hpp"
Expand Down
49 changes: 49 additions & 0 deletions shared/sdk/AActor.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
#include <vector>
#include "UObjectArray.hpp"

#include "AActor.hpp"

namespace sdk {
UClass* AActor::static_class() {
return sdk::find_uobject<UClass>(L"Class /Script/Engine.Actor");
}

bool AActor::set_actor_location(const glm::vec3& location, bool sweep, bool teleport) {
static const auto func = static_class()->find_function(L"K2_SetActorLocation");
static const auto fhitresult = sdk::find_uobject<UScriptStruct>(L"ScriptStruct /Script/Engine.HitResult");

// Need to dynamically allocate the params because of unknown FHitResult size
std::vector<uint8_t> params{};

// add a vec3
params.insert(params.end(), (uint8_t*)&location, (uint8_t*)&location + sizeof(glm::vec3));
// add a bool
params.insert(params.end(), (uint8_t*)&sweep, (uint8_t*)&sweep + sizeof(bool));
// align
params.insert(params.end(), 3, 0);
// add a FHitResult
params.insert(params.end(), fhitresult->get_struct_size(), 0);
// add a bool
params.insert(params.end(), (uint8_t*)&teleport, (uint8_t*)&teleport + sizeof(bool));
// add a bool
bool ret = false;
void* ret_ptr = &ret;
params.insert(params.end(), (uint8_t*)&ret_ptr, (uint8_t*)&ret_ptr + sizeof(void*));

this->process_event(func, params.data());

return ret;
}

glm::vec3 AActor::get_actor_location() {
static const auto func = static_class()->find_function(L"K2_GetActorLocation");

struct {
glm::vec3 result{};
} params;

this->process_event(func, &params);

return params.result;
};
}
18 changes: 18 additions & 0 deletions shared/sdk/AActor.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
#pragma once

#include "Math.hpp"

#include "UClass.hpp"

namespace sdk {
class AActor : public UObject {
public:
static UClass* static_class();

// it takes a hit result as out param but we dont care.
bool set_actor_location(const glm::vec3& location, bool sweep, bool teleport);
glm::vec3 get_actor_location();

protected:
};
}
9 changes: 9 additions & 0 deletions shared/sdk/APawn.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#include "UObjectArray.hpp"

#include "APawn.hpp"

namespace sdk {
UClass* APawn::static_class() {
return sdk::find_uobject<UClass>(L"Class /Script/Engine.Pawn");
};
}
12 changes: 12 additions & 0 deletions shared/sdk/APawn.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
#pragma once

#include "AActor.hpp"

namespace sdk {
class APawn : public AActor {
public:
static UClass* static_class();

protected:
};
}
2 changes: 2 additions & 0 deletions src/mods/VR.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2021,6 +2021,8 @@ void VR::on_draw_ui() {
ImGui::TreePop();
}

m_roomscale_movement->draw("Roomscale Movement");

ImGui::SetNextItemOpen(true, ImGuiCond_::ImGuiCond_Once);
if (ImGui::TreeNode("Decoupled Pitch")) {
m_decoupled_pitch->draw("Enabled");
Expand Down
6 changes: 6 additions & 0 deletions src/mods/VR.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -447,6 +447,10 @@ class VR : public Mod {
return m_2d_screen_mode->value();
}

bool is_roomscale_enabled() const {
return m_roomscale_movement->value();
}

private:
Vector4f get_position_unsafe(uint32_t index) const;
Vector4f get_velocity_unsafe(uint32_t index) const;
Expand Down Expand Up @@ -657,6 +661,7 @@ class VR : public Mod {
const ModToggle::Ptr m_decoupled_pitch_ui_adjust{ ModToggle::create(generate_name("DecoupledPitchUIAdjust"), true) };
const ModToggle::Ptr m_load_blueprint_code{ ModToggle::create(generate_name("LoadBlueprintCode"), false) };
const ModToggle::Ptr m_2d_screen_mode{ ModToggle::create(generate_name("2DScreenMode"), false) };
const ModToggle::Ptr m_roomscale_movement{ ModToggle::create(generate_name("RoomscaleMovement"), false) };

// Aim method and movement orientation are not the same thing, but they can both have the same options
const ModCombo::Ptr m_aim_method{ ModCombo::create(generate_name("AimMethod"), s_aim_method_names, AimMethod::GAME) };
Expand Down Expand Up @@ -720,6 +725,7 @@ class VR : public Mod {
*m_decoupled_pitch_ui_adjust,
*m_load_blueprint_code,
*m_2d_screen_mode,
*m_roomscale_movement,
*m_aim_method,
*m_movement_orientation,
*m_aim_speed,
Expand Down
36 changes: 35 additions & 1 deletion src/mods/vr/FFakeStereoRenderingHook.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,12 @@
#include <sdk/RHICommandList.hpp>
#include <sdk/UGameViewportClient.hpp>
#include <sdk/Globals.hpp>
#include <sdk/FName.hpp>
#include <sdk/UObjectArray.hpp>

#include <sdk/UGameplayStatics.hpp>
#include <sdk/APawn.hpp>
#include <sdk/APlayerController.hpp>

#include "Framework.hpp"
#include "Mods.hpp"
Expand Down Expand Up @@ -3997,9 +4003,15 @@ __forceinline void FFakeStereoRenderingHook::calculate_stereo_view_offset(
const auto new_rotation = glm::normalize(vqi_norm * current_hmd_rotation * current_eye_rotation_offset);
const auto eye_offset = glm::vec3{vr->get_eye_offset((VRRuntime::Eye)(true_index))};

const auto pos = glm::vec3{rotation_offset * ((vr->get_position(0) - vr->get_standing_origin()))};

const auto standing_delta = vr->get_position(0) - vr->get_standing_origin();
const auto standing_delta_flat = glm::vec3{standing_delta.x, 0, standing_delta.z};

const auto pos = glm::vec3{rotation_offset * standing_delta};
const auto pos_flat = glm::vec3{rotation_offset * standing_delta_flat};

const auto head_offset = quat_converter * (vqi_norm * (pos * world_scale));
const auto head_offset_flat = quat_converter * (vqi_norm * (pos_flat * world_scale));
const auto eye_separation = quat_converter * (glm::normalize(new_rotation) * (eye_offset * world_scale));

if (!has_double_precision) {
Expand Down Expand Up @@ -4029,6 +4041,28 @@ __forceinline void FFakeStereoRenderingHook::calculate_stereo_view_offset(
rot_d->roll = euler.z;
}
}

// Roomscale movement
if (true_index == 0 && vr->is_roomscale_enabled()) {
const auto world = sdk::UEngine::get()->get_world();

if (const auto controller = sdk::UGameplayStatics::get()->get_player_controller(world, 0); controller != nullptr) {
if (const auto pawn = controller->get_acknowledged_pawn(); pawn != nullptr) {
const auto pawn_pos = pawn->get_actor_location();
const auto new_pos = pawn_pos - head_offset_flat;

pawn->set_actor_location(new_pos, false, false);

// Recenter the standing origin
auto current_standing_origin = vr->get_standing_origin();
const auto hmd_pos = vr->get_position(0);
// dont touch the Y axis
current_standing_origin.x = hmd_pos.x;
current_standing_origin.z = hmd_pos.z;
vr->set_standing_origin(current_standing_origin);
}
}
}
}

if (!is_full_pass) {
Expand Down

0 comments on commit 1e633a8

Please sign in to comment.