0

请看我的乒乓球比赛的以下结构。

游戏循环();方法

   //Only run this in another Thread!
   private void gameLoop()
   {
      //This value would probably be stored elsewhere.
      final double GAME_HERTZ = 30.0;
      //Calculate how many ns each frame should take for our target game hertz.
      final double TIME_BETWEEN_UPDATES = 1000000000 / GAME_HERTZ;
      //At the very most we will update the game this many times before a new render.
      //If you're worried about visual hitches more than perfect timing, set this to 1.
      final int MAX_UPDATES_BEFORE_RENDER = 5;
      //We will need the last update time.
      double lastUpdateTime = System.nanoTime();
      //Store the last time we rendered.
      double lastRenderTime = System.nanoTime();

      //If we are able to get as high as this FPS, don't render again.
      final double TARGET_FPS = 60;
      final double TARGET_TIME_BETWEEN_RENDERS = 1000000000 / TARGET_FPS;

      //Simple way of finding FPS.
      int lastSecondTime = (int) (lastUpdateTime / 1000000000);

      while (running)
      {
         double now = System.nanoTime();
         int updateCount = 0;

         if (!paused)
         {

             //Do as many game updates as we need to, potentially playing catchup.
            while( now - lastUpdateTime > TIME_BETWEEN_UPDATES && updateCount < MAX_UPDATES_BEFORE_RENDER )
            {
               updateGame();
               lastUpdateTime += TIME_BETWEEN_UPDATES;
               updateCount++;
            }



            //If for some reason an update takes forever, we don't want to do an insane number of catchups.
            //If you were doing some sort of game that needed to keep EXACT time, you would get rid of this.
            if ( now - lastUpdateTime > TIME_BETWEEN_UPDATES)
            {

               lastUpdateTime = now - TIME_BETWEEN_UPDATES;
            }


            //Render. To do so, we need to calculate interpolation for a smooth render.
           float interpolation = Math.min(1.0f, (float) ((now - lastUpdateTime) / TIME_BETWEEN_UPDATES) );

           //float interpolation = 1.0f;

            drawGame(interpolation);
            lastRenderTime = now;

            //Yield until it has been at least the target time between renders. This saves the CPU from hogging.
            while ( now - lastRenderTime < TARGET_TIME_BETWEEN_RENDERS && now - lastUpdateTime < TIME_BETWEEN_UPDATES)
            {
               Thread.yield();

               //This stops the app from consuming all your CPU. It makes this slightly less accurate, but is worth it.
               //You can remove this line and it will still work (better), your CPU just climbs on certain OSes.
               //FYI on some OS's this can cause pretty bad stuttering. Scroll down and have a look at different peoples' solutions to this.
               try {Thread.sleep(1);} catch(Exception e) {} 

               now = System.nanoTime();
            }


         }
      }
   }

更新游戏();方法

  if(p1_up){


        if(player.equals("p1")){
                    p1.moveUp();
        }
        else
        {

                    p2.moveUp();

        }


  }
  else if(p1_down){


          if(player.equals("p1")){

                    p1.moveDown();

          }
          else
          {

                p2.moveDown();

          }


  }

提升(); 向下移动();桨法

  public void moveUp(){

      last_y = y;
      last_x = x;

      y -= 50.0;


  }




  public void moveDown(){

      last_y = y;
      last_x = x;

      y += 50.0;


  }

drawGame(插值);方法

      public void paintComponent(Graphics g)
      {

          super.paintComponent(g);

          for(int i=0;i<balls.size();i++){

              paintBall(g, balls.get(i));

          }

          drawPaddle(g, p1);          
          drawPaddle(g, p2);




      }





      public void drawPaddle(Graphics g, Paddle p){


          paddle_drawX = (int)((p.x - p.last_x)*interpolation + p.last_x);
          paddle_drawY = (int)((p.y - p.last_y)*interpolation + p.last_y);



              g.drawRect(paddle_drawX, paddle_drawY, 10, 50);


      }

我是游戏编程的初学者,所以我对游戏循环不太了解。我在互联网上找到了上述固定时间步长的游戏循环,并将其用作我的游戏的游戏循环。循环使球平稳移动,但桨在移动时不会停留在一个地方。当我通过按下一个键来移动我的桨时,桨会不停地摇晃而不会停在一个地方。桨的 y 坐标不断变化

33, 45, 20, 59, 34, 59, 34, 59, 33, 59, 34, 58

我知道问题出在插值上,因为它不断改变值,这将改变渲染中桨的 y 坐标。我一直在考虑这个问题,但我不知道如何让游戏循环适用于任何动作,所以我来这里寻求帮助。我感谢任何建议/帮助!

这是我完整的桨课。

   public class Paddle
   {

       float x;
       float y;      
       float last_y;
       float last_x;

      public Paddle(int x, int y)
      {

          this.x = x;
          this.y = y;
          this.last_x = x;
          this.last_y = y;

      }


      public void setNewX(int d){


      last_y = y;
      last_x = x;

      x = d;


      }


      public void setNewY(int d){

      last_y = y;
      last_x = x;

      y = d;


      }

      public void moveUp(){

          last_y = y;
          last_x = x;

          y -= 50.0;


      }




      public void moveDown(){

          last_y = y;
          last_x = x;

          y += 50.0;


      }



    }

我通过全局变量在主类中启动桨位置。

public Paddle p1 = new Paddle(10, 10);
public Paddle p2 = new Paddle(950, 10);

我有以下事件侦听器来处理击键。

  Action handle_up_action = new AbstractAction(){

      public void actionPerformed(ActionEvent e){

          p1_up = true;


      }

  };



  Action handle_up_action_released = new AbstractAction(){

      public void actionPerformed(ActionEvent e){

          p1_up = false;
      }

  };


  Action handle_down_action = new AbstractAction(){

      public void actionPerformed(ActionEvent e){

          p1_down = true;


      }

  };



  Action handle_down_action_released = new AbstractAction(){

      public void actionPerformed(ActionEvent e){

          p1_down = false;

      }

  };
4

1 回答 1

1

你想用什么来实现interpolation?据我了解,它表示上一个和下一个“更新时间”之间经过的时间百分比。所以它应该每 33.3 毫秒从 0 到 1 连续进行。

我不知道你如何interpolation在方法中使用这个变量paintBall,但是对于桨,它会将你的桨画在和之间的“伪随机位置” p.x;p.yp.last_x;p.last_y取决于两者之间的时间updateGame())。

为了纠正这个问题,从您的循环逻辑中,您应该了解每个游戏实体(球、桨......)必须有两个状态(位置): - 逻辑状态,仅更新每个TIME_BETWEEN_UPDATES - 视觉状态,可以在每次渲染时随时更新。

这就像您有一组点(表示逻辑状态)并且您想要在这些点之间的任何位置进行插值(表示视觉状态)。你的代码是这样

第一个解决方案

纠正桨抖动的最简单方法是避免插值并使用:

public void drawPaddle(Graphics g, Paddle p){
   paddle_drawX = (int)p.x;
   paddle_drawY = (int)p.y;
   g.drawRect(paddle_drawX, paddle_drawY, 10, 50);
}

但是你的动作会是这样的(视觉位置只会改变每个TIME_BETWEEN_UPDATES

第二种解决方案

您想p.x;p.y成为逻辑位置,但如果在输入处理和下一个 updateGame() 之间完成渲染,则视觉位置应在 和 逻辑位置之间插值:调用时p.last_x;p.last_y必须重置。为此,请在内部调用桨的 updateMovement() 方法。p.last_x;p.last_yupdateGame()updateGame()

public void updateMovement(){
    last_y = y;
    last_x = x;
}

您可以有其他解决方案,例如使用速度变量或运动函数,以便获得平滑的运动、加速度等。它主要是第二种解决方案的推广。它需要更大的改变,但它更加灵活和强大。为了实现这一点,您可能希望在桨中存储最后的“更新位置”,以及所有与运动相关的变量,例如运动开始日期。添加一个方法来检索可以在两次更新之间的任何日期调用的“视觉位置”,以及一个更新“逻辑位置”的方法,称为 each updateGame()

于 2013-12-11T14:18:49.453 回答