Question

So, I've been making a small demo program in java using opengl just as an exercise. All was going well until I implemented getTime(); and getDelta(); functions to calculate how much time each frame takes. I then applied it to all my movement calculations by multiplying their speed with getDelta(); and it works well, only that when frames randomly slightly drop and getDelta(); returns a slightly larger value, my calculations start outputting really weird values which are still in the range of the movement the calculation calculates but incorrect, that happens for a few frames until the framerate goes up to normal (250FPS in this case) and the movement continues normally and even from the exact position. I have no idea what could cause this since the getDelta(); seems to function properly?

Heres some code:

private void movementCalculations(double delta) {

    sinInc += 0.0015;
    sinInc2 += 0.010;

    xMove[0] = xSpeed * (float) Math.sin(Math.toDegrees(sinInc / 100 * delta)) * 7500;
    yMove[0] = ySpeed * (float) Math.sin(Math.toDegrees(sinInc / 200 * Math.PI * delta)) * 2500;
    zMove[0] = zSpeed * (float) Math.sin(Math.toDegrees(sinInc / 200 * delta)) * 1250;

    for (int i = 1; i<16; i++) {
        xMove[i] = xSpeed * (float) Math.sin(sinInc2 / 128 * Math.PI * i/2 * delta) * WIDTH * 16;
        yMove[i] = ySpeed * (float) Math.sin(sinInc2 / 64 * Math.PI * i * delta) * HEIGHT * 4;
    }

    xMove[16] = xSpeed * (float) Math.sin(sinInc2 / 64 * Math.PI * 2 * delta) * WIDTH * 8;
    yMove[16] = ySpeed * (float) Math.sin(sinInc2 / 32 * Math.PI * 2 * delta) * HEIGHT * 4;

    xMove[17] = xSpeed * (float) Math.sin(sinInc2 / 32 * Math.PI * 2 * delta) * HEIGHT * 10;
}

In the following debug output, Delta 4.0 is the normal value for 250FPS, then FPS drops and suddenly xMove[17] returns a nearly as large but NEGATIVE value?

Last frame in milliseconds: 231884072 Delta: 4.0 xMove[17]: 164.297 Last frame in milliseconds: 231884077 Delta: 5.0 xMove[17]: -160.83817 Last frame in milliseconds: 231884080 Delta: 3.0 xMove[17]: -0.7198944 Last frame in milliseconds: 231884084 Delta: 4.0 xMove[17]: 162.12277

And if not that's enough, here's a working JAR exectuable of the application which has everything for running it inside the JAR so you can see yourself: http://harha.us.to/files/files_public/Starfield_Exe.jar

I just can't figure out what's wrong or is there even anything wrong in my code at all?

Was it helpful?

Solution

You're currently multiplying sinInc and sinInc2 with delta, so what's framerate-independent is the... rotation speed?

You probably want to multiply the movement speeds ({x|y|z}Speed), though it's hard to be sure.

You should start by moving the delta multiplication up to the common factor instead of pasting * delta everywhere.

sinInc += (0.0015 * delta);

will make it clear that the sine increase will be framerate-independent, .0015 units per unit time.

As for movement, I suspect you're doing someXCoordinate[i] += xMove[i] somewhere. If so, then that's the place to put delta:

obj.xPosition += (obj.xVelocity * delta);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top