Advertisement
Guest User

RayCast

a guest
Apr 12th, 2024
61
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 6.68 KB | Source Code | 0 0
  1. #include "SFML/Graphics.hpp"
  2. #include "iostream"
  3. #include "math.h"
  4.  
  5.  
  6. constexpr float PI = 3.14159265358979323846264338327952f;
  7. constexpr  float dr = 0.0174532925f;
  8.  
  9. float deg_to_rad(float i_degrees)
  10. {
  11.    
  12.     return PI * i_degrees / 180;
  13. }
  14.  
  15. int main() {
  16.     float gridF = 64.f;
  17.     float dist[60]{};
  18.     float io[60]{};
  19.     sf::RenderWindow window(sf::VideoMode(1000, 1000), "SFML Application");
  20.     window.setFramerateLimit(60);
  21.     sf::RectangleShape shape(sf::Vector2f(10.f, 10.f));
  22.     shape.setPosition(300.f, 300.f);
  23.     shape.setFillColor(sf::Color::Cyan);
  24.     sf::RectangleShape line(sf::Vector2f(70.f, 3.f));
  25.     line.setFillColor(sf::Color::Cyan);
  26.     sf::RectangleShape shape1[60];
  27.     float timer;
  28.     float timerMax;
  29.     float px, py, pdx, pdy;
  30.     float pa = 0;
  31.     sf::CircleShape c;
  32.     c.setRadius(3.f);
  33.     c.setPointCount(100.f);
  34.     float yx;
  35.     float xy;
  36.  
  37.     sf::Text text;
  38.     sf::Font font;
  39.     float rad[60]{};
  40.     int map[][8] = {
  41.             {1, 1, 1, 1, 1, 1, 1, 1},
  42.             {1, 0, 0, 0, 0, 0, 0, 1},
  43.             {1, 0, 0, 0, 0, 0, 0, 1},
  44.             {1, 0, 0, 0, 0, 0, 0, 1},
  45.             {1, 0, 0, 0, 0, 0, 0, 1},
  46.             {1, 0, 0, 0, 0, 0, 0, 1},
  47.             {1, 0, 0, 0, 0, 0, 0, 1},
  48.             {1, 1, 1, 1, 1, 1, 1, 1}
  49.     };
  50.  
  51.     int mapX = 8;
  52.     int mapY = 8;
  53.     px = 300.f;
  54.     float projection;
  55.     py = 300.f;
  56.     timerMax = 1.5f;
  57.     timer = timerMax;
  58.    
  59.     pdx = cos(pa) * 5;
  60.     pdy = sin(pa) * 5;
  61.     sf::Clock clock;
  62.     sf::VertexArray cast(sf::LineStrip, 2.f);
  63.     sf::VertexArray lines(sf::LinesStrip,60);
  64.  
  65.  
  66.     sf::RectangleShape tile[mapY][mapX];
  67.     for (int y = 0; y < mapY; y++) {
  68.         for (int x = 0; x < mapX; x++) {
  69.             if (map[y][x] == 1) {
  70.                 tile[y][x].setSize(sf::Vector2f(gridF, gridF));
  71.                 tile[y][x].setFillColor(sf::Color(55, 55, 55, 255));
  72.                 tile[y][x].setOutlineThickness(1.f);
  73.                 tile[y][x].setOutlineColor(sf::Color::White);
  74.                 tile[y][x].setPosition(x * gridF, y * gridF);
  75.             } else {
  76.                 tile[y][x].setSize(sf::Vector2f(gridF, gridF));
  77.                 tile[y][x].setFillColor(sf::Color::Transparent);
  78.                 tile[y][x].setOutlineThickness(1.f);
  79.                 tile[y][x].setOutlineColor(sf::Color::White);
  80.                 tile[y][x].setPosition(x * gridF, y * gridF);
  81.             }
  82.  
  83.         }
  84.     }
  85.     float a;
  86.    
  87.     float  ra;
  88.     pa = PI * 2;
  89.    
  90.     sf::VertexArray fov{sf::TriangleFan,60+1};
  91.     while (window.isOpen()) {
  92.  
  93.  
  94.         sf::Event event;
  95.         while (window.pollEvent(event)) {
  96.             if (event.type == sf::Event::Closed)
  97.                 window.close();
  98.         }
  99.  
  100.  
  101.         if (sf::Keyboard::isKeyPressed(sf::Keyboard::A) && timer >= timerMax) {
  102.             pa -= 0.1f;
  103.  
  104.             pdx = cos(pa) * 5;
  105.             pdy = sin(pa) * 5;
  106.  
  107.  
  108.         } else if (sf::Keyboard::isKeyPressed(sf::Keyboard::D) && timer >= timerMax) {
  109.             pa += 0.1f;
  110.             pdx = cos(pa) * 5;
  111.             pdy = sin(pa) * 5;
  112.  
  113.  
  114.         } else if (sf::Keyboard::isKeyPressed(sf::Keyboard::W)) {
  115.             px += pdx;
  116.             py += pdy;
  117.  
  118.         } else if (sf::Keyboard::isKeyPressed(sf::Keyboard::S)) {
  119.  
  120.             px -= pdx;
  121.             py -= pdy;
  122.         }
  123.         if (timer >= timerMax) {
  124.             timer = 0;
  125.         } else {
  126.             timer += 1.f;
  127.         }
  128.  
  129.         shape.setPosition(px, py);
  130.         xy = px + (shape.getSize().x / 2);
  131.         yx = py + (shape.getSize().y / 2);
  132.         cast[0].position = sf::Vector2f(xy, yx);
  133.         cast[1].position = sf::Vector2f(xy + (pdx * 180 / PI), yx + (pdy * 180 / PI));
  134.         for (int i = 0; i < 60; i++) {
  135.  
  136.             ra = pa + (float(30-i)* dr);
  137.          
  138.             float dir_y = sin(ra);
  139.             float dir_x = cos(ra);
  140.  
  141.  
  142.             int mapx = floor(px / 64);
  143.             int mapy = floor(py / 64);
  144.  
  145.  
  146.             float sideDistX;
  147.             float sideDistY;
  148.  
  149.             int step_x;
  150.             int step_y;
  151.             int hit = 0;
  152.  
  153.             float delta_x = sqrt(1.f + (pow(dir_y, 2) / pow(dir_x, 2))) * gridF;
  154.             float delta_y = sqrt(1.f + (pow(dir_x, 2) / pow(dir_y, 2))) * gridF;
  155.  
  156.             if (dir_x < 0) {
  157.  
  158.                 step_x = -1;
  159.                 sideDistX = ((xy / 64) - float(mapx)) * delta_x;
  160.  
  161.             } else {
  162.                 step_x = 1;
  163.                 sideDistX = ((float(mapx) + 1.f) - (xy / 64)) * delta_x;
  164.  
  165.             }
  166.  
  167.             if (dir_y < 0) {
  168.                 step_y = -1;
  169.                 sideDistY = ((yx / 64) - float(mapy)) * delta_y;
  170.  
  171.             } else {
  172.                 step_y = 1;
  173.                 sideDistY = ((float(mapy) + 1.f) - (yx / 64)) * delta_y;
  174.             }
  175.  
  176.             while (hit == 0) {
  177.                 if (sideDistX < sideDistY) {
  178.                     a = sideDistX;
  179.                     mapx += step_x;
  180.                     sideDistX += delta_x;
  181.                    
  182.  
  183.                 }
  184.                 else{
  185.                     a = sideDistY;
  186.                     mapy += step_y;
  187.                     sideDistY += delta_y;
  188.                  
  189.                 }
  190.                 //Check if ray has hit a wall
  191.                 if (mapy < 8 && mapx < 8 && map[mapy][mapx] > 0 ) {
  192.                     hit = 1;
  193.  
  194.                 }
  195.  
  196.  
  197.             }
  198.  
  199.             dist[i] =  dir_x;
  200.             io[i] = ra;
  201.             fov[0].position = sf::Vector2f(xy, yx);
  202.             fov[1 + i].position = sf::Vector2f(xy + dir_x * a, yx + dir_y * a);
  203.  
  204.             shape1[i].setSize(sf::Vector2f (5.f,5.f));
  205.             shape1[i].setPosition(xy + dir_x * a, yx + dir_y * a);
  206.             shape1[i].setFillColor(sf::Color::Green);
  207.  
  208.             rad[i] = a;
  209.  
  210.  
  211.         }
  212.         projection = 0.5f*64.f/ tan(deg_to_rad(0.5f*58.75f));
  213.  
  214.         window.clear();
  215.  
  216.         for(int i =0;i < 60;i++) {
  217.  
  218.  
  219.             float shape_height = round(1000.f* projection  /(rad[i] * dist[i]));
  220.             sf::RectangleShape shape2(sf::Vector2f(16.666f, shape_height));
  221.             shape2.setFillColor(sf::Color(0, 255 * (1 - rad[i] / 800), 0));
  222.             shape2.setPosition(i * 16.666f,0.5f* (1000.f - shape_height));
  223.             if(shape_height >= 900){
  224.                 std::cout << i << "\n";
  225.             }
  226.             window.draw(shape2);
  227.         }
  228.         for (int y = 0; y < mapY; y++) {
  229.             for (int x = 0; x < mapX; x++) {
  230.                 window.draw(tile[y][x]);
  231.             }
  232.         }
  233.  
  234.         window.draw(fov);
  235.         for(int i = 0;i < 60;i++) {
  236.             window.draw(shape1[i]);
  237.         }
  238.  
  239.  
  240.         window.draw(shape);
  241.         window.draw(cast);
  242.  
  243.  
  244.  
  245.         window.display();
  246.     }
  247.  
  248.  
  249. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement