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!