I've been looking up on FPS independent movement recently, and I somewhat understand the concept. But I'm having trouble implementing it in pure Java.

Any one have any idea of how I could implement is as well as abstract it in its own class such as "Time" class?

So, I tried implementing the suggestions in my game loop. but even though i am getting steady 60 FPS, my "deltaTime" has values of around 0.6 - 0.9, but for 60 FPS it should be 0.01 !

What is going wrong here? Should I calculate "deltaTime" at 1 or 2?

Code :

long lastTime = System.nanoTime();
    double delta = 0.0;
    double ns = 1000000000.0 / 60.0;
    long timer = System.currentTimeMillis();
    int updates = 0;
    int frames = 0;
    while (running) {
        long now = System.nanoTime();
        delta += (now - lastTime) / ns;

        // 1

        deltaTime = ((now - lastTime) / 1000000d);

        lastTime = now;

        if (delta >= 1.0) {

            // 2 :

        if (System.currentTimeMillis() - timer > 1000) {
            timer += 1000;
            //System.out.println(updates + " ups, " + frames + " fps");
            updates = 0;
            frames = 0;
        if (glfwWindowShouldClose(window))
            running = false;
  • 2
    \$\begingroup\$ What part are you having trouble with? What have you tried & how has it not worked? \$\endgroup\$ – Pikalek Jan 16 '18 at 14:45
  • \$\begingroup\$ I am having trouble with calculating the delta time using pure java functions. \$\endgroup\$ – Saumi Jan 16 '18 at 16:08
  • \$\begingroup\$ Is this what you're looking for: gamedev.stackexchange.com/questions/111741/… \$\endgroup\$ – Pikalek Jan 16 '18 at 16:38
  • 2
    \$\begingroup\$ Possible duplicate of Calculating delta time \$\endgroup\$ – Tyyppi_77 Jan 17 '18 at 14:44
  • \$\begingroup\$ @Pikalek I tried implementing the solution, but something is going wrong. please check the post for the code. \$\endgroup\$ – Saumi Jan 17 '18 at 15:32

I recommend reading Glenn Fiedler article on that topic, it's very old but useful. At the end of the article, he shares a solid implementation of a fixed time-step, though not in java language. If you have a problem with the syntax, please point to the part which you can't implement in Java, so we might help.

Regarding your delta time problem, try setting a value for your fixed delta time, say 0.01, then in each update loop, add the delta time (without dividing by 1000...)to a temp variable, then check if this variable is greater than or equal to your fixed delta time, if it's, run your update function, then decrease that temp variable by the delta time, this is the same implementation in the provided link.

  • \$\begingroup\$ I tried to implement the suggestions in my game loop, but i get the wrong values for delta time. \$\endgroup\$ – Saumi Jan 17 '18 at 15:59
  • \$\begingroup\$ Why are you dividing by 1000000000.0? and why are you using the nanoseconds instead of milliseconds inside the while loop? \$\endgroup\$ – Yaser Alosh Jan 18 '18 at 18:28
  • \$\begingroup\$ Edited my answer. \$\endgroup\$ – Yaser Alosh Jan 20 '18 at 6:14

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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