2
\$\begingroup\$

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?

\$\endgroup\$
1

1 Answer 1

4
\$\begingroup\$

Ok, I was doing something stupid. I was using an integer for mRadius instead of a double. When the time steps are very very small as it was in my case, the increment was < 1px so it was behaving strangely because of that. I changed mRadius to a double so that it would increment properly.

\$\endgroup\$
1
  • 1
    \$\begingroup\$ Be sure to mark this answer as "accepted" so the bot doesn't rebump this question because it thinks it's unanswered. \$\endgroup\$
    – Tetrad
    Commented Nov 27, 2010 at 14:42

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .