SFML community forums

Help => Graphics => Topic started by: Culinary on November 18, 2015, 02:17:03 pm

Title: My sprite stutters when it moves after using a delta
Post by: Culinary on November 18, 2015, 02:17:03 pm
I'm simply trying to have it so my sprite moves smoothly across the screen.
It stutters. I tried reading the FAQ about this but I'm still trying to process why it's stuttering.

I also tried limiting the windows to 60FPS and it still stutters, while also disabling/enabling vertical sync.
They all seem to produce the same stuttering result.

#include <SFML/Graphics.hpp>
#include <iostream>

using namespace std;

int main()
{
    sf::RenderWindow window(sf::VideoMode(640, 480), "SFML works!");
        window.setFramerateLimit(60);

    sf::CircleShape shape(10.f);
    shape.setFillColor(sf::Color::Green);

        float x = 0.0f;
        float y = 0.0f;

        float hspeed = 50.0f;
        float vspeed = 50.0f;

        sf::Clock clock;

        sf::Texture texture;
        if (!texture.loadFromFile("test_sprite.png"))
        {
                return EXIT_FAILURE;
        }

        sf::Sprite sprite;
        sprite.setTexture(texture);

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

                float delta = clock.restart().asSeconds();
                //cout << "delta: " << delta << endl;

                x+= hspeed * delta;
                y+= vspeed * delta;

                sprite.setPosition(x, y);

        window.clear();
        window.draw(sprite);
        window.display();
    }

    return 0;
}
 

Also on a side note, why is there a value greater than 0 being outputted by this line:
Code: [Select]
delta = clock.restart().asSeconds();

If you restart the clock, shouldn't it be 0? When I cout the value it's usually 0.001-something. But didn't we restart the clock making it 0?
Title: Re: My sprite stutters when it moves after using a delta
Post by: SpeCter on November 18, 2015, 02:46:31 pm
Quote
Also on a side note, why is there a value greater than 0 being outputted by this line:

delta = clock.restart().asSeconds();

If you restart the clock, shouldn't it be 0? When I cout the value it's usually 0.001-something. But didn't we restart the clock making it 0?

This function puts the time counter back to zero. It also returns the time elapsed since the clock was started.

This is straight from the documentation.

Title: Re: My sprite stutters when it moves after using a delta
Post by: Grundkurs on November 18, 2015, 02:47:48 pm
does it stutter in fullscreen-mode too?
you can enable it with sf::RenderWindow window(sf::VideoMode(640, 480), "SFML works!", sf::Style::Fullscreen);

if you check it out, add if(sf::Keyboard::isKeyPressed(sf::Keyboard::Escape)) window.close(); to your update-function, otherwise you're gonna have trouble closing the fullscreen-window.
Title: Re: My sprite stutters when it moves after using a delta
Post by: Culinary on November 18, 2015, 03:14:13 pm
Oh god, setting it to fullscreen made all of my windows go to my right monitor, and disabled aero theme.
I also didn't even see my game I just saw a hugely zoomed in version of my desktop.

Force closed it and nothing responded for around 10 seconds.

Don't want to do that again.

So yeah still not sure what to do about the stuttering.
Title: Re: My sprite stutters when it moves after using a delta
Post by: SpeCter on November 18, 2015, 04:00:44 pm
I compiled your code and can't notice any stuttering, neither with vsync enabled or with setting a frameratelimit.
The only thing I changed was moving the circleshape around, but that should not make any difference.
Title: Re: My sprite stutters when it moves after using a delta
Post by: Culinary on November 18, 2015, 04:44:26 pm
I honestly don't know what to say, it's definitely stuttering on my screen. I can see it wiggle a bit as it moves.
Title: Re: My sprite stutters when it moves after using a delta
Post by: Culinary on November 18, 2015, 05:27:09 pm
Wait but the official SFML FAQ says to use sf::Clock.
But yeah I'm not sure if it would make a huge difference or not.

Also you're saying to enable both vsync and set the frame limit at the same time? Is this okay to do?
Will it decrease CPU performance on my game if I enable both or will it be fine?
Title: Re: My sprite stutters when it moves after using a delta
Post by: zsbzsb on November 18, 2015, 05:30:54 pm
Wait but the official SFML FAQ says to use sf::Clock.
But yeah I'm not sure if it would make a huge difference or not.

This is my point exactly to mkalex777, when he posts these random tidbits of 'information' all it does is confuse people and then they don't know what to do and it gets everyone else nowhere.

Culinary, just continue doing what you are doing (there is nothing wrong with sf::Clock as one person has claimed) and read http://gafferongames.com/game-physics/fix-your-timestep/
Title: Re: My sprite stutters when it moves after using a delta
Post by: Culinary on November 18, 2015, 05:33:32 pm
Alright what about the last question in my last post about vsync and setting the frame limit?
Should I just enable both or just one?
Title: Re: My sprite stutters when it moves after using a delta
Post by: zsbzsb on November 18, 2015, 05:37:25 pm
Alright what about the last question in my last post about vsync and setting the frame limit?
Should I just enable both or just one?

Its explained in the tutorials, but you should choose one or the other. Using both at the same time could cause timing issues. Also see the FAQ on the subject (http://www.sfml-dev.org/faq.php#graphics-vsync-framelimit).
Title: Re: My sprite stutters when it moves after using a delta
Post by: eXpl0it3r on November 18, 2015, 09:38:03 pm
Split some posts into their own thread (http://en.sfml-dev.org/forums/index.php?topic=19375.0), since there were off-topic.
Title: Re: My sprite stutters when it moves after using a delta
Post by: Hapax on November 19, 2015, 08:50:48 pm
In the link posted above by zsb (Fix Your Timestep (http://gafferongames.com/game-physics/fix-your-timestep/)), the stuttering you describe is only (finally) fixed in the very final step ("The final touch"), which is undoubtedly the most confusing. I'd recommend working through the entire article though - understanding each part - and if, when getting to that final part, you have trouble understanding it, have a break and come back later. It helps. If you understand it all first time, though, that's awesome!

I actually created a timing library with classes that help with this sort of thing. People are probably a bit bored of my links to it though  :D
Title: Re: My sprite stutters when it moves after using a delta
Post by: fubris on February 20, 2018, 02:52:33 pm
In the link posted above by zsb (Fix Your Timestep (http://gafferongames.com/game-physics/fix-your-timestep/)), the stuttering you describe is only (finally) fixed in the very final step ("The final touch"), which is undoubtedly the most confusing. I'd recommend working through the entire article though - understanding each part - and if, when getting to that final part, you have trouble understanding it, have a break and come back later. It helps. If you understand it all first time, though, that's awesome!

I actually created a timing library with classes that help with this sort of thing. People are probably a bit bored of my links to it though  :D

I would really appreciate those links to those libraries... please?
Title: Re: My sprite stutters when it moves after using a delta
Post by: eXpl0it3r on February 20, 2018, 02:53:47 pm
Check his signature. ;)
Title: Re: My sprite stutters when it moves after using a delta
Post by: Phanoo on February 21, 2018, 11:00:00 am
I've always noticed stutterings even without using any advanced timestep, just a plain loop v-synced doing +1 to the X position of a sprite.
I'm not sure it's even related to SFML, seems it depends on the running applications (on my Windows session) or other strange factors... In fullscreen that doesn't happen, everything is smooth.
Title: Re: My sprite stutters when it moves after using a delta
Post by: eXpl0it3r on February 21, 2018, 12:24:55 pm
Every application has some degree of stuttering. It's usually caused by stalled updates for one frame due to some other application using up GPU or CPU resources.
It's also why in fullscreen mode you usually get less stuttering because there you more or less have exclusive GPU time for that monitor.

If you use a fixed timestep the movement of the object isn't dependent on the frame rendering any more, but it just keeps going and whenever there are resources available it will be drawn to the screen.
Title: Re: My sprite stutters when it moves after using a delta
Post by: Hapax on February 26, 2018, 01:11:12 am
I actually created a timing library with classes that help with this sort of thing. People are probably a bit bored of my links to it though  :D

I would really appreciate those links to those libraries... please?
Just in case I decide to change the links in my signature again, my timing library called Kairos is available on github here:
https://github.com/Hapaxia/Kairos/wiki