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

Author Topic: Clock::getElapsedTime() with asMilliseconds() causing weird behaviour  (Read 6092 times)

0 Members and 1 Guest are viewing this topic.

hoenchioma

  • Newbie
  • *
  • Posts: 4
    • View Profile
When using the Clock::getElapsedTime() or restart() I noticed something weird.

        RenderWindow window(VideoMode(700, 700), "Hello SFML");
        Event event;
        Clock clock;
        Clock clock2;
        Clock c3;
       
        RectangleShape box(Vector2f(20, 20));

        //window.setFramerateLimit(30);

        while (window.isOpen())
        {      
                while (window.pollEvent(event))
                {
                        if (event.type == Event::EventType::Closed)
                        {
                                window.close();
                        }
                }
                box.move(0, 0.5 * clock.restart().asMilliseconds());
                clock.restart();

                if (box.getPosition().y > 700)
                {
                        cerr << clock2.getElapsedTime().asSeconds() << endl;
                        window.close();
                }

                window.clear();

                window.draw(box);

                window.display();
        }
 

The code seems simple enough it outputs how much the box takes to move across the screen. Moving a box to down. I was trying to implement frame rate independent movement. This produces weird jagged movement. And the measured time varying at each run (3-6 seconds). So I decided to measure the elapsed time to see whats going on.
        box.move(0, 0.5 * clock.getElapsedTime().asMilliseconds());
        cerr << clock.restart().asMilliseconds() << endl;
 
Suddenly the movement was fixed and the time taken seemed to average to around 1.76 seconds.
This was so at any framerate I set (as it should be). But as soon as i commented out the second line the same happened again.
What's even more weird is that this doesn't seem to happen when I use asMicroseconds().
Is this me or what could be causing this. I'm running this on a Windows 10 64 bit machine (developing for 32 bit machines) if that helps.

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11032
    • View Profile
    • development blog
    • Email
Re: Clock::getElapsedTime() with asMilliseconds() causing weird behaviour
« Reply #1 on: September 10, 2018, 03:46:34 pm »
If you have a a very high framerate, then asMilliseconds will return 0 due to rounding errors, thus you may end up with odd movement. Introducing a std::cerr adds more time to each frame, making asMilliseconds return useful values.

I suggest using asSeconds which provides you a float and won't be 0, even for very short frame time.
Additionally, you should really setFramerateLimit your FPS, as you're burning CPU and GPU time for nothing. ;)
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

hoenchioma

  • Newbie
  • *
  • Posts: 4
    • View Profile
Re: Clock::getElapsedTime() with asMilliseconds() causing weird behaviour
« Reply #2 on: October 24, 2018, 06:09:55 am »
Thanks a lot. That seemed to solve the problem.  ;D