Using Intervals to move the Panda

Intervals and Sequences

Intervals

Intervals are tasks that change a property from one value to another over a specified period of time. Starting an interval effectively starts a background process that modifies the property over the specified period of time.

Sequences

Sequences, sometimes called MetaIntervals, are a type of interval that contains other intervals. Playing a sequence will cause each contained interval to execute in sequence, one after the other.

The Program

Update the Code

The next step is to cause the panda to actually move back and forth. Update the code to the following:

 1from math import pi, sin, cos
 2
 3from direct.showbase.ShowBase import ShowBase
 4from direct.task import Task
 5from direct.actor.Actor import Actor
 6from direct.interval.IntervalGlobal import Sequence
 7from panda3d.core import Point3
 8
 9
10class MyApp(ShowBase):
11    def __init__(self):
12        ShowBase.__init__(self)
13
14        # Disable the camera trackball controls.
15        self.disableMouse()
16
17        # Load the environment model.
18        self.scene = self.loader.loadModel("models/environment")
19        # Reparent the model to render.
20        self.scene.reparentTo(self.render)
21        # Apply scale and position transforms on the model.
22        self.scene.setScale(0.25, 0.25, 0.25)
23        self.scene.setPos(-8, 42, 0)
24
25        # Add the spinCameraTask procedure to the task manager.
26        self.taskMgr.add(self.spinCameraTask, "SpinCameraTask")
27
28        # Load and transform the panda actor.
29        self.pandaActor = Actor("models/panda-model",
30                                {"walk": "models/panda-walk4"})
31        self.pandaActor.setScale(0.005, 0.005, 0.005)
32        self.pandaActor.reparentTo(self.render)
33        # Loop its animation.
34        self.pandaActor.loop("walk")
35
36        # Create the four lerp intervals needed for the panda to
37        # walk back and forth.
38        posInterval1 = self.pandaActor.posInterval(13,
39                                                   Point3(0, -10, 0),
40                                                   startPos=Point3(0, 10, 0))
41        posInterval2 = self.pandaActor.posInterval(13,
42                                                   Point3(0, 10, 0),
43                                                   startPos=Point3(0, -10, 0))
44        hprInterval1 = self.pandaActor.hprInterval(3,
45                                                   Point3(180, 0, 0),
46                                                   startHpr=Point3(0, 0, 0))
47        hprInterval2 = self.pandaActor.hprInterval(3,
48                                                   Point3(0, 0, 0),
49                                                   startHpr=Point3(180, 0, 0))
50
51        # Create and play the sequence that coordinates the intervals.
52        self.pandaPace = Sequence(posInterval1, hprInterval1,
53                                  posInterval2, hprInterval2,
54                                  name="pandaPace")
55        self.pandaPace.loop()
56
57    # Define a procedure to move the camera.
58    def spinCameraTask(self, task):
59        angleDegrees = task.time * 6.0
60        angleRadians = angleDegrees * (pi / 180.0)
61        self.camera.setPos(20 * sin(angleRadians), -20 * cos(angleRadians), 3)
62        self.camera.setHpr(angleDegrees, 0, 0)
63        return Task.cont
64
65
66app = MyApp()
67app.run()

When the pandaPosInterval1 interval is started, it will gradually adjust the position of the panda from (0, 10, 0) to (0, -10, 0) over a period of 13 seconds. Similarly, when the pandaHprInterval1 interval is started, the heading of the panda will rotate 180 degrees over a period of 3 seconds.

The pandaPace sequence above causes the panda to move in a straight line, turn, move in the opposite straight line, and finally turn again. The code pandaPace.loop() causes the Sequence to be started in looping mode.

Run the Program

The result of all this is to cause the panda to pace back and forth from one tree to the other.