Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix player direction when teleported #59

Merged
merged 1 commit into from
Jan 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 18 additions & 5 deletions code/client/src/core/modules/human.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#include "human.h"

#include <flecs/flecs.h>
#include <glm/glm.hpp>

#include "game/helpers/controls.h"
#include "game/helpers/human.h"
Expand Down Expand Up @@ -464,30 +465,42 @@ namespace MafiaMP::Core::Modules {
if (e.get<Interpolated>()) {
auto interp = e.get_mut<Interpolated>();
// 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);
transform.SetPos(newPos);
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<uintptr_t> 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);
}
}
Expand Down
2 changes: 1 addition & 1 deletion code/client/src/core/ui/entity_browser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

Expand Down
2 changes: 1 addition & 1 deletion code/client/src/core/ui/player_debug.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

Expand Down
2 changes: 1 addition & 1 deletion code/client/src/core/ui/vehicle_debug.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

Expand Down
13 changes: 10 additions & 3 deletions gamemode/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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", () => {
Expand Down Expand Up @@ -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) => {
Expand Down