Permalink
Browse files

render the visibility cone

  • Loading branch information...
ssloy committed Feb 9, 2019
1 parent 9475f8d commit e7a8b510aeefd3eb5fee981f5b3c4334835ce510
Showing with 12 additions and 7 deletions.
  1. BIN doc/005.png
  2. +12 −7 tinyraycaster.cpp
BIN +33.1 KB doc/005.png
Binary file not shown.
@@ -68,6 +68,7 @@ int main() {
float player_x = 3.456; // player x position
float player_y = 2.345; // player y position
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 i = 0; i<win_w; i++) {
@@ -92,14 +93,18 @@ int main() {
// 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));

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

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);
for (float t=0; t<20; t+=.05) {
float cx = player_x + t*cos(angle);
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);

0 comments on commit e7a8b51

Please sign in to comment.