Thursday, October 18, 2018

java - Android Game-Loop (rendering & logic removed, but still 'skipping frames')


Overview


Guys really hoping for some help here. My game loop is based on code from Fix Your Timestep!.


I've read the article more than a few times, but I can't quite work out what exactly is supposed to be going on within the loop.


I'm running my code on a Nexus 10 tablet which has VSync set to 60fps.


Playing catch up


When I run my code, I'm getting caught quite often in the while loop, ie it runs my logic more than once - so runs logic twice in a row before rendering).


With my understanding of the loop, this should happen when rending takes too long to fit into the time slice (which is 16.66666666666667ms).


So, maybe in 'normal circumstances', I would expect this to happen occasionally).



However, I've tried removing all rendering and logic updating and just running a skeleton loop and I'm still getting caught regularly and constantly within the 'while-loop'.


Therefore, when I'm running my game (with rendering and logic), I'm getting a lot of stuttering because I know the loop is going to skip frames. The logic updates are however being capped at 60 which is exactly what I want.


Obviously whether or not my rendering or physics update could be more efficient is not the issue here and is a question for another day as I've already removed that from the equation.


I can smooth things out somewhat with interpolation and it really does make a difference, however, it's not as smooth as some of the games I download from the Play Store and I really need to make it better.


So, what am I missing or not understanding? I would think it should (certainly with no rendering or logic updating to create a bottleneck):



  • Enter the while-loop

  • Exit the while-loop

  • Wait for next VSync

  • And repeat



Here is the loop I'm testing with:


@Override
public void onDrawFrame(GL10 gl) {

newTime = System.currentTimeMillis()*0.001;
frameTime = newTime - currentTime; //Amount of time this entire frame took
if ( frameTime > (0.25))
frameTime = (0.25);


currentTime = newTime;
accumulator += frameTime;

//Update logic
while (accumulator >= dt){
// saveGameState(); //Removed for testing
// updateLogic(dt); //Removed for testing
accumulator -= dt;
}


//Get the amount to interpolate by
// interpolation = (float) (accumulator / dt); //Removed for testing
// render(interpolate); //Removed for testing

}


No comments:

Post a Comment

Simple past, Present perfect Past perfect

Can you tell me which form of the following sentences is the correct one please? Imagine two friends discussing the gym... I was in a good s...