From bf77723f70a7fc92da473c60a7e64110973092f7 Mon Sep 17 00:00:00 2001 From: "Zed A. Shaw" Date: Mon, 6 Jan 2025 10:59:55 -0500 Subject: [PATCH] Fixed the map view on the left so it shows the correct visible squares. --- raycaster.cpp | 42 ++++++++++++++++++++++++------------------ 1 file changed, 24 insertions(+), 18 deletions(-) diff --git a/raycaster.cpp b/raycaster.cpp index 594b29f..5891cc3 100644 --- a/raycaster.cpp +++ b/raycaster.cpp @@ -69,6 +69,28 @@ void draw_line(sf::RenderWindow &window, sf::Vector2f start, sf::Vector2f end) { window.draw(line, 2, sf::Lines); } +void draw_map_rays(sf::RenderWindow &window, int col, int row, sf::Vector2f target) { + draw_map_rect(window, col, row, 100); + draw_line(window, {player_x, player_y}, target); +} + +void draw_3d_view(sf::RenderWindow &window, int depth, float start_angle, int ray) { + uint8_t color = 255 / (1 + depth * depth * 0.0001); + + float fixed_depth = depth * std::cos(player_angle - start_angle); + + float wall_height = 21000 / fixed_depth; + + if(wall_height > SCREEN_HEIGHT){ + wall_height = SCREEN_HEIGHT; + } + + draw_rect(window, + {SCREEN_HEIGHT + ray * SCALE, (SCREEN_HEIGHT / 2) - wall_height / 2}, + {SCALE, wall_height}, + color); +} + void ray_casting(sf::RenderWindow &window, Matrix& map) { float start_angle = player_angle - HALF_FOV; @@ -82,24 +104,8 @@ void ray_casting(sf::RenderWindow &window, Matrix& map) { int row = int(target_y / TILE_SIZE); if(map[row][col] == 1) { - draw_map_rect(window, col & TILE_SIZE, row * TILE_SIZE, 100); - - draw_line(window, {player_x, player_y}, {target_x, target_y}); - - uint8_t color = 255 / (1 + depth * depth * 0.0001); - - float fixed_depth = depth * std::cos(player_angle - start_angle); - - float wall_height = 21000 / fixed_depth; - - if(wall_height > SCREEN_HEIGHT){ - wall_height = SCREEN_HEIGHT; - } - - draw_rect(window, - {SCREEN_HEIGHT + ray * SCALE, (SCREEN_HEIGHT / 2) - wall_height / 2}, - {SCALE, wall_height}, - color); + draw_map_rays(window, col, row, {target_x, target_y}); + draw_3d_view(window, depth, start_angle, ray); break; } }