Raycaster mostly cleaned up. Only thing I can think of is bringing in CameraLOL as the coordinates directly.

master
Zed A. Shaw 4 weeks ago
parent 2de4b43914
commit 9c37960283
  1. 24
      raycaster.cpp
  2. 2
      raycaster.hpp

@ -47,6 +47,8 @@ Raycaster::Raycaster(TexturePack &textures, int width, int height) :
} }
void Raycaster::set_position(int x, int y) { void Raycaster::set_position(int x, int y) {
$screen_pos_x = x;
$screen_pos_y = y;
$view_sprite.setPosition({(float)x, (float)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 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); 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); 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)); 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_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; int tex_render_width = tex_x_end - tex_x;
// avoid drawing sprites that are not visible (width < 0)
if(tex_render_width <= 0) continue; if(tex_render_width <= 0) continue;
float x = float(draw_start_x + RAY_VIEW_X); float x = float(draw_start_x + $screen_pos_x);
float y = float(draw_start_y + RAY_VIEW_Y); float y = float(draw_start_y + $screen_pos_y);
float sprite_w = float(sprite_width) / float(texture_width); float sprite_w = float(sprite_width) / float(texture_width);
float sprite_h = float(sprite_height) / float(texture_height); 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}); $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); float level = sqrt(rec.first);
$brightness.setUniform("darkness", level); $brightness.setUniform("darkness", level);
target.draw(*sf_sprite, &$brightness); target.draw(*sf_sprite, &$brightness);
@ -158,10 +165,6 @@ void Raycaster::cast_rays() {
int map_x = int($pos_x); int map_x = int($pos_x);
int map_y = int($pos_y); 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 // 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_x = std::abs(1.0 / ray_dir_x);
double delta_dist_y = std::abs(1.0 / ray_dir_y); double delta_dist_y = std::abs(1.0 / ray_dir_y);
@ -171,7 +174,10 @@ void Raycaster::cast_rays() {
int hit = 0; int hit = 0;
int side = 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) { if(ray_dir_x < 0) {
step_x = -1; step_x = -1;
side_dist_x = ($pos_x - map_x) * delta_dist_x; side_dist_x = ($pos_x - map_x) * delta_dist_x;
@ -226,7 +232,7 @@ void Raycaster::cast_rays() {
} else { } else {
wall_x = $pos_x + perp_wall_dist * ray_dir_x; 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 // x coorindate on the texture
int tex_x = int(wall_x * double(texture_width)); int tex_x = int(wall_x * double(texture_width));

@ -32,6 +32,8 @@ struct Raycaster {
int $width; int $width;
int $height; int $height;
int $screen_pos_x = RAY_VIEW_X;
int $screen_pos_y = RAY_VIEW_Y;
GameLevel $level; GameLevel $level;
Matrix $map; Matrix $map;
std::unordered_map<DinkyECS::Entity, SpriteTexture> $sprites; std::unordered_map<DinkyECS::Entity, SpriteTexture> $sprites;

Loading…
Cancel
Save