Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <SFML/Window.hpp>
- #include <SFML/Graphics.hpp>
- #include <iostream>
- #include <cmath>
- #include <vector>
- #include <utility>
- #define row std::vector<int>
- float to_radians(const float degree);
- std::pair<uint8_t,uint8_t> ray_direction(const float degree);
- std::vector<row> level = {{1,1,1,1,1,1,1,1,1,1},
- {1,1,0,1,0,1,0,0,0,1},
- {1,0,0,1,0,1,0,0,0,1},
- {1,0,0,0,0,0,1,0,0,1},
- {1,1,1,0,0,0,1,0,0,1},
- {1,1,0,0,0,0,1,0,0,1},
- {1,1,1,1,1,1,1,1,1,1}};
- int main(){
- const uint16_t HEIGHT = 600;
- const uint16_t WIDTH = 800;
- const float BLOCKSIZE = 1.0f;
- const float FOV = 60.0f;
- float ANGLE = 0.0f;
- float px = 5.5f;
- float py = 4.5f;
- sf::RenderWindow window(sf::VideoMode(WIDTH, HEIGHT), "Raycast");
- window.setFramerateLimit(60);
- while(window.isOpen()){
- for(uint16_t x = 0; x < WIDTH; x++){
- bool collision = false;
- const float curr_angle = to_radians((ANGLE + FOV/2)-x *(FOV/WIDTH));
- const std::pair<int, int> inc = ray_direction(curr_angle);
- //current tile
- const int cx = static_cast<int>(px);
- const int cy = static_cast<int>(py);
- //next x
- float nx = 0.0f;
- //horizontal x and y
- float hx = 0.0f;
- float hy = 0.0f;
- //dist to next horizontal
- float nh = 0.0f;
- //next y
- float ny = 0.0f;
- //vertical x and y
- float vx = 0.0f;
- float vy = 0.0f;
- //dist to next vertial
- float nv = 0.0f;
- if(inc.first == 1){
- nx = BLOCKSIZE - (px - cx);
- nv = std::tan(curr_angle);
- }else{
- nx = -BLOCKSIZE * (px - cx);
- nv = -std::tan(curr_angle);
- }
- if(inc.second == 1){
- ny = BLOCKSIZE - (py - cy);
- nh = 1/std::tan(curr_angle);
- }else{
- ny = -BLOCKSIZE * (py - cy);
- nh = -1/std::tan(curr_angle);
- }
- //first collision with horizontal gridline
- hx = ny/std::tan(curr_angle);
- hy = ny;
- //first collision with vertical gridline
- vx = nx;
- vy = nx * std::tan(curr_angle);
- //distance to the wall
- float w_dist = 0.0f;
- std::cout << hx << " " << hy << " " << vx << " " << vy << '\n';
- //std::cin.get();
- while(collision == false){
- if(hx > vy){
- std::cout << "v: " << curr_angle << " " << level[px+vx][py+vy] << " " << vx << " " << vy << " " << x << '\n';
- std::cout << ny << '\n';
- if(level[int(px+vx)][int(py+vy)] > 0){
- collision = true;
- w_dist = std::sqrt(vx*vx + vy*vy);
- }else{
- vx += 1;
- vy += nv;
- }
- }else{
- std::cout << "h: " << curr_angle << " " << level[px+hx][py+hy] << " " << hx << " " << hy << " " << x << '\n';
- if(level[int(px+hx)][int(py+hy)] > 0){
- collision = true;
- w_dist = std::sqrt(hx*hx + hy*hy);
- }else{
- hx += nh;
- hy += 1;
- }
- }
- }
- /*
- sf::Vertex line[] = {
- sf::Vertex(sf::Vector2f(x, top), sf::Color::Red),
- sf::Vertex(sf::Vector2f(x, btm), sf::Color::Red)
- };
- window.draw(line, 2, sf::Lines);
- */
- }
- window.clear(sf::Color::Black);
- window.display();
- }
- return 0;
- }
- float to_radians(const float degree){
- return degree * 3.14159/180;
- }
- std::pair<uint8_t,uint8_t> ray_direction(const float degree){
- if(degree > 0 && degree < 90){
- return std::make_pair(1, 1);
- }else if(degree > 90 && degree < 180){
- return std::make_pair(-1, 1);
- }else if(degree > 180 && degree < 270){
- return std::make_pair(-1, -1);
- }else if(degree > 270 && degree < 360){
- return std::make_pair(1, -1);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement