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?