Java Android Gameloop(openGL ES 2.0)的问题
概述 在我的游戏循环中有很多起伏的动画问题。我正在运行来自的循环 onDrawFrame如您所见,如果时间过长,我的循环允许跳帧(我使用的是固定时间增量,60帧的时间“切片”为16.6666ms) 我已经证实,当跳过帧时会发生口吃Java Android Gameloop(openGL ES 2.0)的问题,java,android,opengl-es,opengl-es-2.0,game-loop,Java,Android,Opengl Es,Opengl Es 2.0,Game Loop,概述 在我的游戏循环中有很多起伏的动画问题。我正在运行来自的循环 onDrawFrame如您所见,如果时间过长,我的循环允许跳帧(我使用的是固定时间增量,60帧的时间“切片”为16.6666ms) 我已经证实,当跳过帧时会发生口吃 当存在帧跳过时,如何阻止动画变得不稳定 如果它绝对必须跳过帧(我承认在某些设备上会出现这种情况),我如何减轻这种波动 自上次调用onDrawFrame(GL10 gl)以来,似乎最多调用24毫秒 在我的Google Nexus 10平板电脑上,我的实际渲染时间
- 当存在帧跳过时,如何阻止动画变得不稳定
- 自上次调用onDrawFrame(GL10 gl)以来,似乎最多调用24毫秒
float interpolation;
long nextGameTick = System.currentTimeMillis();
//loop counter
int loops;
//This is the amount of frames that we allow our app to skip before logic updating is affected (generally shouldn't happen)
final int maxFrameskip = 5;
//Game updates per second
final int ticksPerSecond = 60;
//Amount of time each update should take
final int skipTicks = (1000 / ticksPerSecond);
public void onDrawFrame(GL10 gl) {
//Set/Re-set loop back to 0 to start counting again
loops=0;
while(System.currentTimeMillis() > nextGameTick && loops < maxFrameskip){
SceneManager.getInstance().getCurrentScene().updateLogic();
nextGameTick+=skipTicks;
timeCorrection += (1000d/ticksPerSecond) % 1;
nextGameTick+=timeCorrection;
timeCorrection %=1;
loops++;
tics++;
}
interpolation = (float)(System.currentTimeMillis() + skipTicks - nextGameTick)
/ (float)skipTicks;
render(interpolation);
}
当渲染模式脏时,尝试使用渲染模式。这意味着不再使用onDrawFrame,而是在游戏循环中使用requestRender() 您是否在游戏循环中分配了内存(记住,使用for-each语法遍历集合会分配一个迭代器)?GC可能导致口吃。检查“D/dalvikvm GC_CONCURRENT freed”的logcat调试消息,它将显示它暂停了多长时间。您好@感谢您的评论-在我的循环过程中没有发生分配-这是我检查的第一件事,所以我在早期排除了它-干杯@你解决问题了吗?我也有同样的问题。很不幸,我还没有解决。我已经尝试了所有的方法,但是当跳过帧时,仍然有起伏的动画。现在,我只是和它生活在一起,但需要在某个时候重新访问它。如果你遇到任何建议,我很乐意听听!!:-(
render(float interpolation){
//This example shows interpolation for X axis only. Y position (spriteScreenY is assumed to be valid)
interpolatedPosX = spriteGridX+(SpriteXVelocity*dt)*interpolation;
spriteScreenPosX = interpolationPosX * screenWidth;
drawSprite(spriteScreenX, spriteScreenY);
}