From 9c379602836d6cc4ad276ff90831668f6339ca89 Mon Sep 17 00:00:00 2001 From: "Zed A. Shaw" Date: Mon, 10 Feb 2025 12:05:07 -0500 Subject: [PATCH] Raycaster mostly cleaned up. Only thing I can think of is bringing in CameraLOL as the coordinates directly. --- raycaster.cpp | 24 +++++++++++++++--------- raycaster.hpp | 2 ++ 2 files changed, 17 insertions(+), 9 deletions(-) diff --git a/raycaster.cpp b/raycaster.cpp index 14a6571..7fa2918 100644 --- a/raycaster.cpp +++ b/raycaster.cpp @@ -47,6 +47,8 @@ Raycaster::Raycaster(TexturePack &textures, int width, int height) : } void Raycaster::set_position(int x, int y) { + $screen_pos_x = x; + $screen_pos_y = y; $view_sprite.setPosition({(float)x, (float)y}); } @@ -81,8 +83,8 @@ void Raycaster::sprite_casting(sf::RenderTarget &target) { double inv_det = 1.0 / ($plane_x * $dir_y - $dir_x * $plane_y); // required for correct matrix multiplication double transform_x = inv_det * ($dir_y * sprite_x - $dir_x * sprite_y); - //this is actually the depth inside the screen, that what Z is in 3D, the distance of sprite to player, matching sqrt(spriteDistance[i]) + //this is actually the depth inside the screen, that what Z is in 3D, the distance of sprite to player, matching sqrt(spriteDistance[i]) double transform_y = inv_det * (-$plane_y * sprite_x + $plane_x * sprite_y); int sprite_screen_x = int(($width / 2) * (1 + transform_x / transform_y)); @@ -119,10 +121,12 @@ void Raycaster::sprite_casting(sf::RenderTarget &target) { int tex_x = int(texture_width * (draw_start_x - (-sprite_width / 2 + sprite_screen_x)) * texture_width / sprite_width) / texture_width; int tex_render_width = tex_x_end - tex_x; + + // avoid drawing sprites that are not visible (width < 0) if(tex_render_width <= 0) continue; - float x = float(draw_start_x + RAY_VIEW_X); - float y = float(draw_start_y + RAY_VIEW_Y); + float x = float(draw_start_x + $screen_pos_x); + float y = float(draw_start_y + $screen_pos_y); float sprite_w = float(sprite_width) / float(texture_width); float sprite_h = float(sprite_height) / float(texture_height); @@ -135,6 +139,9 @@ void Raycaster::sprite_casting(sf::RenderTarget &target) { $brightness.setUniform("offsetFactor", sf::Glsl::Vec2{0.0f, 0.0f}); + // the SpatialMap.distance_sorted only calculates the + // (x1-x2)^2 + (y1-y2)^2 portion of distance, so to get + // the actual distance we need to sqrt that. float level = sqrt(rec.first); $brightness.setUniform("darkness", level); target.draw(*sf_sprite, &$brightness); @@ -158,10 +165,6 @@ void Raycaster::cast_rays() { int map_x = int($pos_x); int map_y = int($pos_y); - // length of ray from current pos to next x or y-side - double side_dist_x; - double side_dist_y; - // length of ray from one x or y-side to next x or y-side double delta_dist_x = std::abs(1.0 / ray_dir_x); double delta_dist_y = std::abs(1.0 / ray_dir_y); @@ -171,7 +174,10 @@ void Raycaster::cast_rays() { int hit = 0; int side = 0; - // calculate step and initial sideDist + // length of ray from current pos to next x or y-side + double side_dist_x; + double side_dist_y; + if(ray_dir_x < 0) { step_x = -1; side_dist_x = ($pos_x - map_x) * delta_dist_x; @@ -226,7 +232,7 @@ void Raycaster::cast_rays() { } else { wall_x = $pos_x + perp_wall_dist * ray_dir_x; } - wall_x -= floor((wall_x)); + wall_x -= floor(wall_x); // x coorindate on the texture int tex_x = int(wall_x * double(texture_width)); diff --git a/raycaster.hpp b/raycaster.hpp index 2c67dd9..50626b5 100644 --- a/raycaster.hpp +++ b/raycaster.hpp @@ -32,6 +32,8 @@ struct Raycaster { int $width; int $height; + int $screen_pos_x = RAY_VIEW_X; + int $screen_pos_y = RAY_VIEW_Y; GameLevel $level; Matrix $map; std::unordered_map $sprites;