diff --git a/include/Game/Systems/PlayerMovementSystem.h b/include/Game/Systems/PlayerMovementSystem.h index 72f3b879..f39740ec 100644 --- a/include/Game/Systems/PlayerMovementSystem.h +++ b/include/Game/Systems/PlayerMovementSystem.h @@ -3,6 +3,7 @@ #include "Core/System.h" #include "Core/EPlayerSpawned.h" #include "Input/FirstPersonInputController.h" +#include class PlayerMovementSystem : public ImpureSystem, PureSystem { diff --git a/resources/Schema/Entities/Player.xml b/resources/Schema/Entities/Player.xml index 4398dcc1..58550357 100644 --- a/resources/Schema/Entities/Player.xml +++ b/resources/Schema/Entities/Player.xml @@ -6,13 +6,12 @@ - - - - - + + + false + - 3 + 7 @@ -20,7 +19,7 @@ - + diff --git a/src/Game/Systems/PlayerMovementSystem.cpp b/src/Game/Systems/PlayerMovementSystem.cpp index c5b55cde..ae54d684 100644 --- a/src/Game/Systems/PlayerMovementSystem.cpp +++ b/src/Game/Systems/PlayerMovementSystem.cpp @@ -36,29 +36,51 @@ void PlayerMovementSystem::Update(double dt) glm::vec3& ori = cTransform["Orientation"]; ori.y += controller->Rotation().y; - glm::vec3& pos = cTransform["Position"]; - float speed; - if (controller->Crouching()) { - speed = player["Player"]["CrouchSpeed"]; - } else { - speed = player["Player"]["MovementSpeed"]; - } - pos += controller->Movement() * glm::inverse(glm::quat(ori)) * speed * (float)dt; - if (player.HasComponent("Physics")) { - glm::vec3& velocity = player["Physics"]["Velocity"]; - if (controller->Jumping() && !controller->Crouching() && velocity.y == 0.f) { - velocity.y += 4.f; - } - } + ComponentWrapper cPhysics = player["Physics"]; - if (player.HasComponent("AABB")) { - glm::vec3& size = player["AABB"]["Size"]; + glm::vec3 wishDirection = controller->Movement() * glm::inverse(glm::quat(ori)); + float wishSpeed; if (controller->Crouching()) { - size = glm::vec3(1.f, 1.f, 1.f); + wishSpeed = player["Player"]["CrouchSpeed"]; } else { - size = glm::vec3(1.f, 1.6f, 1.f); + wishSpeed = player["Player"]["MovementSpeed"]; } + glm::vec3& velocity = cPhysics["Velocity"]; + ImGui::Text("velocity: (%f, %f, %f)", velocity.x, velocity.y, velocity.z); + ImGui::Text("wishDirection: (%f, %f, %f)", wishDirection.x, wishDirection.y, wishDirection.z); + float currentSpeedProj = glm::dot(velocity, wishDirection); + float addSpeed = wishSpeed - currentSpeedProj; + ImGui::Text("currentSpeedProj: %f", currentSpeedProj); + ImGui::Text("wishSpeed: %f", wishSpeed); + ImGui::Text("addSpeed: %f", addSpeed); + + if (addSpeed > 0) { + static float accel = 15.f; + ImGui::InputFloat("accel", &accel); + float actualAccel = accel; + static float surfaceFriction = 1.f; + ImGui::InputFloat("surfaceFriction", &surfaceFriction); + float accelerationSpeed = actualAccel * (float)dt * wishSpeed * surfaceFriction; + accelerationSpeed = glm::min(accelerationSpeed, addSpeed); + velocity += accelerationSpeed * wishDirection; + ImGui::Text("velocity: (%f, %f, %f) |%f|", velocity.x, velocity.y, velocity.z, glm::length(velocity)); + } + //pos += controller->Movement() * glm::inverse(glm::quat(ori)) * speed * (float)dt; + + //glm::vec3& velocity = player["Physics"]["Velocity"]; + //if (controller->Jumping() && !controller->Crouching() && velocity.y == 0.f) { + // velocity.y += 4.f; + //} + + //if (player.HasComponent("AABB")) { + // glm::vec3& size = player["AABB"]["Size"]; + // if (controller->Crouching()) { + // size = glm::vec3(1.f, 1.f, 1.f); + // } else { + // size = glm::vec3(1.f, 1.6f, 1.f); + // } + //} } controller->Reset(); @@ -78,6 +100,15 @@ void PlayerMovementSystem::UpdateComponent(EntityWrapper& entity, ComponentWrapp velocity.y -= 9.82f * (float)dt; } + // Ground friction + float speed = glm::length(velocity); + static float groundFriction = 7.f; + ImGui::InputFloat("groundFriction", &groundFriction); + if (speed > 0) { + float drop = speed * groundFriction * (float)dt; + velocity *= glm::max(speed - drop, 0.f) / speed; + } + glm::vec3& position = cTransform["Position"]; position += velocity * (float)dt; }