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