I’m new to Panda3D and Python. I’ve been fiddling around with the examples and have modified the first one to add some spinning blocks and user-controlled camera movement. However, when I run the code below the camera flickers between what seems to be the origin and the desired position when a key is pressed. What am I doing wrong?
Also, not sure if creating an object to handle keyboard input in this way is a good idea… what’s the usual set up?
TIA
Paul
import sys
from direct.showbase import DirectObject
from random import randint
from math import pi, sin, cos
from direct.showbase.ShowBase import ShowBase
from direct.task import Task
# 'constants'
NUM_BLOCKS = 10
class Block():
def __init__(self):
self.model = loader.loadModel("lump.x")
self.model.reparentTo(render)
self.model.setPos(randint(-8, 8), randint(-8, 8), 1)
self.rotation = randint(0, 359)
if randint(1, 2) == 2:
self.delta = randint(1, 3)
else:
self.delta = randint(-3, -1)
class ReadKeys(DirectObject.DirectObject):
def __init__(self):
self.accept('arrow_up-repeat', self.moveCameraUp)
self.accept('arrow_down-repeat', self.moveCameraDown)
self.accept('escape', sys.exit)
def moveCameraUp(time):
app.cameraUp()
def moveCameraDown(time):
app.cameraDown()
class MyApp(ShowBase):
def __init__(self):
ShowBase.__init__(self)
r = ReadKeys()
# load scenery
self.scene = self.loader.loadModel("models/environment")
self.scene.reparentTo(self.render)
self.scene.setScale(0.25, 0.25, 0.25)
self.scene.setPos(-8, 42, 0)
# create random blocks
self.blocks = []
for i in range(0, NUM_BLOCKS):
self.blocks.append(Block())
# set up task to spin blocks
self.taskMgr.add(self.spinBlocksTask, "SpinBlocks")
# set up camera
self.cameraX = 0.0
self.cameraY = -20.0
self.cameraZ = 3.0
self.camera.setPos(self.cameraX, self.cameraY, self.cameraZ)
def cameraDown(self):
self.cameraZ -= 0.1
self.camera.setPos(self.cameraX, self.cameraY, self.cameraZ)
def cameraUp(self):
self.cameraZ += 0.1
self.camera.setPos(self.cameraX, self.cameraY, self.cameraZ)
def spinBlocksTask(self, task):
for i in range(0, NUM_BLOCKS):
self.blocks[i].rotation += self.blocks[i].delta
self.blocks[i].model.setHpr(0, 90, self.blocks[i].rotation)
return Task.cont
app = MyApp()
app.run()