SFML community forums

Help => Graphics => Topic started by: thisdyingsoul on October 01, 2013, 08:15:54 am

Title: Smooth movement.
Post by: thisdyingsoul on October 01, 2013, 08:15:54 am
I see a lot of this topics and still doesn't work for me:
    sf::RenderWindow window(sf::VideoMode(640, 480), "My window");
        window.setFramerateLimit(60);
       
        sf::Texture tex;
        tex.loadFromFile("bomberman.png");
        sf::Sprite sprite;
        sprite.setTexture( tex );      

        auto move = 100.f;
        auto dt   = 0.f;
        sf::Clock clock;
    while (window.isOpen()){        
        sf::Event event;
        while (window.pollEvent(event)){    
            if (event.type == sf::Event::Closed)
                window.close();
        }  
        sprite.move( move * dt, 0 );

        window.clear();        
        window.draw( sprite );
        window.display();

        auto& pos = sprite.getPosition();
        if((pos.x > 640) || (pos.x < 0)) move = -move;

        dt = clock.restart().asSeconds();                    
    }
 

The sprite speed up randomly (small but perceptible jumps).
I read about here
http://gafferongames.com/game-physics/fix-your-timestep/  (http://gafferongames.com/game-physics/fix-your-timestep/) and here http://gamedev.tutsplus.com/tutorials/implementation/how-to-create-a-custom-2d-physics-engine-the-core-engine/ (http://gamedev.tutsplus.com/tutorials/implementation/how-to-create-a-custom-2d-physics-engine-the-core-engine/).
Implemented the timestepping and the interpolation rendering.
Implemented everything on sdl2.
Tried also on different computers.
And run the sfml examples.
All have the same problem.
And in all cases, without frame limit or vsync (or with framelimit >=75 (+-) ) all runs smooth.
At 60fps or less the problem continues.

Thank you for the patience!
Title: Re: Smooth movement.
Post by: BaneTrapper on October 01, 2013, 12:53:14 pm
Floats are inaccurate when so low.
Try following:
auto move = 100.f / 1000;
dt = clock.restart().asMiliseconds();
Title: Re: Smooth movement.
Post by: thisdyingsoul on October 02, 2013, 03:50:34 am
Same problem.
Only render more than 60fps make it look smooth.
If I update the movement at 30fps and render at eg. 100fps, its looks ok. Which is the reverse of what I read o.O
Title: Re: Smooth movement.
Post by: Izman on October 02, 2013, 10:00:20 am
Same problem.
Only render more than 60fps make it look smooth.
If I update the movement at 30fps and render at eg. 100fps, its looks ok. Which is the reverse of what I read o.O

No, that should be correct, assuming you're using frame interpolation.

I've found that, especially at a strictly 2D level, there's a big notice in quality from 30fps to 60fps.

For example, http://boallen.com/fps-compare.html
Title: Re: Smooth movement.
Post by: thisdyingsoul on October 02, 2013, 10:28:08 am
hm, right, thanks ^^
But the problem is really that small jumps that occurs when render at 60 fps.
It get better at 75, and are almost gone at 100fps (and vanish with more).
The delta time is very stable so i was unable to detect where this jumps come from.
Title: Re: Smooth movement.
Post by: BaneTrapper on October 02, 2013, 01:36:13 pm
The issue is obviously with float type for position & how is drawing done.

Do following.
Add two floats.
Code: [Select]
float posX, posY;
Then for movement update posX,posY with coordinates.
Code: [Select]
posX += (move * dt);
posY += 0;
Before drawing, update sprite position from the posX, posY
Code: [Select]
sprite.setPosition(static_cast<int>(posX), static_cast<int>(posY);

Tada! smooth movement
Title: Re: Smooth movement.
Post by: thisdyingsoul on October 02, 2013, 10:08:10 pm
Unfortunately doesn't work. ;(
I also have the same code on SDL and there it uses your solution since SDL_Rect positions are ints, but it shows the same problem.
Thank you anyway ^^
Im starting to think that my eyes see more frames that it should ;)
Title: Re: Smooth movement.
Post by: BaneTrapper on October 02, 2013, 10:48:18 pm
Unfortunately doesn't work. ;(
I also have the same code on SDL and there it uses your solution since SDL_Rect positions are ints, but it shows the same problem.
Thank you anyway ^^
Im starting to think that my eyes see more frames that it should ;)
Pass me exe file, i need/want to see that.

I used your code, just made the adjustments i suggested and it was fine, perfect movement.
Title: Re: Smooth movement.
Post by: thisdyingsoul on October 02, 2013, 11:12:52 pm
Yes, i was thinking that may be my pc, but i tested on another and see the same..
https://dl.dropboxusercontent.com/u/8711710/base_testing.rar
2 images, 2 .exe (with and without frame limit for comparison), and source. no dlls.
Title: Re: Smooth movement.
Post by: BaneTrapper on October 03, 2013, 01:32:13 pm
I do not notice the issue, somehow turning Vsync on makes it look even smoother.
Title: Re: Smooth movement.
Post by: thisdyingsoul on October 03, 2013, 11:30:58 pm
Ok, seems that me and my brother have better frame perception o.O
I send to some friends and they also didn't see the problem.
This is awkward. Thank you anyway ^^
Title: Re: Smooth movement.
Post by: BaneTrapper on October 07, 2013, 09:33:08 pm
eXpl0it3r
Don't mix real-time inputs and event handling.
Read the linked tutorials (again), especially pay attention to when you should use what and where you should use what. :)

Read true tutorials, you will figure you why the movement is jaggy.
Its pretty highlighted in certain parts you wont miss it!