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

Author Topic: Simple rotation does not work  (Read 1140 times)

0 Members and 1 Guest are viewing this topic.

BlueSky

  • Newbie
  • *
  • Posts: 4
    • View Profile
    • Email
Simple rotation does not work
« on: November 21, 2020, 09:47:36 pm »
#include <iostream>
#include <SFML/Graphics.hpp>

const int WIDTH = 1024;
const int HEIGHT = 860;
const int W_CENTER = WIDTH / 2;
const int H_CENTER = HEIGHT / 2;

int main()
{
        sf::RenderWindow window(sf::VideoMode(1024, 860), "Asteroids");

        sf::RectangleShape rectangle(sf::Vector2f(50.0f, 50.0f));
        rectangle.setFillColor(sf::Color(200, 0, 200));
        rectangle.setPosition(sf::Vector2f(W_CENTER, H_CENTER));
        rectangle.setOrigin(25, 25);

        float angle = 1;

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

                        if (sf::Keyboard::isKeyPressed(sf::Keyboard::Left))
                        {

                                rectangle.rotate(-angle);
                        }
                       
                        if (sf::Keyboard::isKeyPressed(sf::Keyboard::Right))
                        {

                                rectangle.rotate(angle);
                        }

                }



                window.clear(sf::Color::Black);
                window.draw(rectangle);
                window.display();
        }

        return 0;
}
 

Very simple, but if I mouse the mouse around at the same time I press either the Left or Right key, the rotation goes nuts and you see that I don't have any work to do with the mouse, just with the keys. Maybe I am missing something, anyway, makes no snese tp me.

Thank you!

Stauricus

  • Sr. Member
  • ****
  • Posts: 369
    • View Profile
    • A Mafia Graphic Novel
    • Email
Re: Simple rotation does not work
« Reply #1 on: November 22, 2020, 12:27:09 am »
well, for me, when I move the mouse the square turns faster  ;D
my guess is that the default spinning speed depends on the default keyboard key delay. each signal sent by the keyboard generates one single event.
but when you move the mouse, lots of other events are also generated. and since the arrow key is being hold down, your code recognizes if (sf::Keyboard::isKeyPressed(sf::Keyboard::Left)) as true and rotates the square by the number of new events generated. I don't know if I made myself clear.

anyway, if you just want to check is a key is being holded down, you don't need events for that. just move the 
if (sf::Keyboard::isKeyPressed(sf::Keyboard::Left))//or Right, with the rotation commands
outside the while (window.pollEvent(event)), but keep it inside the while (window.isOpen()) loop.
Visit my game site (and hopefully help funding it? )
Website | IndieDB

BlueSky

  • Newbie
  • *
  • Posts: 4
    • View Profile
    • Email
Re: Simple rotation does not work
« Reply #2 on: November 22, 2020, 01:11:45 am »
Now it makes perfect sense, thank you!  ;D