|
|
|
@ -69,23 +69,12 @@ void draw_line(sf::RenderWindow &window, sf::Vector2f start, sf::Vector2f end) { |
|
|
|
|
window.draw(line, 2, sf::Lines); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
void ray_casting(sf::RenderWindow &window, Matrix& map) { |
|
|
|
|
float start_angle = player_angle - HALF_FOV; |
|
|
|
|
|
|
|
|
|
for(int ray = 0; ray < CASTED_RAYS; ray++, start_angle += STEP_ANGLE) |
|
|
|
|
{ |
|
|
|
|
for(int depth = 1; depth < MAX_DEPTH; depth++) { |
|
|
|
|
float target_x = player_x - std::sin(start_angle) * depth; |
|
|
|
|
float target_y = player_y + std::cos(start_angle) * depth; |
|
|
|
|
|
|
|
|
|
int col = int(target_x / TILE_SIZE); |
|
|
|
|
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}); |
|
|
|
|
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); |
|
|
|
@ -100,6 +89,23 @@ void ray_casting(sf::RenderWindow &window, Matrix& map) { |
|
|
|
|
{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; |
|
|
|
|
|
|
|
|
|
for(int ray = 0; ray < CASTED_RAYS; ray++, start_angle += STEP_ANGLE) |
|
|
|
|
{ |
|
|
|
|
for(int depth = 1; depth < MAX_DEPTH; depth++) { |
|
|
|
|
float target_x = player_x - std::sin(start_angle) * depth; |
|
|
|
|
float target_y = player_y + std::cos(start_angle) * depth; |
|
|
|
|
|
|
|
|
|
int col = int(target_x / TILE_SIZE); |
|
|
|
|
int row = int(target_y / TILE_SIZE); |
|
|
|
|
|
|
|
|
|
if(map[row][col] == 1) { |
|
|
|
|
draw_map_rays(window, col, row, {target_x, target_y}); |
|
|
|
|
draw_3d_view(window, depth, start_angle, ray); |
|
|
|
|
break; |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|