Fix #160 with suggestions from @A-lex-Ra

This commit is contained in:
InfiniteCoder
2024-02-24 17:09:11 +03:00
parent 7ee0c672f0
commit 3edf065550
7 changed files with 63 additions and 82 deletions
+15 -41
View File
@@ -54,27 +54,19 @@ const AABB* Chunks::isObstacleAt(float x, float y, float z){
int iy = floor(y);
int iz = floor(z);
voxel* v = get(ix, iy, iz);
if (v == nullptr)
return &contentIds->getBlockDef(0)->hitbox;
if (v == nullptr) {
static const AABB empty;
return ∅
}
const Block* def = contentIds->getBlockDef(v->id);
if (def->obstacle) {
const AABB& hitbox = def->rotatable
? def->rt.hitboxes[v->rotation()]
: def->hitbox;
if (def->rt.solid) {
return &hitbox;
} else if (def->hitboxExplicit) {
const auto& boxes = def->rotatable
? def->rt.hitboxes[v->rotation()]
: def->hitboxes;
for (const auto& hitbox : boxes) {
if (hitbox.contains({x - ix, y - iy, z - iz}))
return &hitbox;
} else {
const auto& boxes = def->rotatable
? def->rt.modelBoxes[v->rotation()]
: def->modelBoxes;
for (const auto& hitbox : boxes) {
if (hitbox.contains({x - ix, y - iy, z - iz}))
return &hitbox;
}
}
}
}
return nullptr;
}
@@ -247,18 +239,9 @@ voxel* Chunks::rayCast(glm::vec3 start,
iend.z = iz;
if (!def->rt.solid) {
std::vector<AABB> hitboxes;
if (def->hitboxExplicit) {
hitboxes = {
def->rotatable
? def->rt.hitboxes[voxel->rotation()]
: def->hitbox
};
} else {
hitboxes = def->rotatable
? def->rt.modelBoxes[voxel->rotation()]
: def->modelBoxes;
}
const std::vector<AABB>& hitboxes = def->rotatable
? def->rt.hitboxes[voxel->rotation()]
: def->hitboxes;
scalar_t distance = maxDist;
Ray ray(start, dir);
@@ -365,18 +348,9 @@ glm::vec3 Chunks::rayCastToObstacle(glm::vec3 start, glm::vec3 dir, float maxDis
const Block* def = contentIds->getBlockDef(voxel->id);
if (def->obstacle) {
if (!def->rt.solid) {
std::vector<AABB> hitboxes;
if (def->hitboxExplicit) {
hitboxes = {
def->rotatable
? def->rt.hitboxes[voxel->rotation()]
: def->hitbox
};
} else {
hitboxes = def->rotatable
? def->rt.modelBoxes[voxel->rotation()]
: def->modelBoxes;
}
const std::vector<AABB>& hitboxes = def->rotatable
? def->rt.hitboxes[voxel->rotation()]
: def->modelBoxes;
scalar_t distance;
glm::ivec3 norm;