But nothing seems to work! I always get stuttering! Even if I interpolate!
And I just doing it wrong or am I crazy?
Here is my current game loop (it is based on the first article), I am using SDL and openGL to create the window for rendering. Double buffering is on and there is no vsync enabled (even if I do enable it, it does not seem to do anything)
//The max amount of time the game logic loop can be updated before forcing the render call
constint MAXUPDATES = 5;
//Time we have updated the game logic loop
int loops = 0;
//The amount of time (in milliseconds) spent updating the game loop in this case 33.33333 milliseconds
double updateTime = 1000.0 / 30.0;
//Used for the interpolation calculation
Get the current time before we start the main game loop
timeGetTime() returns the amount of time since the "system" has started in milliseconds
In this case the system is Windows
double gameClock = timeGetTime();
//Reset the loop count
loops = 0;
The GAME LOOP!
Continue to update the game until the game Clock is greater than the current time ( timeGetTime() )
Until we have hit the max amount of time we are allowed to update before we a re forcing the game to render
( the loops < MAXUPDATES part)
while( (timeGetTime() >= gameClock) && (loops < MAXUPDATES))
//Store the previous player position
player.prevX = player.x;
player.prevY = player.y;
player.x += player.speed;
player.x -= player.speed;
player.y -= player.speed;
player.y += player.speed;
//Add onto the game clock by the update time; Add onto the number of loops
gameClock += updateTime;
//Calculate for interpolation
interpolation = ((timeGetTime() - gameClock) + updateTime) / updateTime;
//Apply the interpolation to the player's render position before actually rendering the game
player.ViewX = player.prevX + interpolation * (player.x - player.prevX);
player.ViewY = player.prevY + interpolation * (player.y - player.prevY);
//Render the game sprites and etc using on the render positions ( player.viewX / player.viewY )
//Flip / swap the buffers to draw everything to the screen
Any help would be greatly appreciated!
My specs :
Dell Studio XPS 1640 -
Windows 7 64 bit
4.0 GB ram DDR2
Core Duo T955 2.66 Ghz
ATI Mobility Radeon HD 4670 [Drivers up to date]
Other information :
Window mode at 800 x 600
2D sprites only
If you own a low-end PC, no matter what you will get struttering.
If you don't, using AntiAliasing may fix your problem, but using it on 3D-scenes or high-poly-scenes will degrade performance. As you're using it for 2D-Scenes it should be acceptable.
I placed all of the code inside my game loop and nothing moves. (I placed LastUpdate = timeGetTime() before I enter the main loop). Also isn't this just variable time step (calculating the delta time and applying it to the game objects)?
Yeah, that's how interpolation works, doesn't it?
Maybe you should increase player.speed to the pixels-per-second the player should move.
LastUpdate isn't used anywhere in that part of the code anyways.
Also notice there is no loop for checking key states.
I don't know why it won't work otherwise, I always used it like this and always worked.
EDIT: Little bugfix, try again?
Also, try with the 'static' keyword and give the results, even just for a try.