From cad1f6a669eb5fe2a2717db2d4fa3f0721bb27d8 Mon Sep 17 00:00:00 2001 From: MihailRis Date: Wed, 29 Nov 2023 13:29:28 +0300 Subject: [PATCH] temporary fixed grass hitbox --- src/definitions.cpp | 2 +- src/frontend/world_render.cpp | 10 +++----- src/voxels/Chunks.cpp | 43 +++++++++++++++++++++++++++-------- 3 files changed, 38 insertions(+), 17 deletions(-) diff --git a/src/definitions.cpp b/src/definitions.cpp index 6378d523..44540b3d 100644 --- a/src/definitions.cpp +++ b/src/definitions.cpp @@ -71,7 +71,7 @@ void setup_definitions(ContentBuilder* builder) { block->lightPassing = true; block->obstacle = false; block->model = BlockModel::xsprite; - block->hitboxScale = 0.5f; + block->hitboxScale = 0.7f; builder->add(block); block = new Block("base:flower", "flower"); diff --git a/src/frontend/world_render.cpp b/src/frontend/world_render.cpp index 42c4a804..94e8bded 100644 --- a/src/frontend/world_render.cpp +++ b/src/frontend/world_render.cpp @@ -168,13 +168,9 @@ void WorldRenderer::draw(const GfxContext& pctx, Camera* camera, bool occlusion) linesShader->use(); linesShader->uniformMatrix("u_projview", camera->getProjView()); lineBatch->lineWidth(2.0f); - if (block->model == BlockModel::block){ - lineBatch->box(pos.x+0.5f, pos.y+0.5f, pos.z+0.5f, - 1.008f,1.008f,1.008f, 0,0,0,0.5f); - } else if (block->model == BlockModel::xsprite){ - lineBatch->box(pos.x+0.5f, pos.y+0.35f, pos.z+0.5f, - 0.805f,0.705f,0.805f, 0,0,0,0.5f); - } + float size = block->hitboxScale; + lineBatch->box(pos.x+0.5f, pos.y+size*0.5f, pos.z+0.5f, + size, size, size, 0, 0, 0, 0.5f); lineBatch->render(); } skybox->unbind(); diff --git a/src/voxels/Chunks.cpp b/src/voxels/Chunks.cpp index c8a7d6d0..1b2ef382 100644 --- a/src/voxels/Chunks.cpp +++ b/src/voxels/Chunks.cpp @@ -200,20 +200,45 @@ voxel* Chunks::rayCast(vec3 start, while (t <= maxDist){ voxel* voxel = get(ix, iy, iz); - if (voxel == nullptr || contentIds->getBlockDef(voxel->id)->selectable){ + const Block* def = nullptr; + if (voxel == nullptr || (def = contentIds->getBlockDef(voxel->id))->selectable){ end.x = px + t * dx; end.y = py + t * dy; end.z = pz + t * dz; + + // TODO: replace this dumb solution with something better + if (def && def->hitboxScale < 1.0f) { + const float sz = def->hitboxScale; + const int subs = 16; + for (int i = 0; i < subs; i++) { + end.x += dx / float(subs); + end.y += dy / float(subs); + end.z += dz / float(subs); + if (end.x - ix >= 0.5f - sz * 0.5f && end.x - ix <= 0.5f + sz * 0.5f && + end.y - iy >= 0.0f && end.y - iy <= sz && + end.z - iz >= 0.5f - sz * 0.5f && end.z - iz <= 0.5f + sz * 0.5f) { + iend.x = ix; + iend.y = iy; + iend.z = iz; - iend.x = ix; - iend.y = iy; - iend.z = iz; + norm.x = norm.y = norm.z = 0.0f; + if (steppedIndex == 0) norm.x = -stepx; + if (steppedIndex == 1) norm.y = -stepy; + if (steppedIndex == 2) norm.z = -stepz; + return voxel; + } + } + } else { + iend.x = ix; + iend.y = iy; + iend.z = iz; - norm.x = norm.y = norm.z = 0.0f; - if (steppedIndex == 0) norm.x = -stepx; - if (steppedIndex == 1) norm.y = -stepy; - if (steppedIndex == 2) norm.z = -stepz; - return voxel; + norm.x = norm.y = norm.z = 0.0f; + if (steppedIndex == 0) norm.x = -stepx; + if (steppedIndex == 1) norm.y = -stepy; + if (steppedIndex == 2) norm.z = -stepz; + return voxel; + } } if (txMax < tyMax) { if (txMax < tzMax) {