Time for action — adding time-based movement

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

  1. Change the line where we translate the node to:
    _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.

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.

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.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.149.241.48