# 如何让精灵在不同的帧率下运动速度不变--Frame Rate Independent Movement

1650人阅读 评论(0)

Frame Rate Independent Movement

翻译Kevin Lynx 2006-5-21

I have seen countless posts on this and other message boards, and even personal email has been sent to me, all asking the same question: How do I make it so my stupid objects move at the same speed if the frame rate rises or drops?

在各种论坛以及我收到的个人邮件里，很多人都问我相同的一个问题：我是怎样让我游戏里那些物体以相同速度运动，而无论帧率是下降还是上升？

After a few months programming, I devised what I believe to be the most efficient and useful way to handle this problem. For the purposes of this article, I will encapsulate this functionality in its own class.

在几个月的编程以后，我设计了一个我认为最有效果且最有用的方法来解决这个问题。为了在这篇文章里达到这个目的，我把那些有作用的东西封装成一个类：

 class framerate
{
public:
  float         targetfps;
  float         fps;
  LARGE_INTEGER tickspersecond;
  LARGE_INTEGER currentticks;
  LARGE_INTEGER framedelay;

  float         speedfactor;

  void          Init(float tfps);
  void          SetSpeedFactor();
};

I leave all the members public because they work independently of each other and are all useful in some way to the outside program.

Here is the implementation, followed by some explanation

void framerate::Init(float tfps)
{
  targetfps = tfps;
  QueryPerformanceCounter(&framedelay);
  QueryPerformanceFrequency(&tickspersecond);
}
void framerate::SetSpeedFactor()
{
  QueryPerformanceCounter(&currentticks);
  //This frame's length out of desired length
  speedfactor=(float)(currentticks.QuadPart-framedelay.QuadPart)/((float)tickspersecond.QuadPart/targetfps);
  fps = targetfps/speedfactor;
  if (speedfactor <= 0)
    speedfactor = 1;

  framedelay = currentticks;
}


Some explanation:
targetfps is passed into the Init function, but can be set at any time. It is the target frame rate for the program. This is used in the SetSpeedFactor function to determine what the speedfactor will be. Look down at my explanation of speedfactor.

targetfps 是由Init函数来设置的，但是它也可以在任何时候被设置。它用来表示程序中物体的帧率。这个被用在 SetSpeedFactor 函数里去决定 speedfactor 会怎么样。看下面对于speedfactor 的解释。

fps is the actual frame rate of the program. It is not really needed to make frame rate independent movement, but since it is so closely linked, I include it anyway.

fps 用来表示程序真实的帧率 它没有必要去让帧率成为独立的运动，但是很接近。（不明白这句话的意思）

tickspersecond is set in the Init function to be the number of ticks the high performance timer has per second.

currentticks is set in the SetSpeedFactor function to be the current high performance timer ticks.

framedelay is the previous frame currentticks.

tickspersecond    Init函数里被设置成每秒的tick数。

currentticks SetSpeedFactor 被设置成当前时钟 ticks .

framedelay 是前一帧的 ticks

speedfactor is the heart of this class. When it is set is SetSpeedFactor, it becomes a number that you multiply all your motion by. For instance, if the targetfps is 100, and the actual fps is 85, the speedfactor will be set to 100/85, or about 1.2. You then multiply all your motion ib the game, at its lowest level, by this number.

speedfactor   是这个类的核心。当它在SetSpeedFactor中被设置时，它成为一个****的数。举个例子，如果物体的FPS100，而真实的FPS85speedfactor就会被设置为100/85，或者1.2。在游戏里你把所有的动作与这个数相乘，。。。。

For instance, rather than simply coding spaceship->MoveForward(5), I would code spaceship->MoveForward(5*framerate.speedfactor).

In conclusion:
This simple routine saves a whole heap of trouble. Just plug it into almost any game or other real-time program. Call Init at the beginning of the program, and then each frame call SetSpeedFactor. Then multiply all your movement by the speedfactor. It is that simple. Now stop flooding those message boards :)

1．  按照我们以往计算FPS的方式算出实际的FPS，然后再根据希望的FPS来得到

2．  这也是更为通用的方法，也是上文的方法，即通过时间间隔直接计算，反过来根据这个值来计算实际的FPS

actual_fps = 1 / [(this_tick – last_tick) / 1000]

= 1000 / (this_tick – last_tick)       ====è

因为 want_fps 已知 ，根据 :

speedfactor = want_fps / actual_fps ===è

speedfactor = want_fps / actual_fps

= want_fps / [1000 / (this_tick – last_tick)]

=want_fps * (this_tick – last_tick) / 1000

=want_fps * 每一帧用的秒数

最终公式也就是：

speedfactor = want_fps * (this_tick – last_tick) / 1000

与上文提供的公式对比，完全一致。

OK，本文到此结束。

0
0

* 以上用户言论只代表其个人观点，不代表CSDN网站的观点或立场
个人资料
• 访问：234614次
• 积分：3227
• 等级：
• 排名：第10967名
• 原创：86篇
• 转载：5篇
• 译文：2篇
• 评论：67条
文章分类
评论排行
最新评论