From afbc4e39cb019264accb4507af6831a30be556d4 Mon Sep 17 00:00:00 2001 From: Sergwest Date: Tue, 19 Mar 2024 20:32:58 +0300 Subject: [PATCH] allows to put non-square blocks next to yourself --- src/logic/PlayerController.cpp | 2 +- src/physics/PhysicsSolver.cpp | 20 ++++++++++++++++++++ src/physics/PhysicsSolver.h | 2 ++ 3 files changed, 23 insertions(+), 1 deletion(-) diff --git a/src/logic/PlayerController.cpp b/src/logic/PlayerController.cpp index 99e76f19..02bde1ea 100644 --- a/src/logic/PlayerController.cpp +++ b/src/logic/PlayerController.cpp @@ -415,7 +415,7 @@ void PlayerController::updateInteraction(){ vox = chunks->get(x, y, z); blockid_t chosenBlock = def->rt.id; if (vox && (target = indices->getBlockDef(vox->id))->replaceable) { - if (!level->physics->isBlockInside(x,y,z, player->hitbox.get()) + if (!level->physics->isBlockInside(x,y,z,def,states, player->hitbox.get()) || !def->obstacle){ if (def->grounded && !chunks->isSolidBlock(x, y-1, z)) { chosenBlock = 0; diff --git a/src/physics/PhysicsSolver.cpp b/src/physics/PhysicsSolver.cpp index 4f63fd8c..322f3146 100644 --- a/src/physics/PhysicsSolver.cpp +++ b/src/physics/PhysicsSolver.cpp @@ -4,6 +4,7 @@ #include "../maths/aabb.h" #include "../voxels/Block.h" #include "../voxels/Chunks.h" +#include "../voxels/voxel.h" const double E = 0.03; const double MAX_FIX = 0.1; @@ -218,3 +219,22 @@ bool PhysicsSolver::isBlockInside(int x, int y, int z, Hitbox* hitbox) { y >= floor(pos.y-half.y) && y <= floor(pos.y+half.y); } +bool PhysicsSolver::isBlockInside(int x, int y, int z, Block* def, blockstate_t states, Hitbox* hitbox) { + vec3& pos = hitbox->position; + vec3& half = hitbox->halfsize; + voxel v; + v.states = states; + const auto& boxes = def->rotatable + ? def->rt.hitboxes[v.rotation()] + : def->hitboxes; + for (const auto& block_hitbox : boxes) { + vec3 min = block_hitbox.min(); + vec3 max = block_hitbox.max(); + // 0.00001 - inaccuracy + if (min.x < pos.x+half.x-x-0.00001 && max.x > pos.x-half.x-x+0.00001 && + min.z < pos.z+half.z-z-0.00001 && max.z > pos.z-half.z-z+0.00001 && + min.y < pos.y+half.y-y-0.00001 && max.y > pos.y-half.y-y+0.00001) + return true; + } + return false; +} diff --git a/src/physics/PhysicsSolver.h b/src/physics/PhysicsSolver.h index 08bccfca..4bc748e3 100644 --- a/src/physics/PhysicsSolver.h +++ b/src/physics/PhysicsSolver.h @@ -3,6 +3,7 @@ #include #include "../typedefs.h" +#include "../voxels/Block.h" class Hitbox; class Chunks; @@ -26,6 +27,7 @@ public: const glm::vec3 half, float stepHeight); bool isBlockInside(int x, int y, int z, Hitbox* hitbox); + bool isBlockInside(int x, int y, int z, Block* def, blockstate_t states, Hitbox* hitbox); }; #endif /* PHYSICS_PHYSICSSOLVER_H_ */