Source-like movement
This commit is contained in:
@@ -3,6 +3,7 @@
|
||||
#include "Core/System.h"
|
||||
#include "Core/EPlayerSpawned.h"
|
||||
#include "Input/FirstPersonInputController.h"
|
||||
#include <imgui/imgui.h>
|
||||
|
||||
class PlayerMovementSystem : public ImpureSystem, PureSystem
|
||||
{
|
||||
|
||||
@@ -6,13 +6,12 @@
|
||||
<Origin X="0" Y="0.772000015" Z="0"/>
|
||||
<Size X="1" Y="1.60000002" Z="1"/>
|
||||
</c:AABB>
|
||||
<c:Collidable/>
|
||||
<c:Model>
|
||||
<Resource></Resource>
|
||||
</c:Model>
|
||||
<c:Physics/>
|
||||
<c:Model/>
|
||||
<c:Physics>
|
||||
<Gravity>false</Gravity>
|
||||
</c:Physics>
|
||||
<c:Player>
|
||||
<MovementSpeed>3</MovementSpeed>
|
||||
<MovementSpeed>7</MovementSpeed>
|
||||
</c:Player>
|
||||
<c:Team>
|
||||
<Team>
|
||||
@@ -20,7 +19,7 @@
|
||||
</Team>
|
||||
</c:Team>
|
||||
<c:Transform>
|
||||
<Position X="0" Y="0.0264999978" Z="7.38143063"/>
|
||||
<Position X="0" Y="0" Z="7.38143063"/>
|
||||
</c:Transform>
|
||||
</Components>
|
||||
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user