SFML community forums

Help => General => Topic started by: R4nd0mP3rSon on January 21, 2024, 05:16:18 pm

Title: How do I make a rectangle move at the direction of a line
Post by: R4nd0mP3rSon on January 21, 2024, 05:16:18 pm
How do I make the line change it direction around the player when  I pressed "A" or "D" and when I pressed w it wile move here the line is pointing at
Title: Re: How do I make a rectangle move at the direction of a line
Post by: eXpl0it3r on January 21, 2024, 09:50:40 pm
You need to give us a bit more info to help you.

It all depends on how you're currently moving the rectangle and what "at the direction of a line" really means to you.

If you call move() on a RectangleShape, you can change the x and y values to change in what direction you're moving. You could represent the direction as a Vector2f, say moving left would be vec.x = 1 and moving down would be vec.y = -1. Then you set a value for the speed and multiply it with the direction vector before passing it to the move function, e.g. rect.move(vec * speed).

As for the inputs, you can handle events (https://www.sfml-dev.org/tutorials/2.6/window-events.php) and translate the key presses to setting the direction vector.
Title: Re: How do I make a rectangle move at the direction of a line
Post by: R4nd0mP3rSon on January 22, 2024, 06:42:31 am
Basically I want to have an arrow which can turn 360 degree around the player and it is  turn by pressing  "a" or "d"  then it would point to a direction on the map where the  player can move by pressing "w" to the direction where the arrow is pointing at but I don't how to rotate it
Title: Re: How do I make a rectangle move at the direction of a line
Post by: eXpl0it3r on January 22, 2024, 08:35:08 am
Here's an example of a rotating triangle that will point to the mouse position.
You can adapt it to rotate with keyboard inputs.

https://github.com/eXpl0it3r/Examples/blob/master/SFML/RotatingTriangle.cpp
Title: Re: How do I make a rectangle move at the direction of a line
Post by: R4nd0mP3rSon on January 22, 2024, 01:12:22 pm
i dont know if im dumb or something but I still cant figure it out here my code I want make the line rotate 360 degree around the player but when I do it not very accurate



[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.1 * dt * dtMultiplier;
            if(pa < 0){
                pa += 2* PI;
            }
            pdx =cos(pa) * 5;
            pdy = sin(pa) * 5;

        }
        else if(sf::Keyboard::isKeyPressed(sf::Keyboard::D)){
            pa += 0.1 * dt * dtMultiplier;
            if(pa >PI * 2){
                pa -= 2 * PI;
            }
            pdx =cos(pa) * 5;
            pdy = sin(pa) * 5;




        }
        else if(sf::Keyboard::isKeyPressed(sf::Keyboard::W)){
           px -= pdx * dt * dtMultiplier;
           py -= pdy * dt * dtMultiplier;

        }
        else if(sf::Keyboard::isKeyPressed(sf::Keyboard::S)){
            px += pdx * dt * dtMultiplier;
            py += pdy * dt * dtMultiplier;
        }

        shape.setPosition(px,py);
        line.setPosition(shape.getPosition().x - (shape.getSize().x),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();
    }
}
Title: Re: How do I make a rectangle move at the direction of a line
Post by: Hapax on January 22, 2024, 06:22:46 pm
Here's an example...
Could this line:
https://github.com/eXpl0it3r/Examples/blob/master/SFML/RotatingTriangle.cpp#L26
auto deltaPosition = sf::Vector2f{ mousePosition.x - triangle.getPosition().x, mousePosition.y - triangle.getPosition().y };
not just be this:
auto deltaPosition = mousePosition - triangle.getPosition();
:)
Title: Re: How do I make a rectangle move at the direction of a line
Post by: R4nd0mP3rSon on January 23, 2024, 08:02:03 am
How do I rotate the line the direction is decided like this


float px =300.f;
float py = 300.f;
float pa,pdx,pdy;

    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.1 * dt * dtMultiplier;
            if(pa < 0){
                pa += 2* PI;
            }
            pdx =cos(pa) * 5;
            pdy = sin(pa) * 5;

        }
        else if(sf::Keyboard::isKeyPressed(sf::Keyboard::D)){
            pa += 0.1 * dt * dtMultiplier;
            if(pa >PI * 2){
                pa -= 2 * PI;
            }
            pdx =cos(pa) * 5;
            pdy = sin(pa) * 5;




        }
        else if(sf::Keyboard::isKeyPressed(sf::Keyboard::W)){
           px -= pdx * dt * dtMultiplier;
           py -= pdy * dt * dtMultiplier;

        }
        else if(sf::Keyboard::isKeyPressed(sf::Keyboard::S)){
            px += pdx * dt * dtMultiplier;
            py += pdy * dt * dtMultiplier;
        }

        shape.setPosition(px,py);
        line.setPosition(shape.getPosition().x - (shape.getSize().x),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();
    }
}
Title: Re: How do I make a rectangle move at the direction of a line
Post by: eXpl0it3r on January 23, 2024, 08:20:33 am
Could this line:
https://github.com/eXpl0it3r/Examples/blob/master/SFML/RotatingTriangle.cpp#L26
auto deltaPosition = sf::Vector2f{ mousePosition.x - triangle.getPosition().x, mousePosition.y - triangle.getPosition().y };
not just be this:
auto deltaPosition = mousePosition - triangle.getPosition();
Yeah, totally! ;D
Will fix that right up, thanks!
Title: Re: How do I make a rectangle move at the direction of a line
Post by: Hapax on January 23, 2024, 06:34:09 pm
Will fix that right up, thanks!
You're welcome. :)

How do I rotate the line the direction is decided like this
        line.setPosition(shape.getPosition().x - (shape.getSize().x),shape.getPosition().y + (shape.getSize().y/2));
It would depend on what "line" is. If it's a rectangle, set its rotation to the direction you already calculated. If it's a vertex array (2-vertex line), set its start and end points by using the offset you just calculated.