私は Java と LWJGL で物理/ゲーム エンジンに取り組んでいます。この時点まで、私はDisplay.Sync(int)
一定のフレームレートと 1/int のタイム デルタを物理演算の更新に使用していました。処理能力を十分に活用できるように、リアルタイム デルタを実装しようとしています。以下は、このデルタを追跡するために使用するクラスです。
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();
}
}
各ティックで、エンジンは物理エンティティをループし、標準の重力などを適用して、速度と加速度のコンポーネントを更新します。次に、時間デルタを使用してモーションが適用されることになっています。ループの最後に、updateDelta()
が呼び出されます。問題は、これがどういうわけかすべてをより速く動かすことです。たとえば、これが私のモーション モジュールです。
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()
現在の時間デルタを返すだけです。
public double timeDelta(){
return PhysicsUtils.getTimeDelta();
}
私はこれについて間違った方法で行っていますか?