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

Author Topic: [SOLVED] Strange sf::Clock issue with SFML 2.1  (Read 4693 times)

0 Members and 2 Guests are viewing this topic.

DashWave

  • Newbie
  • *
  • Posts: 9
    • View Profile
[SOLVED] Strange sf::Clock issue with SFML 2.1
« on: December 13, 2013, 02:55:42 pm »
Hey guys,

I've always used sf::Clock to get the delta time between frames to allow for frame-independent movement. On a new project of mine, however, I have encountered some odd behaviour:

        sf::Clock frameClock;
        double timeLast = 0.1;
        while (window.isOpen())
        {
                //This code works correctly.
                const double timeNow = frameClock.getElapsedTime().asMilliseconds() / 16.66666667;
                const double delta = timeNow - timeLast;
                timeLast = timeNow;
 

        sf::Clock frameClock;
        while (window.isOpen())
        {
                //This code works incorrectly; "delta" is all over the place and my sprites move at visibly different
                //speeds at different times.
                const double delta = frameClock.restart().asMilliseconds() / 16.66666667;
 

This is very strange considering I've used the latter method in other projects where it worked correctly. The only difference between this project and the others is, in the others, frameClock is a member of a class.

I looked at the source of sf::Clock on git and it appears to be doing exactly what the former method does. I tried to debug using VS2012 but I don't have pdb files for SFML and I'm unsure as to how to acquire them although I presume one has to compile from source.

For my entire main.cpp, see http://pastebin.com/xK46pjyC.
« Last Edit: December 13, 2013, 08:15:39 pm by DashWave »

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11034
    • View Profile
    • development blog
    • Email
Re: Strange sf::Clock issue with SFML 2.1
« Reply #1 on: December 13, 2013, 04:51:51 pm »
I've no idea  why you'd be dividing with 16.66666667. It doesn't make sense in the first example nor in the last one. Remove it and it should work fine. ;)
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

DashWave

  • Newbie
  • *
  • Posts: 9
    • View Profile
Re: Strange sf::Clock issue with SFML 2.1
« Reply #2 on: December 13, 2013, 06:22:48 pm »
The reason I use 16.66666667 is to achieve something similar to "pixels per frame if the program was running at 60fps". I'm not absolutely certain as to whether the division is actually correct but using the division allows me to have a much more natural unit for speed - I'm currently using 60.0 * delta for the speed of the player. Without the division, I have to use something like 0.0001 * delta. In any case, removing the division doesn't make any difference except I have to use much smaller constants.

Any other ideas?

Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6287
  • Thor Developer
    • View Profile
    • Bromeon
Re: Strange sf::Clock issue with SFML 2.1
« Reply #3 on: December 13, 2013, 07:09:44 pm »
First, you should use sf::Time objects instead of doubles. Since SFML has a dedicated class to measure times, this does not only make your code easier to read (because of fewer conversions), but also more typesafe.

Second, if you want to scale time, then do it directly before you pass it to the game logic. And use float, you don't need the precision anyway.
sf::Time delta = clock.restart();
gameLogic(scale * delta.asSeconds());
But in order to account for a special metric in your world, I would recommend to scale rather the velocity of your game objects than the time. Time passes at the same rate, no matter how big or small your world units are.
Zloxx II: action platformer
Thor Library: particle systems, animations, dot products, ...
SFML Game Development:

DashWave

  • Newbie
  • *
  • Posts: 9
    • View Profile
Re: Strange sf::Clock issue with SFML 2.1
« Reply #4 on: December 13, 2013, 08:15:05 pm »
Thank you! I should have realised I should be measuring time right before logic.