From 43970df494fc0e05571119c38e908c6f0697fc3c Mon Sep 17 00:00:00 2001 From: Teo-CD Date: Sun, 28 Jan 2024 21:58:32 +0000 Subject: [PATCH] World: Move the scale factor to a constant Clear up the computation and split the result from the ray cast to the scale factor. Update the castRay explanations to add the issue of unmatched axes. --- World.cpp | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/World.cpp b/World.cpp index d359063..6bc5e35 100644 --- a/World.cpp +++ b/World.cpp @@ -107,6 +107,9 @@ float World::castRay(float originX, float originY, float orientation) const * the grid for the other one * - Depending on the orientation, signs must be taken into account * to work 360° + * - Those formulas consider regular axes (x→,y↑), however the world is + * built around left-handed axes (x→,y↓), so the rendered world is + * mirrored. This also explains some weird signs for rotations. */ /* Offsets to get back on the grid from the ray's origin. */ float hOffsetX; @@ -148,7 +151,7 @@ float World::castRay(float originX, float originY, float orientation) const if (orientation < 180) { vOffsetX = ceilf(originX); vOffsetY = ceilf(originX) - originX; - vDir = +1; + vDir = 1; vRound = 0; } else { vOffsetX = floorf(originX); @@ -239,6 +242,8 @@ void World::render(sf::RenderWindow& window) const window.draw(ground); window.draw(ceiling); + const float worldToCamera = (player.focalLength*2)/player.sensorSize; + /* * Throw rays and draw walls over the ceiling and ground. * Only throws in the plane, which doesn't work for levels/3D. @@ -252,7 +257,7 @@ void World::render(sf::RenderWindow& window) const } else if (rayAngle > 360) { rayAngle -= 360; } - float obstacleScale = player.focalLength*2/(castRay(player.x, player.y, rayAngle)*player.sensorSize); + float obstacleScale = worldToCamera / castRay(player.x, player.y, rayAngle); /* 2 Is wall height in meters. */ fillColumn(window, i, obstacleScale); }