fix extended blocks collision detection

This commit is contained in:
MihailRis 2026-02-23 18:44:32 +03:00
parent 0a34f9b767
commit c8e884009b
5 changed files with 40 additions and 26 deletions

View file

@ -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;

View file

@ -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) {

View file

@ -214,6 +214,6 @@ void GlobalChunks::putChunk(std::shared_ptr<Chunk> 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<AABB> GlobalChunks::isObstacleAt(float x, float y, float z, const AABB& aabb) const {
return blocks_agent::is_obstacle_at(*this, x, y, z, aabb);
}

View file

@ -1,6 +1,7 @@
#pragma once
#include <memory>
#include <optional>
#include <unordered_map>
#define GLM_ENABLE_EXPERIMENTAL
@ -57,7 +58,7 @@ public:
void putChunk(std::shared_ptr<Chunk> chunk);
const AABB* isObstacleAt(float x, float y, float z, const AABB& aabb) const;
std::optional<AABB> 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));

View file

@ -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 <class Storage>
inline const AABB* is_obstacle_at(const Storage& chunks, float x, float y, float z, const AABB& aabb) {
inline std::optional<AABB> 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 &empty;
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 <class Storage>
inline const AABB* is_obstacle_at(const Storage& chunks, float x, float y, float z) {
inline std::optional<AABB> 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}));
}