2012-01-15 15 views
0

deltaTime iveに問題があります。私のコードは以下の通りです:プログラム開始時のDeltaTimeの問題

public class Time { 
    public static float deltaTime = 0;      
    public static long frameCount = 0;      
    public static float fixedTime = 0;      
    public static float fixedDeltaTime = 0.1f;  
    public static float maxDeltaTime = 0.25f;  

}

そして今、私のMainThread.javaの私の実行()関数内:gamePanel.updateで

while (running) { 
     canvas = null; 
     try { 
      canvas = this.surfaceHolder.lockCanvas(); 
      synchronized (surfaceHolder) { 
       float newTime = System.nanoTime()/1000000000.0f; 
       Time.deltaTime = frameTime = newTime - currentTime; 

       if(frameTime > Time.maxDeltaTime) 
         frameTime = Time.maxDeltaTime; 

       currentTime = newTime; 

       accumulator += frameTime; 

       while(accumulator > Time.fixedDeltaTime) 
       { 
         this.gamePanel.update(); // where the player and my enemy gets updated       
         accumulator -= Time.fixedDeltaTime; 
       } 

       this.gamePanel.render(canvas); 
       //Perform all non-physics-related updates here 


       ++Time.frameCount; 

       framesSkipped = 0; // resetting the frames skipped 

       timeDiff = System.currentTimeMillis() - beginTime; 
       // calculate sleep time 
       sleepTime = (int)(FRAME_PERIOD - timeDiff); 

        if (sleepTime > 0) { 
          // if sleepTime > 0 we're OK 
          try { 
            Thread.sleep(sleepTime);  
          } catch (InterruptedException e) {} 
        } 

        while (sleepTime < 0 && framesSkipped < MAX_FRAME_SKIPS) { 
          // we need to catch up 
          this.gamePanel.update(); // update without rendering 
          sleepTime += FRAME_PERIOD; // add frame period to check if in next frame 
          framesSkipped++; 
        } 

        if (framesSkipped > 0) { 

          Log.d(TAG, "Skipped:" + framesSkipped); 
        } 
          // for statistics 
          framesSkippedPerStatCycle += framesSkipped; 
          // calling the routine to store the gathered statistics 
          storeStats(); 
       } 
      } finally { 
       // in case of an exception the surface is not left in 
       // an inconsistent state 
        if (canvas != null) { 
        surfaceHolder.unlockCanvasAndPost(canvas); 
        } 
      } //end finally 

    } 

}

( )iveは、プレイヤーと私の敵の更新を求めました。 は今私の問題は私のゲームの開始時にdeltaTime iは更新方法で私の敵のクラスでは、次のしているので、私の動きは、非常に高速であるので、めちゃめちゃ高いウントであること、である:

    x += vX * Time.deltaTime; // velocity * deltaTime 
        y -= vY * Time.deltaTime; 
        //Log.d(TAG, "Time:"+Time.deltaTime+" x: "+x+" y: "+y); 
        vY -= gravity; 

私がやっていますそれとも、私の構造に何か問題がありますか? ありがとうございました。

答えて

1

ループの開始前にcurrentTimeを初期化する必要があります。あなたはそれがどのように初期化されているかはわかりませんが、私は0から始めると推測します。したがって、最初のループ反復では、deltaTimeは現在の時刻に設定されています(System.nanoTime())。

P.S.ある場所でSystem.nanoTime()、もう1箇所にSystem.currentTimeMillis()を使用している理由はありますか?また、すべての値をlongの値でミリ秒(またはナノ秒)単位で計算し、浮動小数点計算を排除することを検討してください。

+0

あなたは正しいです。 currentTimeは最初は0に設定されています。それを設定することをお勧めしますか? System.nanoTime()もですか?もし動きが最初に0になるのであれば、それは悪いことではありません。これまでのありがとう – puelo

+0

@ the P.S.ミリ秒を使用してavgFps/currentFPSを計算する必要はありません。私がdeltaTimeをテストしている間、私はfpsCountに影響を与えたくありませんでした。しかし、私は私の目的のために数ミリ秒で十分だったと思います! – puelo

+0

@puelo - それをSystem.nanoTime()に初期化します。ゲームが始まったばかりなので、最初の反復では動きはゼロになります。これは1つのフレームに対してのみ起こります。 (私の推測では、あなたは 'beginTime'と同じ問題を抱えています。) –

関連する問題