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 direct.interval.IntervalGlobal import LerpPosInterval, LerpHprInterval
8from panda3d.core import Point3
9
10
11class MyApp(ShowBase):
12 def __init__(self):
13 ShowBase.__init__(self)
14
15 # Disable the camera trackball controls.
16 self.disable_mouse()
17
18 # Load the environment model.
19 self.scene = self.loader.load_model("models/environment")
20 # Reparent the model to render.
21 self.scene.reparent_to(self.render)
22 # Apply scale and position transforms on the model.
23 self.scene.set_scale(0.25, 0.25, 0.25)
24 self.scene.set_pos(-8, 42, 0)
25
26 # Add the spin_camera_task procedure to the task manager.
27 self.taskMgr.add(self.spin_camera_task)
28
29 # Load and transform the panda actor.
30 self.panda_actor = Actor("models/panda-model",
31 {"walk": "models/panda-walk4"})
32 self.panda_actor.set_scale(0.005, 0.005, 0.005)
33 self.panda_actor.reparent_to(self.render)
34 # Loop its animation.
35 self.panda_actor.loop("walk")
36
37 # Create the sequence of lerp intervals needed for the panda to
38 # walk back and forth.
39 self.panda_pace = Sequence(
40 LerpPosInterval(self.panda_actor, 13,
41 Point3(0, -10, 0), startPos=Point3(0, 10, 0)),
42 LerpHprInterval(self.panda_actor, 3,
43 Point3(180, 0, 0), startHpr=Point3(0, 0, 0)),
44
45 LerpPosInterval(self.panda_actor, 13,
46 Point3(0, 10, 0), startPos=Point3(0, -10, 0)),
47 LerpHprInterval(self.panda_actor, 3,
48 Point3(0, 0, 0), startHpr=Point3(180, 0, 0)),
49 name="panda-pace"
50 )
51 self.panda_pace.loop()
52
53 # Define a procedure to move the camera.
54 def spin_camera_task(self, task):
55 angle_degrees = task.time * 6.0
56 angle_radians = angle_degrees * (pi / 180.0)
57 self.camera.set_pos(20 * sin(angle_radians), -20 * cos(angle_radians), 3)
58 self.camera.set_hpr(angle_degrees, 0, 0)
59 return Task.cont
60
61
62app = MyApp()
63app.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.