Hello, I'm trying to implement the game loop where the physics is independent from rendering but my animation isn't as smooth as I would like and seems to be jumpy. Here is my code:
On line 44, I've added a rest because I want to see how the code behaves when a lot of rendering is involved. If I remove it, the animation is smooth but I'm not happy with that since that's just hoping for the best. On my computer, the frame rate is around 125 and the animation is still jumpy. I've been trying to fix this for a week and have had no luck so I'd be very grateful if someone can help me. Thank you!
Edit: I added the following code to work out the actual velocity (pixels per second) of the ball each time the ball is rendered and surprisingly it's not constant so I'm guessing that's the issue. I'm not sure why it's not constant.
alpha = accumulator / static_cast<double>(step_duration); render_pos_x = old_pos_x + (new_pos_x - old_pos_x) * alpha; cout << (render_pos_x - old_render_pos) / delta_time << endl; old_render_pos = render_pos_x;
I don't know the answer to your question... but instead I converted your example to the interpolation algorithm I tend to use. It seems to me that my way looks smoother, but that might be my brain lying to me. I didn't feel like setting up a double blind experiment to test .
Also... it looks a lot smoother if you use vsync, at least on my computer. If you're on Linux, uncomment the al_wait_for_vsync line. If you're on Windows, uncomment al_set_new_display_option(ALLEGRO_VSYNC...) line. The fact that's its different is probably an Allegro bug.
Here's your code (with added functions to make it run. I also slowed down the ball a bit.):
And here's mine:
Hey SiegeLord, thank you for response. I tried both programs and they seem to be more or less identical on my computer. Vsync does make the animation a lot smoother so I'll start using that. I've read that vsync doesn't work in windowed mode because updates are managed by the OS and the game itself has no control over when the screen updates. I read this when I was trying to figure out how to enable vsync in SDL.
I've been searching for an article which talks about frame rates and refresh rates with game development in mind. In the games I normally play (mainly WoW), the frame rate usually varies a lot but the gameplay is still butter smooth and if I lock the fps to something that doesn't match my monitor's refresh rate, so locked at 70 or 50, the gameplay is still smooth. In my game however, variable frame rate and not matching the refresh rate makes a huge difference. Can someone please briefly explain some of the things that I need to keep in mind?
Thank you again
I don't see the point in drawing more often than the monitor refresh rate. Interpolation allows you to decouple your logic rate from the monitor refresh rate, while keeping things smooth. I have no trouble in enabling vsync in windowed applications on Linux or Windows.
I don't know what technique WoW uses to handle things (it's almost certainly is not delta timing though, given that it works even when FPS drops to single digits).