SFML community forums

Help => General => Topic started by: ka0s420 on April 04, 2017, 01:31:48 am

Title: Choppy Movement With DeltaTime
Post by: ka0s420 on April 04, 2017, 01:31:48 am
Hi everyone,

I'm struggling to get smooth movement in a game I'm working on. I was originally using a fixed timestep loop for handling the game logic, but I noticed that the movement was choppy, so I changed it (at least for now) to use delta time instead, but the choppy movement still persists.

Here is a minimal example of the code :

#include <SFML\Graphics.hpp>

int main()
{
       
        sf::RenderWindow win;
        win.create(sf::VideoMode(640, 512), "");
        win.setVerticalSyncEnabled(true);
        sf::Sprite p;
        sf::Texture t;
        t.loadFromFile("graphics/player.png");
        p.setTexture(t);
        p.setPosition(100, 100);
        sf::Clock clock;
        sf::Time dt;
        while (win.isOpen()) {
                dt = clock.restart();
                sf::Event event;
               
                while (win.pollEvent(event)) {

                        if (event.type == sf::Event::Closed) {
                                win.close();
                        }

                }
                if (sf::Keyboard::isKeyPressed(sf::Keyboard::W)) {
                p.move(0, -32 * dt.asSeconds());
                }
                if (sf::Keyboard::isKeyPressed(sf::Keyboard::A)) {
                p.move(-32 * dt.asSeconds(), 0);
                }
                if (sf::Keyboard::isKeyPressed(sf::Keyboard::S)) {
                p.move(0, 32 * dt.asSeconds());
                }
                if (sf::Keyboard::isKeyPressed(sf::Keyboard::D)) {
                p.move(32 * dt.asSeconds(), 0);
                }

                win.clear();
                win.draw(p);
                win.display();
        }
        return 0;
}
 

As I say this results in choppy movement. By that, I mean that roughly once or twice a second the player stutters and the movement is not completely smooth or continuous. I've looked at a lot of questions regarding this same problem already, but most were caused by problems not present in my code, and otherwise I couldn't find a solution.

Any help would be appreciated thanks.
Title: Re: Choppy Movement With DeltaTime
Post by: Hapax on April 04, 2017, 03:35:47 am
The first thing to try would be to "fix your timestep (http://gafferongames.com/game-physics/fix-your-timestep/)". That isn't to just use a fixed timestep but rather a fixed timestep in a flexible way.

If you are still having "choppy" movement after implementing the "free the physics" section, you may need to implement the more complicated "the final touch" section.

If you are interested in using a small library that implements that solution, you can try Kairos' Timestep (https://github.com/Hapaxia/Kairos/wiki/Timestep).
This Kairos Timestep Example (https://github.com/Hapaxia/Kairos/blob/master/examples/timeStepExample.cpp) shows the usage of Kairos to implement The Final Touch section in the above artice.