1
0
Fork 0

World: Fix wrong block type being returned in castRay

The GetBlock() call has specific offsets that are angle and axis dependent to get the correct
square on the grid from the coordinates of a hit.
This is something that I forgot when introducing the RaycastResult struct, and just used
the first GetBlock() as a common call, returning wrong types in some cases.

As with the hit coordinates, save the type of block hit and use that depending on
the closest hit later, rather than a common and wrong GetBlock().
This commit is contained in:
trotFunky 2024-02-05 23:04:19 +00:00
parent 7241ed7321
commit f7e1b3eab9

View file

@ -179,10 +179,12 @@ RaycastResult World::castRay(float originX, float originY, float orientation) co
int i = 0; int i = 0;
float hCheckX = originX + hOffsetX; float hCheckX = originX + hOffsetX;
float hCheckY = hOffsetY; float hCheckY = hOffsetY;
BlockType hHitBlock = BlockType::AIR;
/* Bounds + sanity check. */ /* Bounds + sanity check. */
while (hCheckX >= 0 && hCheckX <= static_cast<float>(w) && while (hCheckX >= 0 && hCheckX <= static_cast<float>(w) &&
hCheckY >= 0 && hCheckY <= static_cast<float>(h) && i < h) { hCheckY >= 0 && hCheckY <= static_cast<float>(h) && i < h) {
if (getBlock(floorf(hCheckX), floorf(hCheckY) + hRound) == BlockType::WALL) { hHitBlock = getBlock(floorf(hCheckX), floorf(hCheckY) + hRound);
if (hHitBlock == BlockType::WALL) {
break; break;
} }
@ -194,11 +196,12 @@ RaycastResult World::castRay(float originX, float originY, float orientation) co
i = 0; i = 0;
float vCheckX = vOffsetX; float vCheckX = vOffsetX;
float vCheckY = originY + vOffsetY; float vCheckY = originY + vOffsetY;
BlockType vHitBlock = BlockType::AIR;
/* Bounds + sanity check. */ /* Bounds + sanity check. */
while (vCheckX >= 0 && vCheckX < static_cast<float>(w) && while (vCheckX >= 0 && vCheckX < static_cast<float>(w) &&
vCheckY >= 0 && vCheckY < static_cast<float>(h) && i < w) { vCheckY >= 0 && vCheckY < static_cast<float>(h) && i < w) {
if (getBlock(floorf(vCheckX) + vRound, floorf(vCheckY)) == BlockType::WALL) { vHitBlock = getBlock(floorf(vCheckX) + vRound, floorf(vCheckY));
if (vHitBlock == BlockType::WALL) {
break; break;
} }
@ -222,13 +225,13 @@ RaycastResult World::castRay(float originX, float originY, float orientation) co
result.distance = vDist; result.distance = vDist;
result.hitX = vCheckX; result.hitX = vCheckX;
result.hitY = vCheckY; result.hitY = vCheckY;
result.hitBlock = vHitBlock;
} else { } else {
result.distance = hDist; result.distance = hDist;
result.hitX = hCheckX; result.hitX = hCheckX;
result.hitY = hCheckY; result.hitY = hCheckY;
result.hitBlock = hHitBlock;
} }
result.hitBlock = getBlock(floorf(result.hitX) + vRound,
floorf(result.hitY));
return result; return result;
} }