From c8e884009bdc7b7ee8025b05cfb12284f340d0af Mon Sep 17 00:00:00 2001 From: MihailRis Date: Mon, 23 Feb 2026 18:44:32 +0300 Subject: [PATCH] fix extended blocks collision detection --- src/physics/Hitbox.hpp | 1 + src/physics/PhysicsSolver.cpp | 45 ++++++++++++++++++++++------------- src/voxels/GlobalChunks.cpp | 2 +- src/voxels/GlobalChunks.hpp | 3 ++- src/voxels/blocks_agent.hpp | 15 ++++++------ 5 files changed, 40 insertions(+), 26 deletions(-) diff --git a/src/physics/Hitbox.hpp b/src/physics/Hitbox.hpp index 2b4a1aad7..a5137818b 100644 --- a/src/physics/Hitbox.hpp +++ b/src/physics/Hitbox.hpp @@ -57,6 +57,7 @@ struct Hitbox { float friction = 1.0f; float verticalDamping = 1.0f; bool grounded = false; + bool collided[3] {}; float gravityScale = 1.0f; bool crouching = false; float stepHeight = 0.5f; diff --git a/src/physics/PhysicsSolver.cpp b/src/physics/PhysicsSolver.cpp index 3df3fa7b8..7eba5088a 100644 --- a/src/physics/PhysicsSolver.cpp +++ b/src/physics/PhysicsSolver.cpp @@ -174,7 +174,8 @@ static bool calc_collision_neg( glm::vec3& pos, glm::vec3& vel, const glm::vec3& half, - float stepHeight + float stepHeight, + bool collided[3] ) { if (vel[nx] >= 0.0f) { return false; @@ -185,7 +186,7 @@ static bool calc_collision_neg( coord[ny] = ((pos+offset)[ny]-half[ny]+E) + iy; for (int iz = 0; iz <= glm::ceil((half[nz]-E)*2); iz++){ coord[nz] = (pos[nz]-half[nz]+E) + iz; - coord[nx] = (pos[nx]-half[nx]-E); + coord[nx] = (pos[nx]-half[nx]-E) - E; auto boxAABB = AABB(pos - half, pos + half); glm::vec3 scale(1.0f); @@ -195,10 +196,11 @@ static bool calc_collision_neg( boxAABB.b.y -= stepHeight; if (const auto aabb = chunks.isObstacleAt(coord.x, coord.y, coord.z, boxAABB)) { - vel[nx] = 0.0f; float newx = std::floor(coord[nx]) + half[nx] + aabb->max()[nx] + E; - if (newx - pos[nx] <= E) { + if (pos[nx] < newx) { + vel[nx] = 0.0f; pos[nx] = newx; + collided[nx] = true; } return true; } @@ -266,7 +268,8 @@ static void calc_collision_pos( glm::vec3& pos, glm::vec3& vel, const glm::vec3& half, - float stepHeight + float stepHeight, + bool collided[3] ) { if (vel[nx] <= 0.0f) { return; @@ -277,7 +280,7 @@ static void calc_collision_pos( coord[ny] = ((pos+offset)[ny]-half[ny]+E) + iy; for (int iz = 0; iz <= glm::ceil((half[nz]-E)*2); iz++) { coord[nz] = (pos[nz]-half[nz]+E) + iz; - coord[nx] = (pos[nx]+half[nx]+E); + coord[nx] = (pos[nx]+half[nx]+E) + E; auto boxAABB = AABB(pos - half, pos + half); glm::vec3 scale(1.0f); @@ -287,10 +290,11 @@ static void calc_collision_pos( boxAABB.b.y -= stepHeight; if (const auto aabb = chunks.isObstacleAt(coord.x, coord.y, coord.z, boxAABB)) { - vel[nx] = 0.0f; float newx = std::floor(coord[nx]) - half[nx] + aabb->min()[nx] - E; - if (newx - pos[nx] >= -E) { + if (pos[nx] > newx) { + vel[nx] = 0.0f; pos[nx] = newx; + collided[nx] = true; } return; } @@ -306,20 +310,29 @@ void PhysicsSolver::calcCollisions( const glm::vec3 half, float stepHeight ) { + hitbox.collided[0] = false; + hitbox.collided[1] = false; + hitbox.collided[2] = false; stepHeight = calc_step_height(chunks, pos, half, stepHeight); - const AABB* aabb; - - calc_collision_neg<0, 1, 2>(chunks, pos, vel, half, stepHeight); - calc_collision_pos<0, 1, 2>(chunks, pos, vel, half, stepHeight); + auto prevPos = pos; - calc_collision_neg<2, 1, 0>(chunks, pos, vel, half, stepHeight); - calc_collision_pos<2, 1, 0>(chunks, pos, vel, half, stepHeight); + calc_collision_neg<0, 1, 2>(chunks, pos, vel, half, stepHeight, hitbox.collided); + calc_collision_pos<0, 1, 2>(chunks, pos, vel, half, stepHeight, hitbox.collided); + + calc_collision_neg<2, 1, 0>(chunks, pos, vel, half, stepHeight, hitbox.collided); + calc_collision_pos<2, 1, 0>(chunks, pos, vel, half, stepHeight, hitbox.collided); if (calc_collision_neg_y(chunks, solidHitboxes, pos, vel, half, hitbox.groundVelocity)) { hitbox.grounded = true; } + for (int axis = 0; axis < 3; axis++) { + if (hitbox.collided[axis]) { + pos[axis] = prevPos[axis]; + } + } + if (stepHeight > 0.0 && vel.y <= 0.0f){ AABB boxAABB = AABB(-half, +half); boxAABB.scale(glm::vec3(1.0f - E * 2, 1.0f, 1.0f - E * 2)); @@ -330,7 +343,7 @@ void PhysicsSolver::calcCollisions( for (int iz = 0; iz <= glm::ceil((half.z-E)*2); iz++) { float z = (pos.z-half.z) + iz; float y = (pos.y-half.y+E); - if ((aabb = chunks.isObstacleAt(x,y,z, boxAABB))){ + if (auto aabb = chunks.isObstacleAt(x,y,z, boxAABB)){ vel.y = 0.0f; float newy = std::floor(y) + aabb->max().y + half.y; if (std::abs(newy-pos.y) <= stepHeight) { @@ -351,7 +364,7 @@ void PhysicsSolver::calcCollisions( for (int iz = 0; iz <= glm::ceil((half.z-E)*2); iz++) { float z = (pos.z-half.z+E) + iz; float y = (pos.y+half.y+E); - if ((aabb = chunks.isObstacleAt(x,y,z, boxAABB))){ + if (auto aabb = chunks.isObstacleAt(x,y,z, boxAABB)){ vel.y = 0.0f; float newy = std::floor(y) - half.y + aabb->min().y - E; if (std::abs(newy-pos.y) <= 0.0f) { diff --git a/src/voxels/GlobalChunks.cpp b/src/voxels/GlobalChunks.cpp index 11e9d5b5e..d24d8b26d 100644 --- a/src/voxels/GlobalChunks.cpp +++ b/src/voxels/GlobalChunks.cpp @@ -214,6 +214,6 @@ void GlobalChunks::putChunk(std::shared_ptr chunk) { chunksMap[keyfrom(chunk->x, chunk->z)] = std::move(chunk); } -const AABB* GlobalChunks::isObstacleAt(float x, float y, float z, const AABB& aabb) const { +std::optional GlobalChunks::isObstacleAt(float x, float y, float z, const AABB& aabb) const { return blocks_agent::is_obstacle_at(*this, x, y, z, aabb); } diff --git a/src/voxels/GlobalChunks.hpp b/src/voxels/GlobalChunks.hpp index 83b6c17bc..de62cbc58 100644 --- a/src/voxels/GlobalChunks.hpp +++ b/src/voxels/GlobalChunks.hpp @@ -1,6 +1,7 @@ #pragma once #include +#include #include #define GLM_ENABLE_EXPERIMENTAL @@ -57,7 +58,7 @@ public: void putChunk(std::shared_ptr chunk); - const AABB* isObstacleAt(float x, float y, float z, const AABB& aabb) const; + std::optional isObstacleAt(float x, float y, float z, const AABB& aabb) const; inline Chunk* getChunk(int cx, int cz) const { const auto& found = chunksMap.find(keyfrom(cx, cz)); diff --git a/src/voxels/blocks_agent.hpp b/src/voxels/blocks_agent.hpp index d1af7cc23..11470fe44 100644 --- a/src/voxels/blocks_agent.hpp +++ b/src/voxels/blocks_agent.hpp @@ -460,22 +460,21 @@ void get_voxels(const Chunks& chunks, VoxelsVolume* volume, bool backlight=false void get_voxels(const GlobalChunks& chunks, VoxelsVolume* volume, bool backlight=false); template -inline const AABB* is_obstacle_at(const Storage& chunks, float x, float y, float z, const AABB& aabb) { +inline std::optional is_obstacle_at(const Storage& chunks, float x, float y, float z, const AABB& aabb) { int ix = std::floor(x); int iy = std::floor(y); int iz = std::floor(z); voxel* v = get(chunks, ix, iy, iz); if (v == nullptr) { if (iy >= CHUNK_H) { - return nullptr; + return std::nullopt; } else { - static const AABB empty; - return ∅ + return AABB(); } } const auto& def = chunks.getContentIndices().blocks.require(v->id); if (!def.obstacle) { - return nullptr; + return std::nullopt; } glm::ivec3 offset {}; if (v->state.segment) { @@ -487,14 +486,14 @@ inline const AABB* is_obstacle_at(const Storage& chunks, float x, float y, float for (const auto& hitbox : boxes) { if (hitbox.intersects(aabb - glm::ivec3(ix, iy, iz))) { - return &hitbox; + return hitbox + offset; } } - return nullptr; + return std::nullopt; } template -inline const AABB* is_obstacle_at(const Storage& chunks, float x, float y, float z) { +inline std::optional is_obstacle_at(const Storage& chunks, float x, float y, float z) { return is_obstacle_at(chunks, x, y, z, AABB({x, y, z}, {x + 1, y + 1, z + 1})); }