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

Author Topic: Moving rectangle  (Read 6563 times)

0 Members and 1 Guest are viewing this topic.

booster

  • Newbie
  • *
  • Posts: 4
    • View Profile
Moving rectangle
« on: November 01, 2021, 10:05:56 pm »
Hello to everyone! I'm learning sfml and I'm trying to move a rectangleShape based on input. The problem is that when i press a key for change direction, it starts moving from top, and not from last reached position. It has to be a grid-like movement. Can someone help me with this? How should I change the logic to achive that?
#include <iostream>
#include <cstdlib>
#include "game.h"
using namespace std;

int main()
{
        sf::RenderWindow window(sf::VideoMode(600, 600), "Move shape");
        sf::RectangleShape shape(sf::Vector2f(20, 20));
        shape.setFillColor(sf::Color::Red);
        shape.setPosition(sf::Vector2f(20, 20));

        int speed = 15;
        Clock clock;
        float felapsed = 0;
        sf::Vector2f dir(1, 0);
        int x = 9;
        int y = 0;

        while (window.isOpen())
        {
                sf::Event e;

                while (window.pollEvent(e))
                {
                        if (e.type == sf::Event::Closed)
                                window.close();
                }

                if (sf::Keyboard::isKeyPressed(Keyboard::Up)) dir = sf::Vector2f(0, -1);

                else if (sf::Keyboard::isKeyPressed(Keyboard::Down)) dir = sf::Vector2f(0, 1);

                else if (sf::Keyboard::isKeyPressed(Keyboard::Right)) dir = sf::Vector2f(1, 0);

                else if (sf::Keyboard::isKeyPressed(Keyboard::Left)) dir = sf::Vector2f(-1, 0);


                float frameTime = 1.0f / speed;

                window.clear();

                if (felapsed >= frameTime)
                {
                        if (dir == sf::Vector2f(1, 0))
                                x++;
                        else if (dir == sf::Vector2f(-1, 0))
                                x--;
                        else if (dir == sf::Vector2f(0, 1))
                                y++;
                        else if (dir == sf::Vector2f(0, -1))
                                y--;

                        shape.setPosition(dir.x*x*16, dir.y * y * 16);

                        window.draw(shape);

                        window.display();

                        felapsed -= frameTime;
                       
                }

                felapsed += clock.restart().asSeconds();
        }

        return 0;
}
 
« Last Edit: November 02, 2021, 11:42:15 am by eXpl0it3r »

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 10821
    • View Profile
    • development blog
    • Email
Re: Moving rectangle
« Reply #1 on: November 02, 2021, 11:44:23 am »
Since you never retrieve the position of the shape, you're also never move relative to the shape's current position. setPosition() sets the position in an absolute manner. If you want to specify the relative movement only, then you can use move(), otherwise you'll have to call getPosition() and calculate those values into the movement for the new absolute position.
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

booster

  • Newbie
  • *
  • Posts: 4
    • View Profile
Re: Moving rectangle
« Reply #2 on: November 02, 2021, 02:38:39 pm »
Thank you very much, i used move function to solve te problem. It toke me lot of time trying to solve it!!

 

anything