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

Author Topic: Smooth Movement  (Read 7113 times)

0 Members and 1 Guest are viewing this topic.

Karsomir

  • Newbie
  • *
  • Posts: 14
    • View Profile
Smooth Movement
« on: August 14, 2013, 02:57:06 pm »
Hi, i've recently tried to move sprite in my Pong game, and it basically works but when i press key it moves one tick, and if i keep the key pressed it starts to move normally. Using sfml 2.1 and events and some booleans flags, something like this:
bool holdingLeft;

while(window.pollEvent(event))
        {
            switch(event.type)
            {
            case sf::Event::KeyPressed:
                    handlePlayerInput (event.key.code, true);
                    break;
            }
        }
void handlePlayerInput(sf::Keyboard::Key key, bool isPressed)
{
    if (key == sf::Keyboard::Left)
        holdingLeft = isPressed;
}

sf::Vector2f movement(0.f, 0.f);
if (holdingLeft)
            movement.x -= 10.0f;
sprPlayer.move(movement);
 

My question is why is this happening and how to make it run smoothly?
PS. I noticed that when i move the mouse over the window in the same time it is bit better. It seems like window isn't focused and rendering whole time and when u move mouse over it runs smoother. ( I haven't done anything connected with mouse in code )

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11029
    • View Profile
    • development blog
    • Email
Re: Smooth Movement
« Reply #1 on: August 14, 2013, 03:04:03 pm »
Sometimes, people try to use the KeyPressed event to implement smooth movement. Doing so will not produce smooth movements, because when you hold a key you only get a few events (remember, the repeat delay). Smooth movements can only be achieved by using real-time keyboard input with sf::Keyboard (see the dedicated tutorial).

If you still want to go event based, then you'll have to react to the state changes of the key (pressed vs released) and move stuff as long as the key state is pressed, but not in the event loop. It's easier to use sf::Keyboard::isKeypressed instead of "recreating" it with events. ;)
« Last Edit: August 14, 2013, 03:24:39 pm by eXpl0it3r »
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

Karsomir

  • Newbie
  • *
  • Posts: 14
    • View Profile
Re: Smooth Movement
« Reply #2 on: August 14, 2013, 03:07:09 pm »
Thank you, and sorry. :-[

EDIT:

Actually I've changed this to:
sf::Vector2f movement(0.f, 0.f);
if (sf::Keyboard::isKeyPressed(sf::Keyboard::Up))
movement.y -= 10.0f;
... and it's still the same. I get the feeling that something is wrong with focusing window and updating the loop.
As i said if i keep moving mouse over the window, the input feels very smooth and framerate goes up, but if I dont i feel like framerate is rly low, I don't hear my gfx card running and movement is stuttering.
« Last Edit: August 14, 2013, 03:24:27 pm by Karsomir »

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11029
    • View Profile
    • development blog
    • Email
Re: Smooth Movement
« Reply #3 on: August 14, 2013, 03:31:39 pm »
I hope you didn't do that in the event loop...

Can you show the complete but minimal source code, so we're sure you're doing it right? :)

You can also try to see what your FPS is, so you'll know if goes down with mouse movement or not.
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

MorleyDev

  • Full Member
  • ***
  • Posts: 219
  • "It is not enough for code to work."
    • View Profile
    • http://www.morleydev.co.uk/
Re: Smooth Movement
« Reply #4 on: August 14, 2013, 03:48:32 pm »
You know, handling input in events vs polling is a valid way of doing this. That quote is misleading as I've said in another thread on the topic. Evens are a clean way to separate input checking from logic, turning them into something detached from SFML to keep input and outputs on the edges of the program instead of mixed throughout.

So he would just need to handle sf::Event::KeyReleased and turn off the movement flag.
« Last Edit: August 14, 2013, 03:51:06 pm by MorleyDev »
UnitTest11 - A unit testing library in C++ written to take advantage of C++11.

All code is guilty until proven innocent, unworthy until tested, and pointless without singular and well-defined purpose.

Karsomir

  • Newbie
  • *
  • Posts: 14
    • View Profile
Re: Smooth Movement
« Reply #5 on: August 14, 2013, 04:00:22 pm »
I hope you didn't do that in the event loop...
I actually did. Everything is working fine now. :-[
As for Morley - I've done this exactly this way with releasing key and turning off flag, I guess both ways will work fine now, but I'll check it. Still I guess what u're saying is not technical issue but rather design and esthetical.
Thanks again eX. Cheers for brave new pong :) We can change Topic name to [Solved].
« Last Edit: August 14, 2013, 04:08:58 pm by Karsomir »

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: Smooth Movement
« Reply #6 on: August 14, 2013, 04:15:16 pm »
Note that there's an important difference between keyboard input and keyboard events: the former will give the keyboard state regardless of any window, while the latter will be triggered only if the corresponding window has focus.
Laurent Gomila - SFML developer

Karsomir

  • Newbie
  • *
  • Posts: 14
    • View Profile
Re: Smooth Movement
« Reply #7 on: August 14, 2013, 04:38:43 pm »
Point taken. I've run some tests with both implementations of movement ( 2 open windows set aside ) and while moving both my sprites simultaneously ( while having one window focused ) I noticed that with same input  after moving my sprite bit around, they don't end finally in same place. I feel like event driven implementation is kinda more accurate. Nice to meet you Laurent! ;D

 

anything