English

ogre 3d 1.7 beginner’s guide 第四章 第四小节 中文翻译 修改代码使其代替基于每帧的运动为基于时间的运动

2011/3/8

Modifying the code to be time based rather than frame based

【 修改代码使其代替基于每帧的运动为基于时间的运动 】

Depending on your computer, the model in the scene might be moving quite fast or quite

slow or just at the right speed. The reason for the different speeds at which the model might

move is that, in our code, we move the model 0.1 units on the z-axis before a new frame is

rendered every time. A new computer might be able to render the scene with 100 frames

per second; this would move the model 10 units per second. When using an old computer,

we could have 30 frames per seconds, then the model would only move 3 units. This is only one third, as compared to the new computers. Normally, we want our application to be consistent across different platforms and capabilities so that it will run at the same speed. This can be easily achieved with Ogre.

【 根据您的电脑,场景中的模型可能移动的或快或慢或刚刚好的速度。造成不同速度的原因可能是模型移动的原因,在我们代码中,在每次渲染一个新帧之前,我们沿Z轴每帧移动模型0.1个单位。一个新的电脑可能以每秒100帧的速度渲染,那么模型将会每秒移动10个单位。当使用一个旧电脑的时候,可能就是30帧每秒,那么模型就只会移动3个单位。这相比新电脑只是不到三分之一。通常,我们想要程序在不同的平台和电脑性能下保持一致,这样他们就以相同的速度运行。这可以被Ogre简单的完成。】

Time for action – adding time-based movement

【 实践时刻 —— 添加基于时间的运动 】

We will use the previously used code and only modify one line of code:

【 我们将会使用之前的代码并指改变一行代码:】

    • Change the line where we translate the node to:

【 1. 改变我们变换结点的那行代码:】

_node->translate(Ogre::Vector3(10,0,0) * evt.timeSinceLastFrame);

2.  Compile and run the application. You should see the same scene as before, only the

model might move at a different speed.

【 2. 编译运行程序。你可能会看到相同的场景,只有模型会以不同的速度移动。】

What just happened?

【 刚刚发生了什么?】

We changed our movement model from frame-based to time-based. We achieved this by

adding a simple multiplication. As said before, frame-based movement has some downfalls.

Time-based movement is simply superior because we get the same movement on all

computers and have much more control over the movement speed. In step 1, we used the

fact that Ogre 3D passes a FrameEvent when calling the frameStarted() method. This

class holds the time since the last frame was rendered in seconds:

(Ogre::Vector3(10,0,0) * evt.timeSinceLastFrame);

This line uses this data to calculate how much we want to move our model in this frame.

We use a vector and multiply it by the time since the last frame in seconds. In this case,

we will use the vector (10,0,0). This means that we want our model to move 10 units on

the x-axis every second. Let's say we render with 10 frames per second; then for each

frame, evt.timeSinceLastFrame would be 0.1f. In each frame we multiply evt.

timeSinceLastFrame by the vector (10,0,0), which results in the vector (1,0,0). This result

is applied to the scene node of each frame. With 10 frames per second, this will add up to a movement of (10,0,0) per second, which is exactly the value we wanted our model to move by per second.

【 我们改变基于帧的运动模式为基于时间的模式。我们添加一个简单的乘法了实现基于时间的模式。正如前面所说,基于帧的运动有一些缺点。基于时间的运动更为出众,因为我们需要在所有的电脑上达到同样的运动效果而且在运动速度上有更高的可控性。在第一步,我们使用Ogre 3D的一个FrameEvent传递给调用的frameStarted() 函数。这个FrameEvent类包含了在短时间内自从上一帧被渲染到现在的时间:

(Ogre::Vector3(10,0,0) * evt.timeSinceLastFrame);

这行代码使用这个公式来计算移动模型每帧移动的大小。我们使用一个三维向量乘以自上一帧到现在的秒数来计算。在当前的情况下,我们使用了向量(10,0,0)。这意味着我们想要模型每秒沿X轴移动10个单位。比如说,我们每秒渲染10帧;然后对于每一帧,evt.timeSinceLastFrame将会是0.1f。在每帧中,我们用向量(10,0,0)乘以evt.timeSinceLastFrame,那么结果会是(1,0,0).这一结果将会应用于场景结点移动的每一帧。

Pop quiz – the difference between time- and frame-based movement

【 简单测试 —— 基于时间和基于帧运动之间的不同 】

Describe in your own words the difference between frame-based and time-based

movement.

【 用你自己的话描述基于帧和基于时间运动的不同。 】

Have a go hero – adding a second model

【 让英雄动起来 —— 添加第二个模型 】

Add a second model to the scene and let it move in the opposite direction to the

current model.

【 添加第二个模型到场景并使它沿场景中Sinbad模型的相反方向运动。】