Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: Is there a better way to do the line rotation and player directions  (Read 963 times)

0 Members and 1 Guest are viewing this topic.

R4nd0mP3rSon

  • Newbie
  • *
  • Posts: 15
    • View Profile
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();
    }
}
« Last Edit: January 24, 2024, 04:00:46 pm by R4nd0mP3rSon »

Hapax

  • Hero Member
  • *****
  • Posts: 3379
  • My number of posts is shown in hexadecimal.
    • View Profile
    • Links
Re: Is there a better way to do the line rotation and player directions
« Reply #1 on: January 24, 2024, 06:06:31 pm »
Did the answers given on the other thread that you made about this topic not help?
https://en.sfml-dev.org/forums/index.php?topic=29391.msg181091#msg181091
Selba Ward -SFML drawables
Cheese Map -Drawable Layered Tile Map
Kairos -Timing Library
Grambol
 *Hapaxia Links*

kojack

  • Sr. Member
  • ****
  • Posts: 343
  • C++/C# game dev teacher.
    • View Profile
Re: Is there a better way to do the line rotation and player directions
« Reply #2 on: January 25, 2024, 02:57:06 am »
What exactly is it doing that isn't correct? I just tried that code and it rotates and moves in all directions fine.
(Although I'm using SFML 3).

 

anything