From 81858bc7f83790a8c2acca0a52e9a665f386b9cb Mon Sep 17 00:00:00 2001 From: MihailRis Date: Fri, 13 Mar 2026 21:31:20 +0300 Subject: [PATCH] feat: applying Y velocity --- src/physics/Hitbox.hpp | 1 - src/physics/PhysicsSolver.cpp | 42 ++++++++++++----------------------- src/physics/PhysicsSolver.hpp | 9 ++++---- 3 files changed, 18 insertions(+), 34 deletions(-) diff --git a/src/physics/Hitbox.hpp b/src/physics/Hitbox.hpp index 9d2213572..476e1a4aa 100644 --- a/src/physics/Hitbox.hpp +++ b/src/physics/Hitbox.hpp @@ -61,7 +61,6 @@ struct Hitbox { float friction = 1.0f; float verticalDamping = 1.0f; bool grounded = false; - bool yCollided = false; float gravityScale = 1.0f; bool crouching = false; float stepHeight = 0.5f; diff --git a/src/physics/PhysicsSolver.cpp b/src/physics/PhysicsSolver.cpp index bd5da28ed..939caf303 100644 --- a/src/physics/PhysicsSolver.cpp +++ b/src/physics/PhysicsSolver.cpp @@ -119,7 +119,9 @@ static void calc_collision( } } -bool PhysicsSolver::calcCollisionNegY(Hitbox& hitbox, const glm::vec3& half) { +bool PhysicsSolver::calcCollisionNegY( + Hitbox& hitbox, const glm::vec3& half, float dt +) { auto& pos = hitbox.position; auto& vel = hitbox.velocity; @@ -140,8 +142,8 @@ bool PhysicsSolver::calcCollisionNegY(Hitbox& hitbox, const glm::vec3& half) { pos.y = newy; } hitbox.groundVelocity = box->position - box->prevPosition; - if (vel.y < 0.0f) { - vel.y = 0.0f; + if (vel.y < hitbox.groundVelocity.y / dt) { + vel.y = hitbox.groundVelocity.y / dt * (1.0f - E); if (hitbox.groundMaterial.empty() && !box->material.empty()) { hitbox.groundMaterial = box->material; } @@ -185,9 +187,9 @@ void PhysicsSolver::calcCollisions( glm::vec3& vel, glm::vec3& pos, const glm::vec3& half, - float stepHeight + float stepHeight, + float dt ) { - hitbox.yCollided = false; stepHeight = calc_step_height(chunks, pos, half, stepHeight); auto prevPos = pos; @@ -202,7 +204,7 @@ void PhysicsSolver::calcCollisions( calc_collision<2, 1, 0, 1>(hitbox, chunks, solidHitboxes, half, stepHeight); pos.x = xpos; - if (calcCollisionNegY(hitbox, half)) { + if (calcCollisionNegY(hitbox, half, dt)) { hitbox.grounded = true; } @@ -221,7 +223,6 @@ void PhysicsSolver::calcCollisions( if (pos.y >= newy) { vel.y = 0.0f; pos.y = newy; - hitbox.yCollided = true; } break; } @@ -245,10 +246,6 @@ void PhysicsSolver::calcCollisions( } } - if (hitbox.yCollided) { - pos.y = prevPos.y; - } - // step on if (stepHeight > 0.0 && vel.y <= 0.0f) { AABB boxAABB = AABB(pos - half, pos + half); @@ -287,18 +284,8 @@ void PhysicsSolver::calcCollisions( } void PhysicsSolver::calcSubstep( - Hitbox& hitbox, - glm::vec3& vel, - glm::vec3& pos, - float dt, - int substeps + Hitbox& hitbox, glm::vec3& vel, glm::vec3& pos, float dt ) { - if (glm::length2(hitbox.groundVelocity) > 1e-6f) { - if (hitbox.groundVelocity.y < 0.0f) { - pos.y += hitbox.groundVelocity.y; - } - } - auto initpos = pos; auto half = hitbox.getHalfSize(); float gravityScale = hitbox.gravityScale; @@ -309,7 +296,9 @@ void PhysicsSolver::calcSubstep( vel, pos, half, - (hitbox.prevGrounded && gravityScale > 0.0f) ? hitbox.stepHeight : 0.0f + (hitbox.prevGrounded && gravityScale > 0.0f) ? hitbox.stepHeight + : 0.0f, + dt ); } @@ -375,9 +364,8 @@ void PhysicsSolver::step( for (uint i = 0; i < substeps; i++) { for (auto hitbox : hitboxes) { glm::vec3& pos = hitbox->position; - glm::vec3& vel = hitbox->velocity; hitbox->prevPosition = hitbox->position; - calcSubstep(*hitbox, vel, pos, dt, substeps); + calcSubstep(*hitbox, hitbox->velocity, pos, dt); } } @@ -392,9 +380,7 @@ void PhysicsSolver::step( if (hitbox->verticalDamping > 0.0f) { vel.y /= 1.0f + delta * linearDamping * hitbox->verticalDamping; } - if (hitbox->prevGrounded && !hitbox->grounded) { - auto appliedVelocity = hitbox->groundVelocity / dt; - vel += appliedVelocity; + if (!hitbox->grounded) { hitbox->groundVelocity = {}; } diff --git a/src/physics/PhysicsSolver.hpp b/src/physics/PhysicsSolver.hpp index e7b5138a5..e616696ca 100644 --- a/src/physics/PhysicsSolver.hpp +++ b/src/physics/PhysicsSolver.hpp @@ -44,14 +44,13 @@ private: glm::vec3& vel, glm::vec3& pos, const glm::vec3& half, - float stepHeight + float stepHeight, + float dt ); - void calcSubstep( - Hitbox& hitbox, glm::vec3& vel, glm::vec3& pos, float dt, int substeps - ); + void calcSubstep(Hitbox& hitbox, glm::vec3& vel, glm::vec3& pos, float dt); - bool calcCollisionNegY(Hitbox& hitbox, const glm::vec3& half); + bool calcCollisionNegY(Hitbox& hitbox, const glm::vec3& half, float dt); void updateSensors(Hitbox& hitbox); };