的Android游戏固定时间步长小胶质

问题描述:

后改善,但我似乎无法消除偶尔的消耗。的Android游戏固定时间步长小胶质

我GameTimer类包含被称为每一帧来推进时间的方法称为蜱()中,用canStep()和步骤()如下面的方法沿着:

public void tick() { 
    long thisTime = System.nanoTime(); 
    frameTime = thisTime - lastTime; 
    frameTimeMs = (int)(frameTime/NANO_TO_MILLI); 
    frameCounter++; 
    secondCounter -= frameTime; 
    if(secondCounter <= 0) { 
     fps = frameCounter; 
     frameCounter = 0; 
     secondCounter += NANO_TO_SECOND; 
    } 
    lastTime = thisTime; 

    accumulator += Math.min(frameTimeMs, deltaTime * 5);//frameskip >5 frames 
    alpha = accumulator/deltaTime; 
} 

public boolean canStep() { 
    return accumulator >= deltaTime; 
} 

public void step() { 
    accumulator -= deltaTime; 
    alpha = accumulator/deltaTime; 
} 

NB:的DeltaTime保持1000F除以每秒更新的期望数量(例如30)。

我的主游戏循环运行每帧这样的逻辑如下:

gameTimer.tick(); 
while(gameTimer.canStep()) { 
    update(); 
    gameTimer.step(); 
} 

draw(); 

为了计算一个游戏对象的移动予执行以下操作:

float time = gameTimer.getDeltaTime()/1000f; //how much of a second per update 
velocity = speed * direction * time; //simplified: velocity, speed & direction are vec3s 
previousPosition = position;//simplified: values are copied 
position += velocity; 

甲移动的游戏对象的绘制位置然后计算在抽签时使用线性插值:

lerp(drawPosition, previousPosition, position, 1.0f + gameTimer.getAlpha()); 

游戏在赌注在50到60 fps。尝试每秒更新没有产生更好的结果。此外,增加或减少frameskip也不会删除gitter。物体突然向前跳跃几个像素,我一直在努力解决这个问题。

任何人都可以看到的是我错过上面的代码中任何明显的问题?

任何帮助,将不胜感激:)

+0

问题在于时间源和图形缓冲区排队的方式。请参阅https://source.android.com/devices/graphics/architecture.html#loops,了解问题的解释以及如何使用编排器来修复问题。 – fadden

也许有点晚了,但看到这个问题,空让我有点难过。其他人可能会想知道相同的。

乍一看,似乎有什么不对您的代码。 我也成功实现了gafferongames.com在Java中描述的方法。但顺利移动的关键不仅是绘制前的正确时间步或插值。 我不确定你的代码的其余部分,但你确定你永远不会在代码中分配新对象吗? 调用“新建”会在您的堆上分配新对象,这些对象可能会在引用结束时收集垃圾。垃圾收集会造成轻微或更长的时间(当物体太多时)口吃。

解决这个关键无非是:分配每一个对象,你在初始化过程中需要,所以在你更新。 如果您还必须分配新的对象,可以考虑使用这里描述的重复使用同一类的现有对象的对象池: http://gameprogrammingpatterns.com/object-pool.html

希望这会避免很多麻烦的人谁读这一点。