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

  • Home
  • SEARCH
  • 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 7053397
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T03:30:01+00:00 2026-05-28T03:30:01+00:00

i have a problem with deltaTime ive been using. My Code is the following:

  • 0

i have a problem with deltaTime ive been using. My Code is the following:

public class Time {
    public static float deltaTime = 0;                      
    public static long frameCount = 0;                      
    public static float fixedTime = 0;                     
    public static float fixedDeltaTime = 0.1f;      
    public static float maxDeltaTime = 0.25f;       

}

And now in my MainThread.java in my run() function:

 while (running) {
        canvas = null;
        try {
            canvas = this.surfaceHolder.lockCanvas();
            synchronized (surfaceHolder) {
                float newTime = System.nanoTime() / 1000000000.0f;
                Time.deltaTime = frameTime = newTime - currentTime;

                if(frameTime > Time.maxDeltaTime)
                        frameTime = Time.maxDeltaTime;

                currentTime = newTime;

                accumulator += frameTime;

                while(accumulator > Time.fixedDeltaTime)
                { 
                        this.gamePanel.update();   // where the player and my enemy gets updated                         
                        accumulator -= Time.fixedDeltaTime;
                }

                this.gamePanel.render(canvas);
                //Perform all non-physics-related updates here


                ++Time.frameCount;

                framesSkipped = 0;  // resetting the frames skipped

                timeDiff = System.currentTimeMillis() - beginTime;
                // calculate sleep time
                sleepTime = (int)(FRAME_PERIOD - timeDiff);

                    if (sleepTime > 0) {
                            // if sleepTime > 0 we're OK
                            try {
                                    Thread.sleep(sleepTime);    
                            } catch (InterruptedException e) {}
                    }

                    while (sleepTime < 0 && framesSkipped < MAX_FRAME_SKIPS) {
                            // we need to catch up
                            this.gamePanel.update(); // update without rendering
                            sleepTime += FRAME_PERIOD;  // add frame period to check if in next frame
                            framesSkipped++;
                    }

                    if (framesSkipped > 0) {

                            Log.d(TAG, "Skipped:" + framesSkipped);
                    }
                            // for statistics
                            framesSkippedPerStatCycle += framesSkipped;
                            // calling the routine to store the gathered statistics
                            storeStats();
                }
            } finally {
                // in case of an exception the surface is not left in
                // an inconsistent state
                    if (canvas != null) {
                    surfaceHolder.unlockCanvasAndPost(canvas);
                    }
            } //end finally

    } 

}

in the gamePanel.update() ive got the update calls for the player and my enemy.
Now my Problem is, that at the start of my game the deltaTime is extremly high und thus my movement is very fast, because i have the following in my enemy class in the update method:

                   x += vX * Time.deltaTime; // velocity * deltaTime
                   y -= vY * Time.deltaTime;
                   //Log.d(TAG, "Time:"+Time.deltaTime+" x: "+x+" y: "+y);
                   vY -= gravity;

Am i doing it right, or is something wrong with my structure?
Thanks for any help.

  • 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-05-28T03:30:02+00:00Added an answer on May 28, 2026 at 3:30 am

    You need to initialize currentTime before the start of the loop. You don’t show how it’s being initialized, but my guess it starts at 0, so on the first loop iteration, deltaTime is being set to the current time as returned by System.nanoTime().

    P.S. Is there some reason you are using System.nanoTime() in one place and System.currentTimeMillis() in another? Also, consider sticking to computing everything in milliseconds (or nanoseconds) with long values and eliminating the floating point calculations.

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

Sidebar

Related Questions

I have problem compilin this code..can anyone tell whats wrong with the syntax CREATE
I have problem creating new instance of excel 2007 using VBA (from Access 2002).
I have problem with xhr open() method. My code follows : var xmlhttp=false; if(!xmlhttp)
I have problem with using dequeueReusableCellWithIdentifier method. Whenever I use this one, one cell
I have problem with validation such code function show_help_tip(event, element) { var $e =
I have problem with Uploadify: I log in the project using: FormsAuthentication.SetAuthCookie(myName, false); Then,
I have problem with code. I have written a function for extracting a parameter,
I have problem when i using ListView and Linq as datasource. The error down:
i have problem with enum I need make a enum in base class or
I have problem with my code. I don't know what am I doing wrong.

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.