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

Author Topic: My sprite stutters when it moves after using a delta  (Read 9041 times)

0 Members and 1 Guest are viewing this topic.

Culinary

  • Newbie
  • *
  • Posts: 9
    • View Profile
My sprite stutters when it moves after using a delta
« 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?
« Last Edit: November 18, 2015, 02:20:32 pm by Culinary »

SpeCter

  • Full Member
  • ***
  • Posts: 151
    • View Profile
Re: My sprite stutters when it moves after using a delta
« Reply #1 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.

« Last Edit: November 18, 2015, 02:51:58 pm by SpeCter »

Grundkurs

  • Jr. Member
  • **
  • Posts: 54
    • View Profile
Re: My sprite stutters when it moves after using a delta
« Reply #2 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.

Culinary

  • Newbie
  • *
  • Posts: 9
    • View Profile
Re: My sprite stutters when it moves after using a delta
« Reply #3 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.
« Last Edit: November 18, 2015, 03:18:11 pm by Culinary »

SpeCter

  • Full Member
  • ***
  • Posts: 151
    • View Profile
Re: My sprite stutters when it moves after using a delta
« Reply #4 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.

Culinary

  • Newbie
  • *
  • Posts: 9
    • View Profile
Re: My sprite stutters when it moves after using a delta
« Reply #5 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.

Culinary

  • Newbie
  • *
  • Posts: 9
    • View Profile
Re: My sprite stutters when it moves after using a delta
« Reply #6 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?
« Last Edit: November 18, 2015, 05:32:54 pm by Culinary »

zsbzsb

  • Hero Member
  • *****
  • Posts: 1409
  • Active Maintainer of CSFML/SFML.NET
    • View Profile
    • My little corner...
    • Email
Re: My sprite stutters when it moves after using a delta
« Reply #7 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/
Motion / MotionNET - Complete video / audio playback for SFML / SFML.NET

NetEXT - An SFML.NET Extension Library based on Thor

Culinary

  • Newbie
  • *
  • Posts: 9
    • View Profile
Re: My sprite stutters when it moves after using a delta
« Reply #8 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?

zsbzsb

  • Hero Member
  • *****
  • Posts: 1409
  • Active Maintainer of CSFML/SFML.NET
    • View Profile
    • My little corner...
    • Email
Re: My sprite stutters when it moves after using a delta
« Reply #9 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.
Motion / MotionNET - Complete video / audio playback for SFML / SFML.NET

NetEXT - An SFML.NET Extension Library based on Thor

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 10815
    • View Profile
    • development blog
    • Email
Re: My sprite stutters when it moves after using a delta
« Reply #10 on: November 18, 2015, 09:38:03 pm »
Split some posts into their own thread, since there were off-topic.
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

Hapax

  • Hero Member
  • *****
  • Posts: 3351
  • My number of posts is shown in hexadecimal.
    • View Profile
    • Links
Re: My sprite stutters when it moves after using a delta
« Reply #11 on: November 19, 2015, 08:50:48 pm »
In the link posted above by zsb (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
Selba Ward -SFML drawables
Cheese Map -Drawable Layered Tile Map
Kairos -Timing Library
Grambol
 *Hapaxia Links*

fubris

  • Newbie
  • *
  • Posts: 6
    • View Profile
    • Email
Re: My sprite stutters when it moves after using a delta
« Reply #12 on: February 20, 2018, 02:52:33 pm »
In the link posted above by zsb (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?

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 10815
    • View Profile
    • development blog
    • Email
Re: My sprite stutters when it moves after using a delta
« Reply #13 on: February 20, 2018, 02:53:47 pm »
Check his signature. ;)
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

Phanoo

  • Full Member
  • ***
  • Posts: 136
    • View Profile
Re: My sprite stutters when it moves after using a delta
« Reply #14 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.
« Last Edit: February 21, 2018, 11:02:46 am by Phanoo »