I tried doing the same calculation as this tutorial
https://lodev.org/cgtutor/raycasting.htmlbut it didn't work and now it like this ,Does anyone know what wrong with it?
#include "SFML/Graphics.hpp"
#include "sstream"
#include "math.h"
constexpr float PI = 3.14159265358979323846264338327952f;
#define SCREEN_WIDTH 1000
#define SCREEN_HEIGHT 1000
void draw3DRays(float pa,sf::RenderTarget *target,float px,float py) {
int maper[][8] = {
{1, 1, 1, 1, 1, 1, 1, 1},
{1, 0, 0, 0, 0, 1, 0, 1},
{1, 0, 0, 0, 0, 0, 0, 1},
{1, 0, 0, 1, 1, 0, 0, 1},
{1, 0, 1, 1, 0, 0, 0, 1},
{1, 0, 0, 0, 0, 0, 0, 1},
{1, 0, 0, 1, 0, 0, 0, 1},
{1, 1, 1, 1, 1, 1, 1, 1}
};
std::stringstream ss;
sf::Text text;
sf::CircleShape c;
c.setRadius(10.f);
c.setPointCount(100.f);
float dir_y = sin(pa) * 5.f;
float dir_x = cos(pa) * 5.f;
int mapX = floor(px/64);
int mapY = floor(py/64);
float sideDistX;
float sideDistY;
int step_x;
int step_y;
int hit = 0;
float delta_x = sqrt(1 + pow(dir_y,2)/pow(dir_x,2));
float delta_y = sqrt(1 + pow(dir_x,2)/pow(dir_y,2));
if (dir_x < 0)
{
step_x = -1;
sideDistX = (px/64 - mapX) * delta_x;
}
else if (dir_x > 0)
{
step_x = 1;
sideDistX = (mapX + 1 - px/64) * delta_x;
}
else{
sideDistX = 0;
}
if (dir_y < 0)
{
step_y = -1;
sideDistY = (py/64 - mapY) * delta_y;
}
else if (dir_y > 0){
step_y = 1;
sideDistY = (mapY + 1 - py/64) * delta_y;
}
else
{
sideDistY = 0;
}
while (hit == 0)
{
if (sideDistX < sideDistY)
{
sideDistX += delta_x;
mapX += step_x;
}
else
{
sideDistY += delta_y;
mapY += step_y;
}
//Check if ray has hit a wall
if ( mapY < 8 && mapX < 8 && maper[mapY][mapX] > 0) hit = 1;
}
sf::VertexArray line(sf::Lines,2);
line[0] = sf::Vector2f(px, py);
line[1]= sf::Vector2f(sideDistX,sideDistY);
target->draw(line);
}
int main() {
float gridF = 64.f;
sf::RenderWindow window(sf::VideoMode(1000, 1000), "SFML Application");
window.setFramerateLimit(60);
sf::RectangleShape shape(sf::Vector2f(50.f, 50.f));
shape.setPosition(144.f, 144.f);
shape.setFillColor(sf::Color::Cyan);
sf::RectangleShape line(sf::Vector2f (70.f,3.f));
line.setFillColor(sf::Color::Cyan);
float timer;
float timerMax;
float px, py, pdx, pdy;
float pa;
sf::CircleShape c;
c.setRadius(10.f);
c.setPointCount(100.f);
float yx;
float xy;
sf::Text text;
sf::Font font;
int map[][8] = {
{1, 1, 1, 1, 1, 1, 1, 1},
{1, 0, 0, 0, 0, 1, 0, 1},
{1, 0, 0, 0, 0, 0, 0, 1},
{1, 0, 0, 1, 1, 0, 0, 1},
{1, 0, 1, 1, 0, 0, 0, 1},
{1, 0, 0, 0, 0, 0, 0, 1},
{1, 0, 0, 1, 0, 0, 0, 1},
{1, 1, 1, 1, 1, 1, 1, 1}
};
int mapX = 8;
int mapY = 8;
px = 300.f;
py = 300.f;
timerMax = 1.5f;
timer = timerMax;
pdx = cos(pa) * 5;
pdy = sin(pa) * 5;
sf::RectangleShape ray(sf::Vector2f(100.f,2.f));
ray.setFillColor(sf::Color::Green);
sf::Clock clock;
sf::VertexArray cast(sf::LineStrip,2.f);
font.loadFromFile("MadimiOne-Regular.ttf");
sf::RectangleShape tile[mapY][mapX];
for (int y = 0; y < mapY; y++) {
for (int x = 0; x < mapX; x++) {
if (map[y][x] == 1) {
tile[y][x].setSize(sf::Vector2f(gridF, gridF));
tile[y][x].setFillColor(sf::Color(55, 55, 55, 255));
tile[y][x].setOutlineThickness(1.f);
tile[y][x].setOutlineColor(sf::Color::White);
tile[y][x].setPosition(x * gridF , y * gridF);
} else {
tile[y][x].setSize(sf::Vector2f(gridF, gridF));
tile[y][x].setFillColor(sf::Color::Transparent);
tile[y][x].setOutlineThickness(1.f);
tile[y][x].setOutlineColor(sf::Color::White);
tile[y][x].setPosition(x * gridF , y * gridF );
}
}
}
pa = PI * 2;
while (window.isOpen()) {
sf::Event event;
while (window.pollEvent(event)) {
if (event.type == sf::Event::Closed)
window.close();
}
if (sf::Keyboard::isKeyPressed(sf::Keyboard::A)&& timer >= timerMax ) {
pa -= 0.1f;
pdx = cos(pa) * 5;
pdy = sin(pa) * 5;
}
else if (sf::Keyboard::isKeyPressed(sf::Keyboard::D)&& timer >= timerMax ) {
pa += 0.1f;
pdx = cos(pa) * 5;
pdy = sin(pa) * 5;
}
else if (sf::Keyboard::isKeyPressed(sf::Keyboard::W)) {
px += pdx;
py += pdy;
}
else if (sf::Keyboard::isKeyPressed(sf::Keyboard::S)) {
float radians = pa * PI/180.f;
px -= pdx;
py -= pdy;
}
if (timer >= timerMax) {
timer = 0;
}
else {
timer += 1.f;
}
shape.setPosition(px,py);
xy = px + (shape.getSize().x/2);
yx = py + (shape.getSize().y/2);
cast[0].position = sf::Vector2f(xy,yx);
cast[0].position = sf::Vector2f(xy + (pdx * 180/PI),yx + (pdy * 180/PI));
window.clear();
for (int y = 0; y < mapY; y++) {
for (int x = 0; x < mapX; x++) {
window.draw(tile[y][x]);
}
}
window.draw(shape);
window.draw(cast);
draw3DRays(pa,&window,px,py);
window.display();
}