Guys, I'm making a very simple first person shooter as my first 3D game. :cool:
As I started thinking about the multiplayer, I came to the conclusion that capping the frame rate is a really mediocre idea when dealing with lots of PCs.
That's why I decided to make a proper game loop where the game is updated at a fixed timestep but the rendering is done whenever the PC can( variable timestep, they call it).
The main idea is this:
double previous = getCurrentTime();
double lag = 0.0;
while (true)
{
double current = getCurrentTime();
double elapsed = current - previous;
previous = current;
lag += elapsed;
processInput();
while (lag >= MS_PER_UPDATE)
{
update();
lag -= MS_PER_UPDATE;
}
render(lag / MS_PER_UPDATE);
}
But there is a problem.
[attachment=33238:game-loop-timeline.png]
If I lock the game-update rate at 20 fps, then I render 3 to 4 times more than I update the game( talking about my laptop ). And imagine that I render just half a millisecond after an update and there is still time until the next update. Then I need to say: interpolatePosition( lastPos, nextPos, 10%( or 15 or 20%, it doesn't matter ) )
How to use the the next position in the function when I don't know what the next position is. I can predict the next position based on the current movement, but the user will always press a button and do something and then the extrapolation will be wrong, which is what will happen most of the time.
What to do then??
I'm talking just about position here, I won't worry about rotation because in order to interpolate quaternions I need to learn the maths, but I've left it for later.
This is really beyond me and I was wondering if I should leave it for some later time, but I wanted to figure out something really simple that will do for now, because there aren't easy problems with my game anymore, and I can't just leave everything for later.
Can you suggest something really easy to grasp? The easiest way, if there is one at all. <_<
( picture is from this article: http://gameprogrammingpatterns.com/game-loop.html )