Panda3D Manual: Controlling the Camera
  <<prev top next>>     

By default, Panda3D runs a task that enables you to move the camera using the mouse. The keys to do this are:

  • Left Button: pan left and right
  • Right Button: move forwards and backwards
  • Middle Button: rotate around the origin of the application
  • Right and Middle Buttons: roll the point of view around the view axis

Go ahead and try this camera control system. The problem with this camera control system is that it is sometimes awkward, it is not always easy to get the camera pointed in the direction we want.

Instead, we are going to write a task that controls the camera's position explicitly. A task is nothing but a subroutine that gets called every frame. Update your code as follows:

import direct.directbase.DirectStart
from direct.task import Task
from direct.actor import Actor
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")

run()

The function taskMgr.add tells the panda task manager that it should call the subroutine SpinCameraTask every frame. This is a subroutine that we have written to control the camera. As long as the subroutine SpinCameraTask returns the constant Task.cont, the task manager will continue to call it every frame.

The subroutine calculates the desired position of the camera based on how much time has elapsed. The camera rotates 6 degrees every second. The first two lines compute the desired orientation of the camera, first in degrees, then in radians. The setPos call actually sets the position of the camera. The setHpr call actually sets the orientation.

The camera should no longer be underground, and furthermore, the camera should now be rotating about the clearing:

Image:Tutorial2.jpg

  <<prev top next>>