Panda3D Manual: Using Intervals to move the Panda
<<prev | top | next>> |
The next step is to cause the panda to actually move back and forth. Add the following lines of code:
    import direct.directbase.DirectStart
    from pandac.PandaModules import *

    from direct.task import Task
    from direct.actor import Actor
    from direct.interval.IntervalGlobal import *
    import math

    #Load the first environment model
    environ = loader.loadModel("models/environment")
    environ.reparentTo(render)
    environ.setScale(0.25,0.25,0.25)
    environ.setPos(-8,42,0)

    #Task to move the camera
    def SpinCameraTask(task):
      angledegrees = task.time * 6.0
      angleradians = angledegrees * (math.pi / 180.0)
      base.camera.setPos(20*math.sin(angleradians),-20.0*math.cos(angleradians),3)
      base.camera.setHpr(angledegrees, 0, 0)
      return Task.cont

    taskMgr.add(SpinCameraTask, "SpinCameraTask")

    #Load the panda actor, and loop its animation
    pandaActor = Actor.Actor("models/panda-model",{"walk":"models/panda-walk4"})
    pandaActor.setScale(0.005,0.005,0.005)
    pandaActor.reparentTo(render)
    pandaActor.loop("walk")

    #Create the four lerp intervals needed to walk back and forth
    pandaPosInterval1= pandaActor.posInterval(13,Point3(0,-10,0), startPos=Point3(0,10,0))
    pandaPosInterval2= pandaActor.posInterval(13,Point3(0,10,0), startPos=Point3(0,-10,0))
    pandaHprInterval1= pandaActor.hprInterval(3,Point3(180,0,0), startHpr=Point3(0,0,0))
    pandaHprInterval2= pandaActor.hprInterval(3,Point3(0,0,0), startHpr=Point3(180,0,0))

    #Create and play the sequence that coordinates the intervals
    pandaPace = Sequence(pandaPosInterval1, pandaHprInterval1,
      pandaPosInterval2, pandaHprInterval2, name = "pandaPace")
    pandaPace.loop()

    run()
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. For example, consider the `pandaPosInterval1` above. When that 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` is started, the orientation of the panda will rotate 180 degrees over a period of 3 seconds. Sequences are tasks that execute one interval after another. The `pandaPace` sequence above causes the panda to move in a straight line, then turn, then in the opposite straight line, then to turn again. The code `pandaPace.loop()` causes the Sequence to be started in looping mode. The result of all this is to cause the panda to pace back and forth from one tree to the other.
<<prev | top | next>> |