Skip to content

Commit

Permalink
render the visibility cone
Browse files Browse the repository at this point in the history
  • Loading branch information
ssloy committed Feb 10, 2019
1 parent 9475f8d commit e7a8b51
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 7 deletions.
Binary file added doc/005.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
19 changes: 12 additions & 7 deletions tinyraycaster.cpp
Expand Up @@ -68,6 +68,7 @@ int main() {
float player_x = 3.456; // player x position float player_x = 3.456; // player x position
float player_y = 2.345; // player y position float player_y = 2.345; // player y position
float player_a = 1.523; // player view direction float player_a = 1.523; // player view direction
const float fov = M_PI/3.; // field of view


for (size_t j = 0; j<win_h; j++) { // fill the screen with color gradients for (size_t j = 0; j<win_h; j++) { // fill the screen with color gradients
for (size_t i = 0; i<win_w; i++) { for (size_t i = 0; i<win_w; i++) {
Expand All @@ -92,14 +93,18 @@ int main() {
// draw the player on the map // draw the player on the map
draw_rectangle(framebuffer, win_w, win_h, player_x*rect_w, player_y*rect_h, 5, 5, pack_color(255, 255, 255)); draw_rectangle(framebuffer, win_w, win_h, player_x*rect_w, player_y*rect_h, 5, 5, pack_color(255, 255, 255));


for (float t=0; t<20; t+=.05) { for (size_t i=0; i<win_w; i++) { // draw the visibility cone
float cx = player_x + t*cos(player_a); float angle = player_a-fov/2 + fov*i/float(win_w);
float cy = player_y + t*sin(player_a);
if (map[int(cx)+int(cy)*map_w]!=' ') break;


size_t pix_x = cx*rect_w; for (float t=0; t<20; t+=.05) {
size_t pix_y = cy*rect_h; float cx = player_x + t*cos(angle);
framebuffer[pix_x + pix_y*win_w] = pack_color(255, 255, 255); float cy = player_y + t*sin(angle);
if (map[int(cx)+int(cy)*map_w]!=' ') break;

size_t pix_x = cx*rect_w;
size_t pix_y = cy*rect_h;
framebuffer[pix_x + pix_y*win_w] = pack_color(255, 255, 255);
}
} }


drop_ppm_image("./out.ppm", framebuffer, win_w, win_h); drop_ppm_image("./out.ppm", framebuffer, win_w, win_h);
Expand Down

0 comments on commit e7a8b51

Please sign in to comment.