0
votes

I'm working on a physics/game engine in Java and LWJGL. Up until this point, I have used Display.Sync(int) to use a constant frame-rate and a time delta of 1/int for physics updates. I'm attempting to implement true time delta so I can fully utilize processing power. Below is the class that I use to keep track of this delta:

package techwiz24.jVox2D.Physics;

public class PhysicsUtils {
    private static long lastTime = System.nanoTime();

    public static double getTimeDelta(){
        return (System.nanoTime()-lastTime)/1000000000d;
    }

    public static void updateDelta(){
        lastTime = System.nanoTime();
    }
}

On each tick, the engine loops through Physics Entities and applies standard gravity and so on, updating the velocity and acceleration components. Then Motion is supposed to be applied using the time delta. At the end of the loop, updateDelta() is called. The issue is that this somehow makes everything move faster. For example, here is my motion module:

package techwiz24.jVox2D.Physics;

/**
 * Moves an object in relation to it's velocity, acceleration, and time components
 * using the Kinematic equations.
 * @author techwiz24
 *
 */
public class PStandardMotion extends PhysicsEffect {

    @Override
    public String getName() {
        return "Standard Motion Effect";
    }

    @Override
    public String getVersion() {
        return "1";
    }

    /**
     * Using two Kinematic Equations, we can update velocity and location 
     * to simulate an earth-like gravity effect. This is called every tick,
     * and uses the TimeDelta stored in the physics object (Time since last update)
     * as the Time component. TODO: Figure out actual time deltas...
     * 
     * Note: This ignores wind resistance
     * 
     * Using: V_FINAL=V_INITIAL+at
     *        X=V_INITIAL*t + .5 * a * t^2
     * 
     * @param o The physics object to apply this to.
     */
    public void applyTo(PhysicsObject o) {

        double vf = o.getVelocityComponent()[1] + (o.getAccelerationComponent()[1]*o.timeDelta());
        double dy = o.getVelocityComponent()[1] + (0.5d * o.getAccelerationComponent()[1] * Math.pow(o.timeDelta(), 2));
        double dx = o.getVelocityComponent()[0] + (0.5d * o.getAccelerationComponent()[0] * Math.pow(o.timeDelta(), 2));

        o.updateVelocity(o.getVelocityComponent()[0], vf);
        o.updateLocation(o.getLocationComponent()[0]+dx, o.getLocationComponent()[1]+dy);

    }

}

timeDelta() just returns the current time delta.

public double timeDelta(){
        return PhysicsUtils.getTimeDelta();
}

Am I going about this in the wrong manner?

1

1 Answers

-1
votes
This is generally how I do my FPS counter.

public class Game {
public static final int WIDTH = 800;
public static final int HEIGHT = 600;
public static final String TITLE = "Physics Tech Demo";

private long lastFrame;
private long lastFPS;
private int fps;

public void init() {
    try {
        Display.setDisplayMode(new DisplayMode(WIDTH, HEIGHT));
        Display.setTitle(TITLE);
        Display.create();
    } catch (LWJGLException e) {
        e.printStackTrace();
        Display.destroy();
    }

    initGL();
    getDelta();
    lastFPS = getTime();

    while (!Display.isCloseRequested()) {
        int delta = getDelta();
        tick(delta);
        renderGL();
        Display.update();
        Display.sync(60);
    }
    Display.destroy();
}

public int getDelta() {
    long time = getTime();
    int delta = (int) (time - lastFrame);
    lastFrame = time;

    return delta;
}

public long getTime() {
    return (Sys.getTime() * 1000) / Sys.getTimerResolution();
}

public void updateFPS() {
    if (getTime() - lastFPS > 1000) {
        System.out.println("FPS: " + fps);
        fps = 0;
        lastFPS += 1000;
    }
    fps++;
}

public void initGL() {
}

public void tick(int delta) {
    updateFPS();
}

public void renderGL() {
}

public static void main(String[] args) {
    Game game = new Game();
    game.init();
}

}