Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8420915
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T02:59:33+00:00 2026-06-10T02:59:33+00:00

I’m writing a little program based on the LunarLander example project. What I want

  • 0

I’m writing a little program based on the LunarLander example project. What I want to achieve is to update my SurfaceView (reposition some Drawables mainly) between fixed intervals like this one:

private static final int UPDATE_INTERVAL = 1000 / 20; // 20 FPS

I’m running a Thread which is doing this in its run() method:

@Override
    public void run() {
        while (isRunning) {
            Canvas c = null;
            try {
                c = view.getSurfaceHolder().lockCanvas(null);
                synchronized (view.getSurfaceHolder()) {
                    if (GameState.RUNNING.equals(state)) {
                        long now = System.currentTimeMillis();
                        if (now - lastUpdate > UPDATE_INTERVAL) {
                            lastUpdate = now;
                            updater.updatePieces();
                        }
                        doDraw(c);
                    }
                }
            } finally {
                if (c != null) {
                    view.getSurfaceHolder().unlockCanvasAndPost(c);
                }
            }
        }
    }

My problem is that it is possible that this Thread is busy waiting for the (now - lastUpdate > UPDATE_INTERVAL) condition to be true. But there is an other case if redrawing takes more than 1000 / 20 seconds. This means that the animation is not always seamless. I tried it out in my emulator and it is usually OK but sometimes my Drawable is slowing down and after that it speeds up for approximately a half second. Is this just the emulator or something else?

I won’t bother you with the details, updatePieces() just moves a little circle from top to bottom. I’m not creating any new objects. I somehow get the feeling that this piece of code is not right. Is this a good practice for animating Drawables or I’m missing something obvious? I’m kind of new in this topic.

I propagate touch and key events from the UI thread to this one so I don’t really want to call sleep() in this Thread because it may cause the UI thread to become unresponsive.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-10T02:59:35+00:00Added an answer on June 10, 2026 at 2:59 am

    Think about how objects react in reality. An object moves a certain distance. The distance it will move in a certain amount of time is dependent on a velocity. Velocity is dependent on the very last acceleration. Distance, velocity and acceleration all depends on time.

    In your code, you do not deal with time at all and therefore there is no real world interpretation to your current code. Put together your physics like this:

    1. Implement a delta system, like Euler integration. This system should contain information about how long time it took to draw the previous frame. NOTE: The Euler integration is accurate when the acceleration is constant, like gravity, and not when acceleration changes. But that is more of an optimization topic, don’t consider this issue right now.
    2. When you know how long the previous frame took, you also know the time when your object(s) did not move at all.
    3. Consider that time in the next update.

    For example:

    public void updatePieces(float timeDelta) {
        // The amount of an acceleration depends on a force
        // in a specified direction. You can skip this, and just
        // give your object a velocity. But to consider acceleration
        // later on, you have to assign forces and resolve them in
        // their respective direction.
        mAccelerationX  = fResX / weight;             
        mAccelerationY  = fResY / weight; 
    
        // Consider the previous velocity and add the change
        // in velocity.
        mVelocityX = mVelocityX + (mAccelerationX * timeDelta);
        mVelocityY = mVelocityY + (mAccelerationY * timeDelta);
    
        mPositionX = mPositionX + (mVelocityX * timeDelta);
        mPositionY = mPositionY + (mVelocityY * timeDelta);
    }
    

    Knowing a little physics help to understand why this makes sense:

    mAccelerationX  = fResX / weight;
    

    is basically Newton’s second law, F = ma.

    mVelocityX = mVelocityX + (mAccelerationX * timeDelta);
    

    works because what we’re doing is to multiply acceleration m/s^2 by time s. This will give you how much the velocity has changed since the previous frame. Simple cancellation gives us (m/s^2)*s = m*s/s^2 = m/s. Add the previous velocity and your done.

    The same logic applies to:

    mPositionX = mPositionX + (mVelocityX * timeDelta);
    // (m/s)*s  = m*s/s = m
    

    Note that you may have to add a scale factor to multiply with in order to make the physics realistic to your screen – like how many pixels equals five meter, or something like that.

    There are tons of articles describing this, one for example is Integration Basics by Glenn Fiedler. Search and you will find more.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a .ini file as follows: [playlist] numberofentries=2 File1=http://87.230.82.17:80 Title1=(#1 - 365/1400) Example
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have just tried to save a simple *.rtf file with some websites and
I want to count how many characters a certain string has in PHP, but
For some reason, after submitting a string like this Jack’s Spindle from a text
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I have a French site that I want to parse, but am running into
I want use html5's new tag to play a wav file (currently only supported
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
Configuring TinyMCE to allow for tags, based on a customer requirement. My config is

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.