How do I correctly get the ending point of the ray and not get this? because it keep working then stop working
#include "SFML/Graphics.hpp"
#include "iostream"
#include "math.h"
constexpr float PI = 3.14159265358979323846264338327952f;
int main() {
float gridF = 64.f;
sf::RenderWindow window(sf::VideoMode(1000, 1000), "SFML Application");
window.setFramerateLimit(60);
sf::RectangleShape shape(sf::Vector2f(10.f, 10.f));
shape.setPosition(300.f, 300.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 = 0;
sf::CircleShape c;
c.setRadius(3.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::Clock clock;
sf::VertexArray cast(sf::LineStrip,2.f);
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 );
}
}
}
float a;
float b;
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)) {
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[1].position = sf::Vector2f(xy + (pdx * 180/PI),yx + (pdy * 180/PI));
float dir_y = sin(pa) ;
float dir_x = cos(pa) ;
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.f + (pow(dir_y,2)/pow(dir_x,2))) * gridF;
float delta_y = sqrt(1.f + (pow(dir_x,2)/pow(dir_y,2))) * gridF;
if (dir_x < 0)
{
step_x = -1;
sideDistX = ((px/64) - float(mapx)) * delta_x;
}
else
{
step_x = 1;
sideDistX = ((float(mapx) + 1.f) - (px/64)) * delta_x;
}
if (dir_y < 0)
{
step_y = -1;
sideDistY = ( (py/64) - float(mapy)) * delta_y;
}
else {
step_y = 1;
sideDistY = ((float(mapy) + 1.f) - (py/64)) * delta_y;
}
while (hit == 0)
{
if (sideDistX < sideDistY)
{
a = sideDistX;
mapx += step_x;
sideDistX += delta_x;
}
else
{
b = sideDistY;
mapy += step_y;
sideDistY += delta_y;
}
//Check if ray has hit a wall
if ( mapy < 8 && mapx < 8 && map[mapy][mapx] > 0) {
hit = 1;}
}
std::cout << mapy << " " << mapx << "\n";
c.setPosition( px + dir_x * a , py + dir_y * b) ;
window.clear();
for (int y = 0; y < mapY; y++) {
for (int x = 0; x < mapX; x++) {
window.draw(tile[y][x]);
}
}
window.draw(c);
window.draw(shape);
window.draw(cast);
window.display();
}