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

Author Topic: Stutter problem  (Read 9327 times)

0 Members and 1 Guest are viewing this topic.

daktari

  • Newbie
  • *
  • Posts: 13
    • View Profile
    • Email
Stutter problem
« on: January 30, 2016, 09:03:07 pm »
Hello there,

I develop the game named Remnants of Naezith for 1.5 years and I still couldn't fix my stutter.

Here is a minimal working example for my game loop:

https://gist.github.com/naezith/70248aa555ddf87651df

GTX770 on my desktop and ATI 6400M iirc on my laptop, both has the stutter.

Am I doing something wrong? How can I improve this game loop?
« Last Edit: February 01, 2016, 07:57:55 pm by daktari »

victorlevasseur

  • Full Member
  • ***
  • Posts: 206
    • View Profile
Re: Stutter problem
« Reply #1 on: January 30, 2016, 10:03:35 pm »
You need to poll the events in every frames, not only when accumulator > delta.

Hapax

  • Hero Member
  • *****
  • Posts: 3379
  • My number of posts is shown in hexadecimal.
    • View Profile
    • Links
Re: Stutter problem
« Reply #2 on: February 01, 2016, 12:42:39 am »
You shouldn't need "org_pos". Just calculate the interpolated position when setting the position directly or use a temporary and then don't change "pos" to do that  ;)

I have, in an example for my Timestep class, an instance where I use that method - calculate when setting the object's position.
Selba Ward -SFML drawables
Cheese Map -Drawable Layered Tile Map
Kairos -Timing Library
Grambol
 *Hapaxia Links*

daktari

  • Newbie
  • *
  • Posts: 13
    • View Profile
    • Email
Re: Stutter problem
« Reply #3 on: February 01, 2016, 01:37:19 pm »
You shouldn't need "org_pos". Just calculate the interpolated position when setting the position directly or use a temporary and then don't change "pos" to do that  ;)

I have, in an example for my Timestep class, an instance where I use that method - calculate when setting the object's position.

That's interesting. Thanks for sharing.

You need to poll the events in every frames, not only when accumulator > delta.

Is this causing stutter somehow? I don't want to change it because I built my whole buttons and replay/recording system on this implementation.
Edit: It did not help.
« Last Edit: February 01, 2016, 01:44:01 pm by daktari »

Hapax

  • Hero Member
  • *****
  • Posts: 3379
  • My number of posts is shown in hexadecimal.
    • View Profile
    • Links
Re: Stutter problem
« Reply #4 on: February 01, 2016, 05:12:27 pm »
I was just writing, "I just copied and pasted your code** and it works fine without any stutter" when I just noticed that it started to stutter!  :o

I believe your problem is due to using vertical synchronisation. Although I'm not sure exactly, it could be due to the fact that it's synchronising at a low frame rate (missing the refresh so waiting for the next one - half the refresh rate).

I just tried limiting the frame rate* (I tried 30 and 60). Although it's (obviously) not pretty at 30, both 30 and 60 still seems to have 'jumps' so it might be your interpolation calculations.

* Remember that v-sync must be turned off when limiting the frame rate.
** your accumulator variable is not being initialised so I set it to zero at initialisation:
float accumulator = 0.f, delta = 1.0f / 125.0f;

Is this causing stutter somehow?
Probably not but it depends onthe kind of stutter you are experiencing. Not processing events each frame could cause your operating system to whine, which may in turn decide to give you less processing time. Probably not though.
Selba Ward -SFML drawables
Cheese Map -Drawable Layered Tile Map
Kairos -Timing Library
Grambol
 *Hapaxia Links*

daktari

  • Newbie
  • *
  • Posts: 13
    • View Profile
    • Email
Re: Stutter problem
« Reply #5 on: February 01, 2016, 05:42:57 pm »
I was just writing, "I just copied and pasted your code** and it works fine without any stutter" when I just noticed that it started to stutter!  :o

I believe your problem is due to using vertical synchronisation. Although I'm not sure exactly, it could be due to the fact that it's synchronising at a low frame rate (missing the refresh so waiting for the next one - half the refresh rate).

I just tried limiting the frame rate* (I tried 30 and 60). Although it's (obviously) not pretty at 30, both 30 and 60 still seems to have 'jumps' so it might be your interpolation calculations.

* Remember that v-sync must be turned off when limiting the frame rate.
** your accumulator variable is not being initialised so I set it to zero at initialisation:
float accumulator = 0.f, delta = 1.0f / 125.0f;

Is this causing stutter somehow?
Probably not but it depends onthe kind of stutter you are experiencing. Not processing events each frame could cause your operating system to whine, which may in turn decide to give you less processing time. Probably not though.

"* Remember that v-sync must be turned off when limiting the frame rate."

Should I remove all the v-sync option from my game then? I am not limiting the frame rate, I limit the tickrate.

You can simulate the stutter effect by simply click & holding the window for a moment, so line snaps. Just do it. Is there a fix for this, it really bugs me... How can I make my game loop perfect?

About org_pos, I use rect.getPosition() and setPosition inside my update code so I think I have to do it this way.
« Last Edit: February 01, 2016, 06:12:22 pm by daktari »

Hapax

  • Hero Member
  • *****
  • Posts: 3379
  • My number of posts is shown in hexadecimal.
    • View Profile
    • Links
Re: Stutter problem
« Reply #6 on: February 01, 2016, 06:13:29 pm »
There's no need to quote full posts  :P

"* Remember that v-sync must be turned off when limiting the frame rate."
Should I remove all the v-sync option from my game then? I am not limiting the frame rate, I limit the tickrate.
No. I mean the window.setFramerateLimit and window.setVerticalSyncEnabled should not both be true at the same time; that's all.

You can simulate the stutter effect by simply click & holding the window for a moment, so line snaps. Just do it. Is there a fix for this, it really bugs me... How can I make my game loop perfect?
It tends to happen when the operating system is more busy (I found I can cause it by using my browser - on another display) so it could just be missing the vertical sync refresh time.
However, I've found it's doing it with vertical sync and frame limiting both being turned off and I'm not sure why. I've even completely rewritten it using my Timestep class and using a different interpolation calculation and it's still doing it. I'll be looking into this  >:(
Selba Ward -SFML drawables
Cheese Map -Drawable Layered Tile Map
Kairos -Timing Library
Grambol
 *Hapaxia Links*

daktari

  • Newbie
  • *
  • Posts: 13
    • View Profile
    • Email
Re: Stutter problem
« Reply #7 on: February 01, 2016, 06:52:53 pm »
Yeah I haven't setFramerateLimit in my code. But I think of adding one like game will count FPS for couple seconds at launch of the game, if its more than 200 then we can tell that vertical sync is off so I will put setFramerateLimit(200) for example.

Is there a bad effect of setFramerateLimit(200) if vsync is off and player does not get 200 fps at all.

Hapax

  • Hero Member
  • *****
  • Posts: 3379
  • My number of posts is shown in hexadecimal.
    • View Profile
    • Links
Re: Stutter problem
« Reply #8 on: February 01, 2016, 07:06:42 pm »
This stutter thing is weird. Sometimes the code works perfectly (and I mean the smoothest thing that I've ever seen!) but sometimes it stutters. It does look like it's occasionally missing a frame. Outputting the frame time shows that it spikes whenever it stutters. It looks like it could be linked to inaccuracies in the incredibly small frame times.

Is there a bad effect of setFramerateLimit(200) if vsync is off and player does not get 200 fps at all.
SFML's framerate limit is created by sleeping a tiny bit each frame.
« Last Edit: February 01, 2016, 07:16:37 pm by Hapax »
Selba Ward -SFML drawables
Cheese Map -Drawable Layered Tile Map
Kairos -Timing Library
Grambol
 *Hapaxia Links*

daktari

  • Newbie
  • *
  • Posts: 13
    • View Profile
    • Email
Re: Stutter problem
« Reply #9 on: February 01, 2016, 07:42:21 pm »
This stutter thing is weird. Sometimes the code works perfectly (and I mean the smoothest thing that I've ever seen!) but sometimes it stutters. It does look like it's occasionally missing a frame.

I experience the same thing. Sometimes it is like, I run the program, it stutters for a while then it becomes the smoothest thing ever, then it starts stuttering again.

Tried million stuff, can't fix it.

Mario

  • SFML Team
  • Hero Member
  • *****
  • Posts: 879
    • View Profile
Re: Stutter problem
« Reply #10 on: February 01, 2016, 08:06:43 pm »
Might sound stupid, but what happens if you're sleeping for 1 millisecond at the end of your update loop? Does that change anything?

Also which graphics card are you running and what's your actual FPS? Do FPS change when the stuttering happens or is it stable? How about the number of update per second in these situations?

daktari

  • Newbie
  • *
  • Posts: 13
    • View Profile
    • Email
Re: Stutter problem
« Reply #11 on: February 01, 2016, 08:47:15 pm »
This one runs so smooth but that's probably because it renders 6k times a second.
#include <SFML/Graphics.hpp>

int main() {
        sf::RenderWindow window(sf::VideoMode(1280, 720), "Stutter");
        sf::Event event;
        sf::Clock clock;
        float accumulator = 0.0f, delta = 1.0f / 125.0f;

        //window.setVerticalSyncEnabled(true);

        sf::RectangleShape rect(sf::Vector2f(5.0f, 720.0f));
        float pos = 0.0f;

        while(window.isOpen()) {
                accumulator += clock.restart().asSeconds();

                //while(accumulator > delta) {
                        accumulator -= delta;

                        // Poll Events
                        while(window.pollEvent(event)) if(event.type == sf::Event::Closed) window.close();

                        // Update
                        if((pos += 20.0f*delta) > 1280.0f) { pos = 0.0f; }
                //}

                // Set position to interpolated position
                rect.setPosition(pos, 0.0f);

                // Render
                window.clear();
                window.draw(rect);
                window.display();
        }

        return 0;
}


Might sound stupid, but what happens if you're sleeping for 1 millisecond at the end of your update loop? Does that change anything?

Also which graphics card are you running and what's your actual FPS? Do FPS change when the stuttering happens or is it stable? How about the number of update per second in these situations?

Wow... Having
sf::sleep(sf::milliseconds(1));
after the .display() helps to make it smooth like I open the program, line is shaky for like 0.3-0.4 seconds, then it becomes smooth and keeps being smooth. Yet it shakes when I shake my mouse :D (1000 Hz polling rate mouse and I don't have this issue in CS:GO)

Nvidia GTX 770 and 144 Hz monitor I have so I can see the smallest difference.

Fraps says 144 FPS, when I shake the mouse, line shakes and FPS becomes 143.



Edit: It is so weird that I removed sleep(1) and it is still smooth now. Sometimes it stutters, sometimes it does not. Very confusing.
« Last Edit: February 01, 2016, 08:57:52 pm by daktari »

daktari

  • Newbie
  • *
  • Posts: 13
    • View Profile
    • Email
Re: Stutter problem
« Reply #12 on: February 01, 2016, 10:34:51 pm »
A friend reports me that setting my game's priority to high or real-time in task manager makes it smooth as baby's ass.

daktari

  • Newbie
  • *
  • Posts: 13
    • View Profile
    • Email
Re: Stutter problem
« Reply #13 on: February 03, 2016, 09:10:16 pm »
Problem is still there tho.

Hapax

  • Hero Member
  • *****
  • Posts: 3379
  • My number of posts is shown in hexadecimal.
    • View Profile
    • Links
Re: Stutter problem
« Reply #14 on: February 03, 2016, 10:20:29 pm »
Might sound stupid, but what happens if you're sleeping for 1 millisecond at the end of your update loop? Does that change anything?
This is an odd suggestion yet I found that the problem seriously reduced when I added a line of code that changed the window's title (I was using it to output information) so your suggestion isn't as odd as it sounds.

Daktari, did you try adding the slight delay that was suggested?
Selba Ward -SFML drawables
Cheese Map -Drawable Layered Tile Map
Kairos -Timing Library
Grambol
 *Hapaxia Links*