android game loop vs updating in the rendering thread

前端 未结 2 1976
我寻月下人不归
我寻月下人不归 2020-12-24 03:09

I\'m making an android game and am currently not getting the performance I\'d like. I have a game loop in its own thread which updates an object\'s position. The rendering t

2条回答
  •  南笙
    南笙 (楼主)
    2020-12-24 03:36

    One part of the problem may be caused by fact that Thread.sleep() is not accurate. Try to investigate what is the actual time of the sleep.

    The most important thing that should make your animations smooth is that you should compute some interpolation factor, call it alpha, that linearly interpolates your animations in consecutive rendering thread calls between two consecutive animation update thread calls. In other words, if your update interval is high comparing to your framerate, not interpolating your animation update steps is like you'd be rendering at update interval framerate.

    EDIT: As an example, this is how PlayN does it:

    @Override
    public void run() {
      // The thread can be stopped between runs.
      if (!running.get())
        return;
    
      int now = time();
      float delta = now - lastTime;
      if (delta > MAX_DELTA)
        delta = MAX_DELTA;
      lastTime = now;
    
      if (updateRate == 0) {
        platform.update(delta);
        accum = 0;
      } else {
        accum += delta;
        while (accum >= updateRate) {
          platform.update(updateRate);
          accum -= updateRate;
        }
      }
    
      platform.graphics().paint(platform.game, (updateRate == 0) ? 0 : accum / updateRate);
    
      if (LOG_FPS) {
        totalTime += delta / 1000;
        framesPainted++;
        if (totalTime > 1) {
          log().info("FPS: " + framesPainted / totalTime);
          totalTime = framesPainted = 0;
        }
      }
    }
    

提交回复
热议问题