From b4411429af07744888d407edb126e442505d1a46 Mon Sep 17 00:00:00 2001 From: Deewarz Date: Sun, 21 Jan 2024 15:42:57 +0100 Subject: [PATCH] Fix player direction when teleported --- code/client/src/core/modules/human.cpp | 23 +++++++++++++++++----- code/client/src/core/ui/entity_browser.cpp | 2 +- code/client/src/core/ui/player_debug.cpp | 2 +- code/client/src/core/ui/vehicle_debug.cpp | 2 +- gamemode/index.js | 13 +++++++++--- 5 files changed, 31 insertions(+), 11 deletions(-) diff --git a/code/client/src/core/modules/human.cpp b/code/client/src/core/modules/human.cpp index 06e03294..c42b8e68 100644 --- a/code/client/src/core/modules/human.cpp +++ b/code/client/src/core/modules/human.cpp @@ -3,6 +3,7 @@ #include "human.h" #include +#include #include "game/helpers/controls.h" #include "game/helpers/human.h" @@ -464,14 +465,14 @@ namespace MafiaMP::Core::Modules { if (e.get()) { auto interp = e.get_mut(); // todo reset lerp - const auto humanPos = trackingData->human->GetPos(); const auto humanRot = trackingData->human->GetRot(); - interp->interpolator.GetPosition()->SetTargetValue({humanPos.x, humanPos.y, humanPos.z}, tr->pos, MafiaMP::Core::gApplication->GetTickInterval()); + const auto humanPos = trackingData->human->GetPos(); interp->interpolator.GetRotation()->SetTargetValue({humanRot.w, humanRot.x, humanRot.y, humanRot.z}, tr->rot, MafiaMP::Core::gApplication->GetTickInterval()); + interp->interpolator.GetPosition()->SetTargetValue({humanPos.x, humanPos.y, humanPos.z}, tr->pos, MafiaMP::Core::gApplication->GetTickInterval()); } else if (gApplication->GetLocalPlayer() != e.id()) { - SDK::ue::sys::math::C_Vector newPos = {tr->pos.x, tr->pos.y, tr->pos.z}; SDK::ue::sys::math::C_Quat newRot = {tr->rot.x, tr->rot.y, tr->rot.z, tr->rot.w}; + SDK::ue::sys::math::C_Vector newPos = {tr->pos.x, tr->pos.y, tr->pos.z}; SDK::ue::sys::math::C_Matrix transform = {}; transform.Identity(); transform.SetRot(newRot); @@ -479,15 +480,27 @@ namespace MafiaMP::Core::Modules { trackingData->human->SetTransform(transform); } else { - SDK::ue::sys::math::C_Vector newPos = {tr->pos.x, tr->pos.y, tr->pos.z}; SDK::ue::sys::math::C_Quat newRot = {tr->rot.x, tr->rot.y, tr->rot.z, tr->rot.w}; + SDK::ue::sys::math::C_Vector newPos = {tr->pos.x, tr->pos.y, tr->pos.z}; SDK::ue::sys::math::C_Matrix transform = {}; transform.Identity(); transform.SetRot(newRot); transform.SetPos(newPos); + /** + * We use TeleportPlayer because it preloads the world (collisions). + * It also teleports the car the player is in if applicable. + * + * Since we don't know the player direction required by TeleportPlayer, + * we get it from the player rotation. + * + * TODO: preload the world instead of using TeleportPlayer + */ + glm::mat4 rotMatrix = glm::mat4_cast(tr->rot); + SDK::ue::sys::math::C_Vector newDir = {-rotMatrix[1][0], rotMatrix[1][1], rotMatrix[1][2]}; SDK::ue::C_CntPtr syncObject2; - SDK::GetPlayerTeleportModule()->TeleportPlayer(syncObject2, newPos, newPos, true, true, true, false); + SDK::GetPlayerTeleportModule()->TeleportPlayer(syncObject2, newPos, newDir, true, true, true, false); + trackingData->human->SetTransform(transform); } } diff --git a/code/client/src/core/ui/entity_browser.cpp b/code/client/src/core/ui/entity_browser.cpp index 7a47bcb2..70e38692 100644 --- a/code/client/src/core/ui/entity_browser.cpp +++ b/code/client/src/core/ui/entity_browser.cpp @@ -161,7 +161,7 @@ namespace MafiaMP::Core::UI { auto entityPos = inspectedEntity->GetPos(); auto entityDir = inspectedEntity->GetDir(); - if (ImGui::DragFloat3("Pos", (float *)&entityPos, 0.1f, -2000.0f, 2000.0f)) { + if (ImGui::DragFloat3("Pos", (float *)&entityPos, 0.1f, -4500.0f, 4500.0f)) { inspectedEntity->SetPos(entityPos); } diff --git a/code/client/src/core/ui/player_debug.cpp b/code/client/src/core/ui/player_debug.cpp index 5d6d64ab..fea1f54e 100644 --- a/code/client/src/core/ui/player_debug.cpp +++ b/code/client/src/core/ui/player_debug.cpp @@ -24,7 +24,7 @@ namespace MafiaMP::Core::UI { ImGui::Begin("Player debug", &_visible); auto position = pActivePlayer->GetPos(); - if (ImGui::DragFloat3("Pos", (float *)&position, 0.1f, -2000.0f, 2000.0f)) { + if (ImGui::DragFloat3("Pos", (float *)&position, 0.1f, -4500.0f, 4500.0f)) { pActivePlayer->SetPos(position); } diff --git a/code/client/src/core/ui/vehicle_debug.cpp b/code/client/src/core/ui/vehicle_debug.cpp index ce3684d9..d5276f0a 100644 --- a/code/client/src/core/ui/vehicle_debug.cpp +++ b/code/client/src/core/ui/vehicle_debug.cpp @@ -23,7 +23,7 @@ namespace MafiaMP::Core::UI { auto currentVehicle = currentCar->GetVehicle(); auto position = currentCar->GetPos(); - if (ImGui::DragFloat3("Pos", (float *)&position, 0.1f, -2000.0f, 2000.0f)) { + if (ImGui::DragFloat3("Pos", (float *)&position, 0.1f, -4500.0f, 4500.0f)) { currentCar->SetPos(position); } diff --git a/gamemode/index.js b/gamemode/index.js index 39e99e46..9e02b819 100644 --- a/gamemode/index.js +++ b/gamemode/index.js @@ -275,7 +275,7 @@ const weatherSets = ["mm_030_molotov_cp_010_cine", "mm_150_boat_cp_010", "mm_210 const SPAWN_POINT = { pos: sdk.Vector3(-985.871, -299.401, 2.1), - rot: sdk.Quaternion(0.301, 0.0, 0.0, -0.954), + rot: sdk.Quaternion(0.291, 0, 0, -0.957), }; sdk.on("gamemodeLoaded", () => { @@ -417,8 +417,15 @@ RegisterChatCommand("pos", (player, message, command, args) => { .toFixed(3) .replace(/\.?0+$/, ""); - player.sendChat(`[SERVER] Position: ${parse(pos.x)}, ${parse(pos.y)}, ${parse(pos.z)}`); - player.sendChat(`[SERVER] Rotation: ${parse(rot.w)}, ${parse(rot.x)}, ${parse(rot.y)}, ${parse(rot.z)}`); + const posParsedMessage = `${parse(pos.x)}, ${parse(pos.y)}, ${parse(pos.z)}` + const rotParsedMessage = `${parse(rot.w)}, ${parse(rot.x)}, ${parse(rot.y)}, ${parse(rot.z)}` + + player.sendChat(`[SERVER] Position: ${posParsedMessage}`); + player.sendChat(`[SERVER] Rotation: ${rotParsedMessage}`); + + // Log in console for easy copy-paste + console.log(`[GAMEMODE] Player Position: ${posParsedMessage}`); + console.log(`[GAMEMODE] Player Rotation: ${rotParsedMessage}`); }); RegisterChatCommand("veh", (player, message, command, args) => {