Moving objects based on time

While there is beauty in static images, and they add much to the world of games, in modern video games, action is typically the focus. Today's games are usually very dynamic, having the player and non-player characters moving through scenes containing stacks of crates that can be tossed around, dynamic obstacles such as moving platforms, or flocks of birds that are just there to make a level more compelling.

In this recipe you will learn to make things move around in your scenes and how to use the time that has passed as a parameter for creating the illusion of movement.

Getting ready

We will be using and extending the code created in the recipe Modifying the scene graph, so please take a step back and follow that tip before you haven't already tried that recipe.

How to do it...

It's time to get things moving. Let's animate the scene:

  1. Open Application.py and below the last line of code created in Modifying the scene graph, which is marked here, add the following lines to the source code:
    self.cam.setPos(0, -100, 0)
    self.taskMgr.add(self.update, "update")
    def update(self, task):
    self.sun.setP(task.time * 10)
    self.earth.setH(task.time * -100)
    self.earthCenter.setH(task.time * 50)
    self.moonCenter.setR(task.time * 150)
    return task.cont
    
  2. Press F6 to run the code and watch the planets go around.

How it works...

You just added a task to your program whose sole purpose is to set the positions and rotations of the objects found in the scene graph. The twist about this is that you are not setting these values to fixed numbers, but instead you are multiplying them with the time that has passed since the task was started, which is stored in task.time.

Beside the fact that you just implemented the most basic principle of animation, you also did something else! You just ensured that this little animation is progressing at the same rate, no matter how many frames per second the Panda3D's renderer is drawing.

Our update() method is executed every time a new frame of the scene is rendered. Depending on the speed of our system, this happens at a different frequency—a faster system is able to draw more frames per second than a slower one. If we just moved our objects by a fixed amount per frame, the animation speed would be bound to the frame rate. In a game, this would lead to the undesired effect of gameplay being not stable and unpredictable, because any occasion of the frame rate dropping or increasing would cause the game to change its pace.

By multiplying with the time that has passed, we make our animation independent of the current frame rate. Take the rotation of the sun in our code as an example. On a system that takes 0.5 seconds to render a frame, the sun is rotated by 0.5 * 10 units per frame. Now let's think about what happens if we ran the code on a system that takes one second to produce one frame—the rotation rate is multiplied by the time that has passed so that the sun is rotated 10 units per frame.

If we now compare what happens within one second on our slow and fast systems, we can see that the faster one will render two frames instead of only one. More importantly, though, the sun rotates at the same rate of 10 units per second, no matter how fast or slow the system is able to execute our code.

There's more...

Panda3D provides some additional methods for measuring how much time has passed.

For tasks, there's task.frame for getting the number of frames that have passed since the creation of the task.

Another way of accessing time values is the globalClock object, which can be accessed globally from any point within a Panda3D application. By calling globalClock.getDt() you can retrieve the time that has passed since the last frame, and the method getFrameTime() of the globalClock object returns the time since the program was started. Don't be afraid to experiment and replace task.time in the sample code with one of these methods and watch the results!

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

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