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

Author Topic: Gameloops and timing, smooth rotation  (Read 6391 times)

0 Members and 1 Guest are viewing this topic.

zepto

  • Newbie
  • *
  • Posts: 5
    • View Profile
Gameloops and timing, smooth rotation
« on: November 25, 2011, 11:09:14 pm »
Goal
Move or rotate a sprite smoothly on the screen with SFML2.

Implementation
This code should rotate the SFML logo sfml-small.png.
Code: [Select]
#include <SFML/Graphics.hpp>

int main()
{
    sf::RenderWindow window(sf::VideoMode(800, 600), "Gameloops and timing");
    window.EnableVerticalSync(true);

    sf::Texture image;
    if (!image.LoadFromFile("sfml-small.png")) {
        return EXIT_FAILURE;
    }
    sf::Sprite sprite(image);
    sprite.Move(window.GetWidth() / 2.0, window.GetHeight() / 2.0);

    const sf::Uint32 FPS = 50;
    const sf::Uint32 SKIP_TICKS = 1000 / FPS;
    sf::Uint32 sleep_time = 0;
    sf::Clock clock;
    sf::Uint32 next_game_tick = clock.GetElapsedTime();

    while (window.IsOpened())
    {
        sf::Event event;
        while (window.PollEvent(event))
        {
            if (event.Type == sf::Event::Closed) {
                window.Close();
            }
        }

        sprite.Rotate(90.f / FPS);

        window.Clear();
        window.Draw(sprite);
        window.Display();

        next_game_tick += SKIP_TICKS;
        sleep_time = next_game_tick - clock.GetElapsedTime();
        if (sleep_time >= 0) {
            sf::Sleep(sleep_time);
        } else {
            // running behind
        }
    }

    return EXIT_SUCCESS;
}


Observations
  • The sprite sometimes randomly jumps back when it should rotate continiously with constant angular velocity
  • The problem is independent of the programming language or operating system
  • I noticed similar issues in a lot of games and physics simulations
Help
What am I doing wrong? Is it inaccurate timing, or is vsync not working? I have no clue what's going on.

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Gameloops and timing, smooth rotation
« Reply #1 on: November 25, 2011, 11:13:23 pm »
Quote
The problem is independent of the programming language

So please give it in C++ so that more people (including me) can test it ;)
Laurent Gomila - SFML developer

zepto

  • Newbie
  • *
  • Posts: 5
    • View Profile
Gameloops and timing, smooth rotation
« Reply #2 on: November 27, 2011, 11:27:42 pm »
Ok, I implemented the game loop FPS dependent on Constant Game Speed from the blog article deWiTTERS Game Loop in C++ with SFML2.

It has the same issues.

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Gameloops and timing, smooth rotation
« Reply #3 on: November 28, 2011, 07:35:26 am »
Your code doesn't work, on the second iteration sleep_time is huge (negative but its type is unsigned) so it sleeps forever.
Laurent Gomila - SFML developer

zepto

  • Newbie
  • *
  • Posts: 5
    • View Profile
Gameloops and timing, smooth rotation
« Reply #4 on: November 28, 2011, 04:17:53 pm »
Oops my bad, sleep_time should be an integer: new code

From the output it looks to me like everything is working as expected.
Every (40 +- 1) ms the sprite is rotated by 3.6 degrees, I don't understand why the rotation looks so choppy.
Code: [Select]
0, 1
3.6, 40
7.2, 80
10.8, 120
14.4, 161
18, 201
21.6, 241
25.2, 281
28.8, 320
32.4, 361
36, 401
39.6, 441
43.2, 481
46.8, 521
50.4, 561
54, 601
57.6, 640
61.2, 681
64.8, 720
68.4, 760
72, 801
75.6, 841
79.2, 880
82.8, 921
86.4, 961
90, 1000

Atomical

  • Newbie
  • *
  • Posts: 12
    • View Profile
Gameloops and timing, smooth rotation
« Reply #5 on: November 28, 2011, 07:05:44 pm »
try the built in window.SetFrameratelimit(FPS)

zepto

  • Newbie
  • *
  • Posts: 5
    • View Profile
Gameloops and timing, smooth rotation
« Reply #6 on: December 13, 2011, 05:06:42 pm »
Quote from: "Atomical"
try the built in window.SetFrameratelimit(FPS)


No this didn't work, also the gameloop is already limited by the FPS.
Any other ideas?

Anata

  • Jr. Member
  • **
  • Posts: 77
    • View Profile
Gameloops and timing, smooth rotation
« Reply #7 on: December 13, 2011, 05:28:36 pm »
for me, it's limited by EnableVerticalSync and not by FPS

Naufr4g0

  • Full Member
  • ***
  • Posts: 112
    • View Profile
Gameloops and timing, smooth rotation
« Reply #8 on: December 13, 2011, 08:27:51 pm »
Let VSync decide the FPS of the game.
Enable window vsync and use GetFrameTime function to obtain smooth movements and rotations. :) It works!

 

anything