Source-like movement
This commit is contained in:
@@ -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