1
\$\begingroup\$

thank you for checking out my issue! I am trying to develop a game in java, but I have run into a problem. I would like to keep the physics as realistic as possible, so I am using a fixed time-step and 2D vectors to apply the force of gravity to a square. However, when I run the game it appears that the square is accelerating way to slowly. It could be that my physics concepts are flawed but I am pretty positive that my vector math is accurate, so I am having trouble finding the root of the problem. Hopefully I am not completely crazy and simply wasting your time, all help is very much appreciated.

Here is the main game loop:

private static void startLoops() {
    double targetFPS = 60.0;
    double targetUpdates = 60.0;

    //  accumulators for update/render
    double updateTime = 0.0;
    double renderTime = 0.0;

    //  variable to compare to accumulated cycle time
    //  desired time between updates in nanoseconds
    double targetDelta = 1000000000/targetUpdates;
    // desired time between frames in nanoseconds
    double targetFrame = 1000000000/targetFPS;

    // start time of the cycle
    double startTime = System.nanoTime();

    int updCount = 0;
    int frameCount = 0;
    double lastTime = System.nanoTime();

    while(shouldRun) {
        double curTime = System.nanoTime();
        //  end time of the previous cycle
        double cycleTime = curTime - startTime;
        startTime = curTime;

        updateTime += cycleTime;
        renderTime += cycleTime;

        //  input is checked every cycle
        cWrangler.updateInput();

        //  starts updating if enough time has passed
        //  while loop in case 2 full time-steps are accumulated
        while (updateTime > targetDelta) {
            double deltaTime = 1.0/60.0;

            //  do any updating here
            cWrangler.update(deltaTime);

            updCount++;

            updateTime -= targetDelta;
        }

        //  starts rendering if enough time has passed
        while (renderTime > targetFrame) {
            //  do any rendering here
            cWrangler.render();

            frameCount++;
            renderTime -= targetFrame;
        }

        if(curTime - lastTime >= 1000000000) {
            System.out.println("Frames: " + frameCount + ", Updates: " + updCount);
            lastTime = curTime;
            frameCount = 0;
            updCount = 0;
        }   
    }
}

Please let me know if this question has been asked elsewhere. I am having trouble searching for a solution to my problem because I can't figure out the cause. Also, feel free to direct me to any resources you my think are helpful. Thanks again for taking some time to read through this!

\$\endgroup\$
0
\$\begingroup\$

I think there’s a problem in this part:

    double curTime = System.nanoTime();
    //  end time of the previous cycle
    double cycleTime = curTime - startTime;
    startTime = curTime;

    updateTime += cycleTime;
    renderTime += cycleTime;

It looks like you are measuring the time the previous cycle took, then allowing both updates and rendering to each take that long. That would result in the time allowed doubling each time through the loop. Which seems like it would cause your reported symptom of too few updates.

\$\endgroup\$
  • \$\begingroup\$ I edited the code I provided to reflect a frame and update counter that I added. It seems that the loop is updating and rendering 60 times as desired. I also experimented with changing the original numbers for target frames and updates, and the counter changed accordingly. \$\endgroup\$ – Adam the Fruit Jul 2 '18 at 15:13
  • \$\begingroup\$ Have you tried printing out curTime - lastTime? I think it will be longer than a second. I think you are getting 60 updates but they are taking longer than a second to happen, causing the slowness. \$\endgroup\$ – Ryan1729 Jul 2 '18 at 16:44

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.