Panda3D Manual: Using Intervals to move the Panda

Contents

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, also called MetaIntervals are tasks that execute one interval after another.

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:

from math import pi, sin, cos
 
from direct.showbase.ShowBase import ShowBase
from direct.task import Task
from direct.actor.Actor import Actor
from direct.interval.IntervalGlobal import Sequence
from panda3d.core import Point3
 
class MyApp(ShowBase):
def __init__(self):
ShowBase.__init__(self)
 
# Disable the camera trackball controls.
self.disableMouse()
 
# Load the environment model.
self.scene = self.loader.loadModel("models/environment")
# Reparent the model to render.
self.scene.reparentTo(self.render)
# Apply scale and position transforms on the model.
self.scene.setScale(0.25, 0.25, 0.25)
self.scene.setPos(-8, 42, 0)
 
# Add the spinCameraTask procedure to the task manager.
self.taskMgr.add(self.spinCameraTask, "SpinCameraTask")
 
# Load and transform the panda actor.
self.pandaActor = Actor("models/panda-model",
{"walk": "models/panda-walk4"})
self.pandaActor.setScale(0.005, 0.005, 0.005)
self.pandaActor.reparentTo(self.render)
# Loop its animation.
self.pandaActor.loop("walk")
 
# Create the four lerp intervals needed for the panda to
# walk back and forth.
pandaPosInterval1 = self.pandaActor.posInterval(13,
Point3(0, -10, 0),
startPos=Point3(0, 10, 0))
pandaPosInterval2 = self.pandaActor.posInterval(13,
Point3(0, 10, 0),
startPos=Point3(0, -10, 0))
pandaHprInterval1 = self.pandaActor.hprInterval(3,
Point3(180, 0, 0),
startHpr=Point3(0, 0, 0))
pandaHprInterval2 = self.pandaActor.hprInterval(3,
Point3(0, 0, 0),
startHpr=Point3(180, 0, 0))
 
# Create and play the sequence that coordinates the intervals.
self.pandaPace = Sequence(pandaPosInterval1,
pandaHprInterval1,
pandaPosInterval2,
pandaHprInterval2,
name="pandaPace")
self.pandaPace.loop()
 
# Define a procedure to move the camera.
def spinCameraTask(self, task):
angleDegrees = task.time * 6.0
angleRadians = angleDegrees * (pi / 180.0)
self.camera.setPos(20 * sin(angleRadians), -20.0 * cos(angleRadians), 3)
self.camera.setHpr(angleDegrees, 0, 0)
return Task.cont
 
app = MyApp()
app.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.