Advertisement
Guest User

raycast

a guest
Mar 31st, 2020
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.47 KB | None | 0 0
  1. #include <SFML/Window.hpp>
  2. #include <SFML/Graphics.hpp>
  3. #include <iostream>
  4. #include <cmath>
  5. #include <vector>
  6. #include <utility>
  7.  
  8. #define row std::vector<int>
  9.  
  10. float to_radians(const float degree);
  11. std::pair<uint8_t,uint8_t> ray_direction(const float degree);
  12.  
  13. std::vector<row> level = {{1,1,1,1,1,1,1,1,1,1},
  14.                           {1,1,0,1,0,1,0,0,0,1},
  15.                           {1,0,0,1,0,1,0,0,0,1},
  16.                           {1,0,0,0,0,0,1,0,0,1},
  17.                           {1,1,1,0,0,0,1,0,0,1},
  18.                           {1,1,0,0,0,0,1,0,0,1},
  19.                           {1,1,1,1,1,1,1,1,1,1}};
  20.  
  21. int main(){
  22.     const uint16_t HEIGHT    = 600;
  23.     const uint16_t WIDTH     = 800;
  24.     const float    BLOCKSIZE = 1.0f;
  25.  
  26.     const float FOV   = 60.0f;
  27.  
  28.     float ANGLE = 0.0f;
  29.  
  30.     float px = 5.5f;
  31.     float py = 4.5f;
  32.  
  33.     sf::RenderWindow window(sf::VideoMode(WIDTH, HEIGHT), "Raycast");
  34.     window.setFramerateLimit(60);
  35.  
  36.     while(window.isOpen()){
  37.         for(uint16_t x = 0; x < WIDTH; x++){
  38.             bool collision = false;
  39.  
  40.             const float curr_angle = to_radians((ANGLE + FOV/2)-x *(FOV/WIDTH));
  41.             const std::pair<int, int> inc = ray_direction(curr_angle);
  42.  
  43.             //current tile
  44.             const int cx = static_cast<int>(px);
  45.             const int cy = static_cast<int>(py);
  46.  
  47.             //next x
  48.             float nx = 0.0f;
  49.             //horizontal x and y
  50.             float hx = 0.0f;
  51.             float hy = 0.0f;
  52.             //dist to next horizontal
  53.             float nh = 0.0f;
  54.  
  55.             //next y
  56.             float ny = 0.0f;
  57.             //vertical x and y
  58.             float vx = 0.0f;
  59.             float vy = 0.0f;
  60.             //dist to next vertial
  61.             float nv = 0.0f;
  62.  
  63.             if(inc.first == 1){
  64.                 nx = BLOCKSIZE - (px - cx);
  65.                 nv = std::tan(curr_angle);
  66.             }else{
  67.                 nx = -BLOCKSIZE * (px - cx);
  68.                 nv = -std::tan(curr_angle);
  69.             }
  70.             if(inc.second == 1){
  71.                 ny = BLOCKSIZE - (py - cy);
  72.                 nh = 1/std::tan(curr_angle);
  73.             }else{
  74.                 ny = -BLOCKSIZE * (py - cy);
  75.                 nh = -1/std::tan(curr_angle);
  76.             }
  77.  
  78.             //first collision with horizontal gridline
  79.             hx = ny/std::tan(curr_angle);
  80.             hy = ny;
  81.  
  82.             //first collision with vertical gridline
  83.             vx = nx;
  84.             vy = nx * std::tan(curr_angle);
  85.  
  86.             //distance to the wall
  87.             float w_dist = 0.0f;
  88.             std::cout << hx << " " << hy << " " << vx << " " << vy << '\n';
  89.  
  90.             //std::cin.get();
  91.  
  92.             while(collision == false){
  93.                 if(hx > vy){
  94.                     std::cout << "v: " << curr_angle << " " << level[px+vx][py+vy] << " " << vx << " " << vy << " " << x << '\n';
  95.                     std::cout << ny << '\n';
  96.                     if(level[int(px+vx)][int(py+vy)] > 0){
  97.                         collision = true;
  98.                         w_dist = std::sqrt(vx*vx + vy*vy);
  99.                     }else{
  100.                         vx += 1;
  101.                         vy += nv;
  102.                     }
  103.                 }else{
  104.                     std::cout << "h: " << curr_angle << " " << level[px+hx][py+hy] << " " << hx << " " << hy << " " << x << '\n';
  105.                     if(level[int(px+hx)][int(py+hy)] > 0){
  106.                         collision = true;
  107.                         w_dist = std::sqrt(hx*hx + hy*hy);
  108.                     }else{
  109.                         hx += nh;
  110.                         hy += 1;
  111.                     }
  112.                 }
  113.             }
  114.  
  115.             /*
  116.             sf::Vertex line[] = {
  117.                 sf::Vertex(sf::Vector2f(x, top), sf::Color::Red),
  118.                 sf::Vertex(sf::Vector2f(x, btm), sf::Color::Red)
  119.             };
  120.             window.draw(line, 2, sf::Lines);
  121.             */
  122.         }
  123.         window.clear(sf::Color::Black);
  124.         window.display();
  125.     }
  126.     return 0;
  127. }
  128.  
  129. float to_radians(const float degree){
  130.     return degree * 3.14159/180;
  131. }
  132.  
  133. std::pair<uint8_t,uint8_t> ray_direction(const float degree){
  134.     if(degree > 0 && degree < 90){
  135.         return std::make_pair(1, 1);
  136.     }else if(degree > 90 && degree < 180){
  137.         return std::make_pair(-1, 1);
  138.     }else if(degree > 180 && degree < 270){
  139.         return std::make_pair(-1, -1);
  140.     }else if(degree > 270 && degree < 360){
  141.         return std::make_pair(1, -1);
  142.     }
  143. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement