程序启动时的DeltaTime问题

时间:2012-01-15 18:25:27

标签: android game-physics

我遇到了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;       

}

现在在我的run()函数的MainThread.java中:

 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

    } 

}

在gamePanel.update()中的

我得到了玩家和我的敌人的更新调用。 现在我的问题是,在我的游戏开始时deltaTime非常高,因此我的动作非常快,因为我在更新方法中的敌人类中有以下内容:

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

我做得对,或者我的结构有问题吗? 谢谢你的帮助。

1 个答案:

答案 0 :(得分:1)

您需要在循环开始之前初始化currentTime。你没有显示它是如何初始化的,但是我猜它从0开始,所以在第一次循环迭代时,deltaTime被设置为System.nanoTime()返回的当前时间。

P.S。您是否有理由在一个地方使用System.nanoTime()而在另一个地方使用System.currentTimeMillis()?另外,请考虑坚持使用long值以毫秒(或纳秒)计算所有内容并消除浮点计算。