From 81aaafd26d516c723a3b58459cab42fb87f8d730 Mon Sep 17 00:00:00 2001 From: trotFunky Date: Thu, 25 Jan 2024 22:31:39 +0000 Subject: [PATCH] World: make CastRay generic Previously CastRay did the scale computation and used the player view statistics. This makes it quite specialized, and the rest of the computations are made outside. Move the computations in the render function and make castRay generic. --- World.cpp | 7 +++---- World.h | 4 ++-- 2 files changed, 5 insertions(+), 6 deletions(-) diff --git a/World.cpp b/World.cpp index 40120ed..1ca062c 100644 --- a/World.cpp +++ b/World.cpp @@ -200,10 +200,8 @@ float World::castRay(float originX, float originY, float orientation) const (originY - hCheckY)*(originY - hCheckY)); float vDist = sqrtf((originX - vCheckX)*(originX - vCheckX) + (originY - vCheckY)*(originY - vCheckY)); - float finalDist = hDist > vDist ? vDist : hDist; - /* 2 Is wall height in meters. */ - return player.focalLength*2/finalDist; + return hDist > vDist ? vDist : hDist; } void World::fillColumn(sf::RenderWindow& window, unsigned int column, @@ -250,7 +248,8 @@ void World::render(sf::RenderWindow& window) const } else if (rayAngle > 360) { rayAngle -= 360; } - float obstacleScale = castRay(player.x, player.y, rayAngle)/player.sensorSize; + float obstacleScale = player.focalLength*2/(castRay(player.x, player.y, rayAngle)*player.sensorSize); + /* 2 Is wall height in meters. */ fillColumn(window, i, obstacleScale); } } diff --git a/World.h b/World.h index 7f716e3..6ecf4ab 100644 --- a/World.h +++ b/World.h @@ -54,11 +54,11 @@ private: void fillColumn(sf::RenderWindow&, unsigned int column, float scale, sf::Color wallColor = sf::Color(84,56,34)) const; /** - * Cast a ray from a given position and return the on-screen scale. + * Cast a ray from a given position and its distance to the origin. * @param originX Ray X origin, strictly positive * @param originY Ray Y origin, strictly positive * @param orientation Angle to cast to, in degrees between 0 and 360 - * @return Scale on the screen of the hit wall. + * @return Distance of the hit to the origin. */ float castRay(float originX, float originY, float orientation) const; };