Source-like movement

This commit is contained in:
2016-01-23 19:45:23 +01:00
parent 666ef6589e
commit 1ce9128da8
3 changed files with 56 additions and 25 deletions
@@ -3,6 +3,7 @@
#include "Core/System.h" #include "Core/System.h"
#include "Core/EPlayerSpawned.h" #include "Core/EPlayerSpawned.h"
#include "Input/FirstPersonInputController.h" #include "Input/FirstPersonInputController.h"
#include <imgui/imgui.h>
class PlayerMovementSystem : public ImpureSystem, PureSystem class PlayerMovementSystem : public ImpureSystem, PureSystem
{ {
+6 -7
View File
@@ -6,13 +6,12 @@
<Origin X="0" Y="0.772000015" Z="0"/> <Origin X="0" Y="0.772000015" Z="0"/>
<Size X="1" Y="1.60000002" Z="1"/> <Size X="1" Y="1.60000002" Z="1"/>
</c:AABB> </c:AABB>
<c:Collidable/> <c:Model/>
<c:Model> <c:Physics>
<Resource></Resource> <Gravity>false</Gravity>
</c:Model> </c:Physics>
<c:Physics/>
<c:Player> <c:Player>
<MovementSpeed>3</MovementSpeed> <MovementSpeed>7</MovementSpeed>
</c:Player> </c:Player>
<c:Team> <c:Team>
<Team> <Team>
@@ -20,7 +19,7 @@
</Team> </Team>
</c:Team> </c:Team>
<c:Transform> <c:Transform>
<Position X="0" Y="0.0264999978" Z="7.38143063"/> <Position X="0" Y="0" Z="7.38143063"/>
</c:Transform> </c:Transform>
</Components> </Components>
+49 -18
View File
@@ -36,29 +36,51 @@ void PlayerMovementSystem::Update(double dt)
glm::vec3& ori = cTransform["Orientation"]; glm::vec3& ori = cTransform["Orientation"];
ori.y += controller->Rotation().y; 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")) { if (player.HasComponent("Physics")) {
glm::vec3& velocity = player["Physics"]["Velocity"]; ComponentWrapper cPhysics = player["Physics"];
if (controller->Jumping() && !controller->Crouching() && velocity.y == 0.f) {
velocity.y += 4.f;
}
}
if (player.HasComponent("AABB")) { glm::vec3 wishDirection = controller->Movement() * glm::inverse(glm::quat(ori));
glm::vec3& size = player["AABB"]["Size"]; float wishSpeed;
if (controller->Crouching()) { if (controller->Crouching()) {
size = glm::vec3(1.f, 1.f, 1.f); wishSpeed = player["Player"]["CrouchSpeed"];
} else { } 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(); controller->Reset();
@@ -78,6 +100,15 @@ void PlayerMovementSystem::UpdateComponent(EntityWrapper& entity, ComponentWrapp
velocity.y -= 9.82f * (float)dt; 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"]; glm::vec3& position = cTransform["Position"];
position += velocity * (float)dt; position += velocity * (float)dt;
} }