This commit is contained in:
MihailRis 2026-07-16 21:55:35 +03:00
parent ad6b72f87c
commit 00d6e28208

View file

@ -239,9 +239,8 @@ static inline voxel* raycast_blocks(
int iy = std::floor(py); int iy = std::floor(py);
int iz = std::floor(pz); int iz = std::floor(pz);
int stepx = (dx > 0.0f) ? 1 : -1; glm::ivec3 step {
int stepy = (dy > 0.0f) ? 1 : -1; (dx > 0.0f) ? 1 : -1, (dy > 0.0f) ? 1 : -1, (dz > 0.0f) ? 1 : -1};
int stepz = (dz > 0.0f) ? 1 : -1;
constexpr float infinity = std::numeric_limits<float>::infinity(); constexpr float infinity = std::numeric_limits<float>::infinity();
constexpr float epsilon = 1e-6f; // 0.000001 constexpr float epsilon = 1e-6f; // 0.000001
@ -249,9 +248,9 @@ static inline voxel* raycast_blocks(
float tyDelta = (std::fabs(dy) < epsilon) ? infinity : std::fabs(1.0f / dy); float tyDelta = (std::fabs(dy) < epsilon) ? infinity : std::fabs(1.0f / dy);
float tzDelta = (std::fabs(dz) < epsilon) ? infinity : std::fabs(1.0f / dz); float tzDelta = (std::fabs(dz) < epsilon) ? infinity : std::fabs(1.0f / dz);
float xdist = (stepx > 0) ? (ix + 1 - px) : (px - ix); float xdist = (step.x > 0) ? (ix + 1 - px) : (px - ix);
float ydist = (stepy > 0) ? (iy + 1 - py) : (py - iy); float ydist = (step.y > 0) ? (iy + 1 - py) : (py - iy);
float zdist = (stepz > 0) ? (iz + 1 - pz) : (pz - iz); float zdist = (step.z > 0) ? (iz + 1 - pz) : (pz - iz);
float txMax = (txDelta < infinity) ? txDelta * xdist : infinity; float txMax = (txDelta < infinity) ? txDelta * xdist : infinity;
float tyMax = (tyDelta < infinity) ? tyDelta * ydist : infinity; float tyMax = (tyDelta < infinity) ? tyDelta * ydist : infinity;
@ -272,12 +271,8 @@ static inline voxel* raycast_blocks(
&& (def.selectable || includeNonSelectable) && (def.selectable || includeNonSelectable)
&& (filter == nullptr || filter->empty() || && (filter == nullptr || filter->empty() ||
filter->find(def.rt.id) == filter->end())) { filter->find(def.rt.id) == filter->end())) {
end.x = px + t * dx; end = start + t * dir;
end.y = py + t * dy; iend = {ix, iy, iz};
end.z = pz + t * dz;
iend.x = ix;
iend.y = iy;
iend.z = iz;
if (!def.rt.solid) { if (!def.rt.solid) {
const std::vector<AABB>& hitboxes = const std::vector<AABB>& hitboxes =
@ -312,51 +307,41 @@ static inline voxel* raycast_blocks(
if (hit) return voxel; if (hit) return voxel;
} else { } else {
iend.x = ix; iend = {ix, iy, iz};
iend.y = iy; norm = {0, 0, 0};
iend.z = iz; norm[steppedIndex] = -step[steppedIndex];
norm.x = norm.y = norm.z = 0;
if (steppedIndex == 0) norm.x = -stepx;
if (steppedIndex == 1) norm.y = -stepy;
if (steppedIndex == 2) norm.z = -stepz;
return voxel; return voxel;
} }
} }
if (txMax < tyMax) { if (txMax < tyMax) {
if (txMax < tzMax) { if (txMax < tzMax) {
ix += stepx; ix += step.x;
t = txMax; t = txMax;
txMax += txDelta; txMax += txDelta;
steppedIndex = 0; steppedIndex = 0;
} else { } else {
iz += stepz; iz += step.z;
t = tzMax; t = tzMax;
tzMax += tzDelta; tzMax += tzDelta;
steppedIndex = 2; steppedIndex = 2;
} }
} else { } else {
if (tyMax < tzMax) { if (tyMax < tzMax) {
iy += stepy; iy += step.y;
t = tyMax; t = tyMax;
tyMax += tyDelta; tyMax += tyDelta;
steppedIndex = 1; steppedIndex = 1;
} else { } else {
iz += stepz; iz += step.z;
t = tzMax; t = tzMax;
tzMax += tzDelta; tzMax += tzDelta;
steppedIndex = 2; steppedIndex = 2;
} }
} }
} }
iend.x = ix; iend = {ix, iy, iz};
iend.y = iy; end = start + t * dir;
iend.z = iz; norm = {0, 0, 0};
end.x = px + t * dx;
end.y = py + t * dy;
end.z = pz + t * dz;
norm.x = norm.y = norm.z = 0;
return nullptr; return nullptr;
} }