From cf539296a54ab3e2d6d51d5dc31475451c1af825 Mon Sep 17 00:00:00 2001 From: "Zed A. Shaw" Date: Mon, 6 Jan 2025 11:41:44 -0500 Subject: [PATCH] Raycaster is smoother without changing much. Big debate is should left-right and forward-back at the same time cancel out motion or should they be exclusive since you can't do both. --- raycaster.cpp | 63 +++++++++++++++++++++++++++------------------------ 1 file changed, 33 insertions(+), 30 deletions(-) diff --git a/raycaster.cpp b/raycaster.cpp index 5891cc3..aae5759 100644 --- a/raycaster.cpp +++ b/raycaster.cpp @@ -112,47 +112,50 @@ void ray_casting(sf::RenderWindow &window, Matrix& map) { } } +void draw_ceiling_floor(sf::RenderWindow &window) { + draw_rect(window, + {SCREEN_HEIGHT, SCREEN_HEIGHT /2}, + {SCREEN_HEIGHT, SCREEN_HEIGHT}, + 100); + draw_rect(window, + {SCREEN_HEIGHT, (SCREEN_HEIGHT * -1) / 2}, + {SCREEN_HEIGHT, SCREEN_HEIGHT}, + 200); +} + +void draw_everything(sf::RenderWindow &window) { + draw_map(window, MAP); + draw_ceiling_floor(window); + ray_casting(window, MAP); + window.display(); +} + int main() { + using KB = sf::Keyboard; sf::RenderWindow window(sf::VideoMode(SCREEN_WIDTH, SCREEN_HEIGHT), "Raycaster"); window.setVerticalSyncEnabled(true); while(window.isOpen()) { - sf::Event event; + draw_everything(window); - draw_map(window, MAP); - - draw_rect(window, - {480, SCREEN_HEIGHT /2}, - {SCREEN_HEIGHT, SCREEN_HEIGHT}, - 100); - - draw_rect(window, - {480, (SCREEN_HEIGHT * -1) / 2}, - {SCREEN_HEIGHT, SCREEN_HEIGHT}, - 200); - - ray_casting(window, MAP); + if(KB::isKeyPressed(KB::A)) { + player_angle -= 0.1; + } else if(KB::isKeyPressed(KB::D)) { + player_angle += 0.1; + } - window.display(); + if(KB::isKeyPressed(KB::W)) { + player_x += -1 * std::sin(player_angle) * 5; + player_y += std::cos(player_angle) * 5; + } else if(KB::isKeyPressed(KB::S)) { + player_x -= -1 * std::sin(player_angle) * 5; + player_y -= std::cos(player_angle) * 5; + } + sf::Event event; while(window.pollEvent(event)) { - using KB = sf::Keyboard; - if(event.type == sf::Event::Closed) { window.close(); - } else if(event.type == sf::Event::KeyPressed) { - if(KB::isKeyPressed(KB::Left)) { - player_angle -= 0.1; - } else if(KB::isKeyPressed(KB::Right)) { - player_angle += 0.1; - } else if(KB::isKeyPressed(KB::Up)) { - player_x += -1 * std::sin(player_angle) * 5; - player_y += std::cos(player_angle) * 5; - } else if(KB::isKeyPressed(KB::Down)) { - player_x -= -1 * std::sin(player_angle) * 5; - player_y -= std::cos(player_angle) * 5; - - } } } }