I am trying to create a simple game on android but I am unsure the best way to do the game loop. Looking at the LunarLander sample the game loop just loops like a variable time step loop (elapsed = now-lastupdate). I tried using this method to draw a circle that grows in size to a max radius then shrinks to a min radius and repeats. However, when I run the program the growing stage is very choppy and slow (it takes longer than it should to get to max radius).
My Code:
public void updateSize(double elapsed) {
if (mGrowing == true && mRadius > mMaxRadius) {
mGrowing = false;
}
if (mGrowing == false && mRadius < mMinRadius) {
mGrowing = true;
}
if (mGrowing == true) {
mRadius += mDRadius * elapsed;
} else {
mRadius -= mDRadius * elapsed;
}
}
mDRadius is the number of pixel the radius should change per second and elapsed is the number of seconds that has passed.
I tried putting in a sleep of 2ms and 5ms in my game loop and I get the same result.
I converted my loop to a fixed time step (only run physics update if now-lastupdate > TIME_STEP) and the animation was a lot smoother.
UPDATE: I change the sleep to 15ms and it's a lot smoother. Every other example I've seen of an android game does not have a sleep in the main loop. Is this because my sample is just so simple that it takes virtually no time to update and all other examples just have a longer update time or am I missing something?