Hi,
I’m trying to write a program that will spin/rotate the camera to the left when I press the left arrow, and spin it to the right when I press the right arrow.
But for now, all I’m trying to do is get the camera to spin left when I press the left arrow. This is what I’ve come up with:
# spincameraclass.py
import direct.directbase.DirectStart
from pandac.PandaModules import*
from direct.showbase.DirectObject import DirectObject # To listen for events
from direct.task import Task # To use tasks
from direct.actor import Actor # To use animated actors
import math # To use math
class World(DirectObject):
#Constructor
def __init__(self):
# Reposition the camera
base.disableMouse()
camera.setPos(0,-15,7) # X = left & right, Y = zoom, Z = up & down
camera.setHpr(0,-15,0) # Heading, Pitch, Roll
self.loadModels() # Declare/name the new class methods you're going to use.
# Setup key controls
self.accept("escape", sys.exit)
self.accept("arrow_left", self.SpinCamLeft)
# end __init__
def loadModels(self): # Define the new class method. Define what it does.
# Load a model
self.box = loader.loadModel("MODELS/box2")
self.box.reparentTo(render)
self.box.setPos(0,0,0)
# Load an environment
self.environ = loader.loadModel("MODELS/grassplain")
self.environ.reparentTo(render)
self.environ.setPos(0,0,0)
# end loadModels
def SpinCamLeft(self): # Define SpinCamLeft
angledegrees = 1 * 50.0 # speed of rotation & direction
angleradians = angledegrees * (math.pi / 180.0)
base.camera.setPos(30*math.sin(angleradians),-30.0*math.cos(angleradians),3)
base.camera.setHpr(angledegrees, 0, 0)
SpinCamLeft.start()
# end class World
world = World()
run()
Now, when I run this code, the Panda window opens, but it closes the instant I press the left arrow. This message is then displayed on the command prompt:
Originally, I was using a task from the tutorials:
def SpinCameraTask(self,task): # Define the SpinCamera task
angledegrees = task.time * -50.0 # speed of rotation & direction
angleradians = angledegrees * (math.pi / 180.0)
base.camera.setPos(30*math.sin(angleradians),-30.0*math.cos(angleradians),3)
base.camera.setHpr(angledegrees, 0, 0)
return Task.cont
But it seems wrong to use a task for this, so I’ve tried to change it. Frustratingly, I have no idea what task.time represents, so I just made up a number for it.
I’m really struggling here, the tutorials just don’t go far enough to teach you how to control the camera. So any help would be most welcome.
Cheers