diff --git a/World.cpp b/World.cpp index 8f37c24..d23aa1f 100644 --- a/World.cpp +++ b/World.cpp @@ -99,7 +99,7 @@ std::ostream& operator<<(std::ostream& ostream, World const& world) return(ostream); } -float World::castRay(float originX, float originY, float orientation) const +RaycastResult World::castRay(float originX, float originY, float orientation) const { /* * Reference used for ray intersection computations : @@ -214,7 +214,21 @@ float World::castRay(float originX, float originY, float orientation) const float vDist = sqrtf((originX - vCheckX)*(originX - vCheckX) + (originY - vCheckY)*(originY - vCheckY)); - return hDist > vDist ? vDist : hDist; + + RaycastResult result{}; + if (hDist > vDist) { + result.distance = vDist; + result.hitX = vCheckX; + result.hitY = vCheckY; + } else { + result.distance = hDist; + result.hitX = hCheckX; + result.hitY = hCheckY; + } + result.hitBlock = getBlock(floorf(result.hitX) + vRound, + floorf(result.hitY)); + + return result; } void World::fillColumn(sf::RenderWindow& window, unsigned int column, @@ -268,7 +282,7 @@ void World::render(sf::RenderWindow& window) rayAngle -= 360; } float obstacleScale = worldToCamera / ( - castRay(player.x, player.y, rayAngle) * + castRay(player.x, player.y, rayAngle).distance * cosf(deltaAngle*deg_to_rad) ); /* 2 Is wall height in meters. */ diff --git a/World.h b/World.h index 7b24d70..5e3a7e2 100644 --- a/World.h +++ b/World.h @@ -9,6 +9,7 @@ #include #include #include +#include #include "Color.h" #include "Player.h" @@ -20,6 +21,13 @@ enum class BlockType { WINDOW, }; +struct RaycastResult { + float distance; + float hitX; + float hitY; + BlockType hitBlock; +}; + class World { public: Player player; @@ -61,9 +69,9 @@ private: * @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 Distance of the hit to the origin. + * @return Result struct containing information on the hit. */ - float castRay(float originX, float originY, float orientation) const; + RaycastResult castRay(float originX, float originY, float orientation) const; };