fix: player flight linearDamping, add verticalDamping (bool)

This commit is contained in:
MihailRis
2024-07-08 23:53:25 +03:00
parent 3cafc39555
commit f3201b7742
5 changed files with 31 additions and 29 deletions
+1 -1
View File
@@ -248,7 +248,7 @@ void PlayerController::postUpdate(float delta, bool input, bool pause) {
updateFootsteps(delta); updateFootsteps(delta);
updateCamera(delta, input); updateCamera(delta, input);
} }
player->postUpdate(this->input, delta); player->postUpdate();
camControl.refresh(); camControl.refresh();
if (input) { if (input) {
updateInteraction(); updateInteraction();
+24 -26
View File
@@ -94,17 +94,20 @@ void Player::updateInput(PlayerInput& input, float delta) {
dir = glm::normalize(dir); dir = glm::normalize(dir);
hitbox->velocity += dir * speed * delta * 9.0f; hitbox->velocity += dir * speed * delta * 9.0f;
} }
}
void Player::postUpdate(PlayerInput& input, float delta) { hitbox->linearDamping = PLAYER_GROUND_DAMPING;
auto hitbox = getHitbox(); hitbox->verticalDamping = flight;
if (hitbox == nullptr) { if (flight){
return; hitbox->linearDamping = PLAYER_AIR_DAMPING;
if (input.jump){
hitbox->velocity.y += speed * delta * 9;
}
if (input.shift){
hitbox->velocity.y -= speed * delta * 9;
}
} }
position = hitbox->position; if (!hitbox->grounded) {
hitbox->linearDamping = PLAYER_AIR_DAMPING;
if (flight && hitbox->grounded) {
flight = false;
} }
if (input.jump && hitbox->grounded){ if (input.jump && hitbox->grounded){
@@ -121,25 +124,20 @@ void Player::postUpdate(PlayerInput& input, float delta) {
if (input.noclip) { if (input.noclip) {
noclip = !noclip; noclip = !noclip;
} }
hitbox->linearDamping = PLAYER_GROUND_DAMPING;
if (flight){
hitbox->linearDamping = PLAYER_AIR_DAMPING;
hitbox->velocity.y *= 1.0f - delta * 9;
if (input.jump){
hitbox->velocity.y += speed * delta * 9;
}
if (input.shift){
hitbox->velocity.y -= speed * delta * 9;
}
}
if (!hitbox->grounded) {
hitbox->linearDamping = PLAYER_AIR_DAMPING;
}
input.noclip = false; input.noclip = false;
input.flight = false; input.flight = false;
}
void Player::postUpdate() {
auto hitbox = getHitbox();
if (hitbox == nullptr) {
return;
}
position = hitbox->position;
if (flight && hitbox->grounded) {
flight = false;
}
if (spawnpoint.y <= 0.1) { if (spawnpoint.y <= 0.1) {
attemptToFindSpawnpoint(); attemptToFindSpawnpoint();
} }
@@ -293,4 +291,4 @@ void Player::convert(dynamic::Map* data, const ContentLUT* lut) {
Inventory::convert(inventory.get(), lut); Inventory::convert(inventory.get(), lut);
} }
} }
} }
+2 -2
View File
@@ -65,7 +65,7 @@ public:
void teleport(glm::vec3 position); void teleport(glm::vec3 position);
void updateEntity(); void updateEntity();
void updateInput(PlayerInput& input, float delta); void updateInput(PlayerInput& input, float delta);
void postUpdate(PlayerInput& input, float delta); void postUpdate();
void attemptToFindSpawnpoint(); void attemptToFindSpawnpoint();
@@ -104,4 +104,4 @@ public:
} }
}; };
#endif // SRC_OBJECTS_PLAYER_HPP_ #endif // SRC_OBJECTS_PLAYER_HPP_
+1
View File
@@ -41,6 +41,7 @@ struct Hitbox {
glm::vec3 halfsize; glm::vec3 halfsize;
glm::vec3 velocity; glm::vec3 velocity;
float linearDamping; float linearDamping;
bool verticalDamping = false;
bool grounded = false; bool grounded = false;
Hitbox(glm::vec3 position, glm::vec3 halfsize); Hitbox(glm::vec3 position, glm::vec3 halfsize);
+3
View File
@@ -47,6 +47,9 @@ void PhysicsSolver::step(
(prevGrounded && gravityScale > 0.0f) ? 0.5f : 0.0f); (prevGrounded && gravityScale > 0.0f) ? 0.5f : 0.0f);
} }
vel.x *= glm::max(0.0f, 1.0f - dt * linearDamping); vel.x *= glm::max(0.0f, 1.0f - dt * linearDamping);
if (hitbox->verticalDamping) {
vel.y *= glm::max(0.0f, 1.0f - dt * linearDamping);
}
vel.z *= glm::max(0.0f, 1.0f - dt * linearDamping); vel.z *= glm::max(0.0f, 1.0f - dt * linearDamping);
pos += vel * dt + gravity * gravityScale * dt * dt * 0.5f; pos += vel * dt + gravity * gravityScale * dt * dt * 0.5f;