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

Author Topic: How do I make a rectangle move at the direction of a line  (Read 1881 times)

0 Members and 2 Guests are viewing this topic.

R4nd0mP3rSon

  • Newbie
  • *
  • Posts: 15
    • View Profile
How do I make a rectangle move at the direction of a line
« 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

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11030
    • View Profile
    • development blog
    • Email
Re: How do I make a rectangle move at the direction of a line
« Reply #1 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 and translate the key presses to setting the direction vector.
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

R4nd0mP3rSon

  • Newbie
  • *
  • Posts: 15
    • View Profile
Re: How do I make a rectangle move at the direction of a line
« Reply #2 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
« Last Edit: January 22, 2024, 07:00:15 am by R4nd0mP3rSon »

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11030
    • View Profile
    • development blog
    • Email
Re: How do I make a rectangle move at the direction of a line
« Reply #3 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
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

R4nd0mP3rSon

  • Newbie
  • *
  • Posts: 15
    • View Profile
Re: How do I make a rectangle move at the direction of a line
« Reply #4 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();
    }
}
« Last Edit: January 22, 2024, 04:09:25 pm by R4nd0mP3rSon »

Hapax

  • Hero Member
  • *****
  • Posts: 3379
  • My number of posts is shown in hexadecimal.
    • View Profile
    • Links
Re: How do I make a rectangle move at the direction of a line
« Reply #5 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();
:)
Selba Ward -SFML drawables
Cheese Map -Drawable Layered Tile Map
Kairos -Timing Library
Grambol
 *Hapaxia Links*

R4nd0mP3rSon

  • Newbie
  • *
  • Posts: 15
    • View Profile
Re: How do I make a rectangle move at the direction of a line
« Reply #6 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();
    }
}
« Last Edit: January 23, 2024, 08:57:04 am by R4nd0mP3rSon »

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11030
    • View Profile
    • development blog
    • Email
Re: How do I make a rectangle move at the direction of a line
« Reply #7 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!
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

Hapax

  • Hero Member
  • *****
  • Posts: 3379
  • My number of posts is shown in hexadecimal.
    • View Profile
    • Links
Re: How do I make a rectangle move at the direction of a line
« Reply #8 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.
Selba Ward -SFML drawables
Cheese Map -Drawable Layered Tile Map
Kairos -Timing Library
Grambol
 *Hapaxia Links*