Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

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?

share|improve this question
    
If you haven't already read it, this might help: gafferongames.com/game-physics/fix-your-timestep –  arriu Nov 27 '10 at 1:59
add comment

1 Answer

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.

share|improve this answer
1  
Be sure to mark this answer as "accepted" so the bot doesn't rebump this question because it thinks it's unanswered. –  Tetrad Nov 27 '10 at 14:42
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.