From 103f3b2b051fab5b8bcc40addf854172e1de43bb Mon Sep 17 00:00:00 2001 From: Andrey Tazetdinov Date: Sat, 17 Oct 2026 08:56:52 +0200 Subject: [PATCH] fixes --- src/physics/PhysicsSolver.cpp | 59 ++++++++++++++++++++++++----------- 1 file changed, 41 insertions(+), 18 deletions(-) diff --git a/src/physics/PhysicsSolver.cpp b/src/physics/PhysicsSolver.cpp index 2c157c7aa..4b8e2ebb9 100644 --- a/src/physics/PhysicsSolver.cpp +++ b/src/physics/PhysicsSolver.cpp @@ -44,6 +44,16 @@ static glm::vec3 calc_collsion_velocity_result( return (mA * vA + mB * vB + a.elasticity * mB * (vB - vA)) / (mA + mB); } +static void integrate( + Hitbox& box, const glm::vec3& gravity, float dt, uint substeps +) { + float n = static_cast(substeps); + auto accel = gravity * box.gravityScale; + box.position += box.velocity * (dt * n) + + accel * (dt * dt * n * (n + 2.0f) * 0.5f); + box.velocity += accel * (dt * n); +} + static void push_body(Hitbox& box, int axis, float value, uint wakeSubstep) { if (box.velocity[axis] != value) { box.velocity[axis] = value; @@ -274,8 +284,9 @@ void PhysicsSolver::calcCollisions( float z = (pos.z - half.z + E) + iz; float y = (pos.y + half.y + E) - E; if (auto aabb = chunks.isObstacleAt(x, y, z, boxAABB)) { - float newy = std::floor(y) - half.y + aabb->min().y - E; - if (pos.y >= newy) { + float bottom = std::floor(y) + aabb->min().y; + float newy = bottom - half.y - E; + if (pos.y >= newy && pos.y - half.y < bottom) { vel.y = -hitbox.elasticity * vel.y; pos.y = newy; } @@ -660,6 +671,9 @@ void PhysicsSolver::rebuildNearbySolids(size_t index, float time) { static uint calc_substep_stride( const Hitbox& hitbox, const glm::vec3& gravity, float dt, uint left ) { + if (hitbox.crouching) { + return 1; + } float speed = glm::length(hitbox.velocity); float accel = glm::length(gravity) * glm::abs(hitbox.gravityScale) * 1.5f; float time; @@ -719,25 +733,34 @@ void PhysicsSolver::step( if (hitbox->nextSubstep > i) { continue; } - float substepTime = - dt * static_cast(i + 1 - hitbox->doneSubsteps); - glm::vec3& pos = hitbox->position; hitbox->prevPosition = hitbox->position; - hitbox->substepTime = substepTime; - calcSubstep( - *hitbox, - hitbox->velocity, - pos, - substepTime, - nearbySolids[j] - ); - scheduleSubstep(*hitbox, i + 1, substeps, dt); + hitbox->substepTime = + dt * static_cast(i + 1 - hitbox->doneSubsteps); + while (hitbox->doneSubsteps <= i) { + uint left = i + 1 - hitbox->doneSubsteps; + uint skip = calc_substep_stride(*hitbox, gravity, dt, left) - 1; + if (skip > 0) { + integrate(*hitbox, gravity, dt, skip); + hitbox->doneSubsteps += skip; + } + calcSubstep( + *hitbox, + hitbox->velocity, + hitbox->position, + dt, + nearbySolids[j] + ); + hitbox->doneSubsteps++; - auto moved = glm::abs(pos - nearbyOrigins[j]); - if (glm::max(moved.x, glm::max(moved.y, moved.z)) > - nearbyBudgets[j]) { - rebuildNearbySolids(j, dt * static_cast(substeps - i)); + auto moved = glm::abs(hitbox->position - nearbyOrigins[j]); + if (glm::max(moved.x, glm::max(moved.y, moved.z)) > + nearbyBudgets[j]) { + rebuildNearbySolids( + j, dt * static_cast(substeps - i) + ); + } } + scheduleSubstep(*hitbox, i + 1, substeps, dt); } }