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

Author Topic: Few questions about using fix timestep for physics  (Read 1267 times)

0 Members and 1 Guest are viewing this topic.

Sythical

  • Newbie
  • *
  • Posts: 9
    • View Profile
Few questions about using fix timestep for physics
« on: July 13, 2013, 11:16:14 am »
Hello, I've been trying to implement the fix time-step technique and I'm having trouble understanding a couple of thing. I've listed the two issues separately so that they make more sense.

i) To interpolate between the old and the new state, I'm using two variables called old_pos_x and new_pos_x. Originally, I would copy new_pos_x to old_pos_x after rendering each time. This makes more sense to me since I want to interpolate between the circle on the screen and the one I'll be drawing next. However, this makes the animation stutter if I don't update the physics at 60 or 120 hertz (multiples of my refresh rate).

When I moved the copy statement inside the physics loop, the stutter goes away and I have no idea why. I would have thought that this will make the game stutter a lot more, especially when the the frame rate is lower than the update rate. Why does this work?

ii) I added Sleep(5) after the render bit in order to decrease the FPS to about 200 so that I can see how it works at low rates. The animation becomes very jumpy and not smooth at all. I understand that Sleep() isn't accurate but surely that shouldn't matter as long as the frame rate is above 60 FPS because of the monitor's refresh rate.

I've written a smaller version of the program which replicates the issue(s) so that it's easier to read. It's just a circle that moves horizontally. The code is still a bit long, sorry about that.

unsigned screen_width = 1200, screen_height = 800;
int old_pos_x = 100, new_pos_x = 100, velocity_x = 0;
LARGE_INTEGER counter, frequency;

LONGLONG
    accumulator = 0, accumulator_max, physics_delta,
    time_difference, new_time, previous_time;

sf::RenderWindow window(sf::VideoMode(screen_width, screen_height), "Example", sf::Style::Titlebar | sf::Style::Close);

sf::CircleShape graphics(15.f);
graphics.setFillColor(sf::Color(255, 255, 255));

QueryPerformanceFrequency(&frequency);
physics_delta = frequency.QuadPart / 50;
accumulator_max = physics_delta * 5;

QueryPerformanceCounter(&counter);
previous_time = counter.QuadPart;

while(window.isOpen()) {
    QueryPerformanceCounter(&counter);

    new_time = counter.QuadPart;
    time_difference = new_time - previous_time;
    previous_time = new_time;
    accumulator += time_difference;

    if(accumulator > accumulator_max) {
        accumulator = accumulator_max;
    }

    while(accumulator >= physics_delta) {
        if(new_pos_x > 1000) velocity_x = -8;
        else if(new_pos_x < 200) velocity_x = 8;

        // if I copy the new state (position) to the old state here instead the animation is much more smooth
        // but I don't understand why this is the case, I would have thought that this is more likely to cause stutter
        // especially if the frame rate is lower than the game update rate

        // old_pos_x = new_pos_x; // copy the current state to the old one
        new_pos_x += velocity_x; // update the current state

        accumulator -= physics_delta;
    }

    double interpolation_val = accumulator / (double) physics_delta;
    graphics.setPosition(new_pos_x * interpolation_val + old_pos_x * (1 - interpolation_val), 400.f);

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

    // this is where I was originally copying the new state (position) to the old state
    // this makes more sense to be because I want to interpolate between the last drawn circle
    // and the current circle

    old_pos_x = new_pos_x;

    // Sleep(5);
}

I'd be very grateful if someone can have a read through the code. Thank you  :)



eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11016
    • View Profile
    • development blog
    • Email
Re: Few questions about using fix timestep for physics
« Reply #1 on: July 15, 2013, 12:13:48 pm »
To be honest, I've no idea why such a change does actually have effect on the stuttering.

First outside the loop, the inside the loop. The effect is even better visible when not seen through the recording.


The only thing that actually changes is that the physics loop gets slowed down a bit, due to the fact that it has to constantly copy a long long around, other than that there's not much different in placing it inside or outside of the loop.

Here's a more complete and better formatted code:
#include <SFML/Graphics.hpp>
#include <SFML/System.hpp>
#include <windows.h>

int main()
{
        unsigned screen_width = 1200, screen_height = 800;
        int old_pos_x = 100, new_pos_x = 100, velocity_x = 0;
        LARGE_INTEGER counter, frequency;

        sf::Int64 accumulator = 0, accumulator_max, physics_delta, time_difference, new_time, previous_time;

        sf::RenderWindow window(sf::VideoMode(screen_width, screen_height), "Example", sf::Style::Titlebar | sf::Style::Close);

        sf::CircleShape graphics(15.f);
        graphics.setFillColor(sf::Color(255, 255, 255));

        QueryPerformanceFrequency(&frequency);
        physics_delta = frequency.QuadPart / 50;
        accumulator_max = physics_delta * 5;

        QueryPerformanceCounter(&counter);
        previous_time = counter.QuadPart;

        while(window.isOpen())
        {
                sf::Event event;
                while(window.pollEvent(event))
                {
                        if(event.type == sf::Event::Closed)
                                window.close();
                }

                QueryPerformanceCounter(&counter);

                new_time = counter.QuadPart;
                time_difference = new_time - previous_time;
                previous_time = new_time;
                accumulator += time_difference;

                if(accumulator > accumulator_max)
                {
                        accumulator = accumulator_max;
                }

                while(accumulator >= physics_delta)
                {
                        if(new_pos_x > 1000)
                                velocity_x = -8;
                        else if(new_pos_x < 200)
                                velocity_x = 8;

                        // if I copy the new state (position) to the old state here instead the animation is much more smooth
                        // but I don't understand why this is the case, I would have thought that this is more likely to cause stutter
                        // especially if the frame rate is lower than the game update rate

                        old_pos_x = new_pos_x; // copy the current state to the old one
                        new_pos_x += velocity_x; // update the current state

                        accumulator -= physics_delta;
                }

                // this is where I was originally copying the new state (position) to the old state
                // this makes more sense to be because I want to interpolate between the last drawn circle
                // and the current circle

                //old_pos_x = new_pos_x; // copy the current state to the old one

                double interpolation_val = accumulator / static_cast<double>(physics_delta);
                graphics.setPosition(new_pos_x * interpolation_val + old_pos_x * (1 - interpolation_val), 400.f);

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

                // Sleep(5);
        }
}
 

I wonder through, why are you using Windows specific types (e.g. LONGLONG or LONG_INTEGER) instead of the ones SFML provides (e.g. sf::Int64)?
And why would you go and use the window specific QueryPerformanceTimer yourself, when you could just use sf::Clock which chooses the QPT on Windows automatically? ???
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

 

anything