Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <SFML/Graphics.hpp>
- #include <cmath>
- #define DEFAULT sf::Style::Default
- #define FULLSCREEN sf:Style::Fullscreen
- #define WIDTH 24
- #define HEIGHT 24
- int worldMap[WIDTH][HEIGHT]={
- {1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1},
- {1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1},
- {1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1},
- {1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1},
- {1,0,0,0,0,0,2,2,2,2,2,0,0,0,0,3,0,3,0,3,0,0,0,1},
- {1,0,0,0,0,0,2,0,0,0,2,0,0,0,0,0,0,0,0,0,0,0,0,1},
- {1,0,0,0,0,0,2,0,0,0,2,0,0,0,0,3,0,0,0,3,0,0,0,1},
- {1,0,0,0,0,0,2,0,0,0,2,0,0,0,0,0,0,0,0,0,0,0,0,1},
- {1,0,0,0,0,0,2,2,0,2,2,0,0,0,0,3,0,3,0,3,0,0,0,1},
- {1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1},
- {1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1},
- {1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1},
- {1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1},
- {1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1},
- {1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1},
- {1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1},
- {1,4,4,4,4,4,4,4,4,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1},
- {1,4,0,4,0,0,0,0,4,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1},
- {1,4,0,0,0,0,5,0,4,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1},
- {1,4,0,4,0,0,0,0,4,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1},
- {1,4,0,4,4,4,4,4,4,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1},
- {1,4,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1},
- {1,4,4,4,4,4,4,4,4,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1},
- {1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1}
- };
- int main(void){
- sf::RenderWindow window(sf::VideoMode(800, 600), "Raycast test", DEFAULT);
- window.setFramerateLimit(30);
- double posX = 22, posY = 12;
- double dirX = -1, dirY = 0;
- double planeX = 0, planeY = 0.66;
- double time = 0;
- double oldTime = 0;
- while(window.isOpen()){
- for(int x = 0; x < 800; x++){
- double cameraX = 2 * x / (double)800 - 1;
- double rayPosX = posX;
- double rayPosY = posY;
- double rayDirX = dirX + planeX * cameraX;
- double rayDirY = dirY + planeY * cameraX;
- int mapX = (int)rayPosX;
- int mapY = (int)rayPosY;
- double sideDistX;
- double sideDistY;
- double deltaDistX = sqrt(1 + (rayDirY * rayDirY) / (rayDirX * rayDirX));
- double deltaDistY = sqrt(1 + (rayDirX * rayDirX) / (rayDirY * rayDirY));
- double perpWallDist;
- int stepX;
- int stepY;
- int hit = 0;
- int side;
- if(rayDirX < 0){
- stepX = -1;
- sideDistX = (rayPosX - mapX) * deltaDistX;
- }else{
- stepX = 1;
- sideDistX = (mapX + 1.0 - rayPosX) * deltaDistX;
- }
- if(rayDirY < 0){
- stepY = -1;
- sideDistY = (rayPosY - mapY) * deltaDistY;
- }else{
- stepY = 1;
- sideDistY = (mapY + 1.0 - rayPosY) * deltaDistY;
- }
- while(hit == 0){
- if(sideDistX < sideDistY){
- sideDistX += deltaDistX;
- mapX += stepX;
- side = 0;
- }else{
- sideDistY += deltaDistY;
- mapY += stepY;
- side = 1;
- }
- if (worldMap[mapX][mapY] > 0){
- hit = 1;
- }
- }
- if(side == 0){
- perpWallDist = (mapX - rayPosX + (1 - stepX) / 2) /rayDirX;
- }else{
- perpWallDist = (mapY - rayPosY + (1 - stepY) / 2) /rayDirY;
- }
- int lineHeight = (int)(800 / perpWallDist);
- int drawStart = -lineHeight / 2 + 800 / 2;
- if(drawStart < 0){
- drawStart = 0;
- }
- int drawEnd = lineHeight / 2 + 800 / 2;
- if(drawEnd >= 800){
- drawEnd = 800 - 1;
- }
- sf::Color color;
- switch(worldMap[mapX][mapY]){
- case 1: color = sf::Color::Red; break;
- case 2: color = sf::Color::Green; break;
- case 3: color = sf::Color::Blue; break;
- case 4: color = sf::Color::White; break;
- default: color = sf::Color::Yellow; break;
- }
- sf::Vertex line[2] ={
- sf::Vertex(sf::Vector2f(x, drawStart), color),
- sf::Vertex(sf::Vector2f(x, drawEnd), color)
- };
- window.draw(line, 2, sf::Lines);
- window.display();
- }
- sf::Event event;
- while(window.pollEvent(event)){
- if(sf::Keyboard::isKeyPressed(sf::Keyboard::Escape)){
- window.close();
- }
- }
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement