is there a better way to do the line rotation and player directions because it doesn't seem to be able to point to all directions
#include "SFML/Graphics.hpp"
#include "iostream"
#include "math.h"
#define PI 3.14159265358979323846264338327952
int main()
{
sf::RenderWindow window(sf::VideoMode(1000, 1000), "SFML Application");
window.setFramerateLimit(60);
sf::RectangleShape shape(sf::Vector2f(50.f,50.f));
shape.setPosition(100.f, 100.f);
shape.setFillColor(sf::Color::Cyan);
sf::RectangleShape line(sf::Vector2f (70.f,3.f));
line.setFillColor(sf::Color::Cyan);
//float stepx,step_y;
float px,py,pdx,pdy;
float pa;
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}
};
sf::View view;
//float viewSpeed= 200.f;
float dtMultiplier = 60.f;
float dt;
float gridF= 100.f;
int mapX = 8;
int mapY = 8;
px = 300.f;
py = 300.f;
pdx =cos(pa) * 5;
pdy = sin(pa) * 5;
int mapS = 16;
sf::Clock clock;
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 + 50.f,y* gridF + 50.f);
}
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 + 50.f,y* gridF + 50.f);
}
}
}
while (window.isOpen())
{
dt = clock.restart().asSeconds();
sf::Event event;
while (window.pollEvent(event))
{
if (event.type == sf::Event::Closed)
window.close();
}
if(sf::Keyboard::isKeyPressed(sf::Keyboard::A)){
pa -= 0.1f ;
if(pa < 0){
pa += 2 * PI;
}
pdx =cos(pa) * 5;
pdy = sin(pa) * 5;
std::cout << 0.1 * 360 /6.28 << "\n";
line.setRotation(pa * 360/(PI *2));
}
else if(sf::Keyboard::isKeyPressed(sf::Keyboard::D)){
pa += 0.1f ;
if(pa > PI * 2){
pa -= 2 * PI ;
}
pdx =cos(pa) * 5;
pdy = sin(pa) * 5;
std::cout << (pa* 180/PI) + 180 << "\n";
line.setRotation(pa * 360/(PI*2));
}
else if(sf::Keyboard::isKeyPressed(sf::Keyboard::W)){
px += pdx ;
py += pdy ;
}
else if(sf::Keyboard::isKeyPressed(sf::Keyboard::S)){
px -= pdx ;
py -= pdy ;
}
shape.setPosition(px,py);
line.setPosition(shape.getPosition().x + (shape.getSize().x/2),shape.getPosition().y+ shape.getSize().y /2 );
window.clear();
window.draw(line);
window.draw(shape);
for(int y =0;y < mapY;y++) {
for (int x = 0; x < mapX; x++) {
window.draw(tile[y][x]);
}
}
window.display();
}
}