2

Hi I am trying to make a game on panda3d v 1.8.1 (python) but the controls seem to be very sloppy. One has to keep the keys pressed for a second or two to make things happen. Is there any way to make panda3d accept controls faster ?

Here's my code of my key handler :

 class KeyHandler(DirectObject):

      def __init__(self):
        self.accept('arrow_left-repeat', self.lookLeft)
        self.accept('arrow_right-repeat', self.lookRight)
        self.accept('arrow_up-repeat', self.lookUp)
        self.accept('arrow_down-repeat', self.lookDown)
        self.accept('w-repeat', self.Moveforward)
        self.accept('s-repeat', self.Movebackward)
        self.accept('a-repeat', self.Moveleft)
        self.accept('d-repeat', self.Moveright)
        self.accept('q-repeat', self.MoveDown)
        self.accept('e-repeat', self.MoveUp)
        self.accept('space', self.Dotask)
     def lookLeft(self):
        global camxy
        camxy += 2
     def lookRight(self):
        global camxy
        camxy -= 2
     def lookUp(self):
        global camyz
        camyz += 2
     def lookDown(self):
        global camyz
        camyz -= 2


    def Moveforward(self):
         global camx
         if camx < 57 :
           camx += 1
    def Movebackward(self):
         global camx
         if camx > -32 :
           camx -= 1

   def Moveleft(self):
         global camy
         if camy < 42 :
           camy += 1
   def Moveright(self):
         global camy
         if camy > -36 :
           camy -= 1
   def MoveUp(self):
         global camz
         if camz < 15 :
           camz += 0.5
   def MoveDown(self):
         global camz
         if camz >1 :
            camz -= 0.5

a = KeyHandler()

def set_cam(task) :

    camera.setPos(camx,camy,camz)
    camera.setHpr(camxy,camyz,camzx)

taskMgr.add(set_cam, "setcamTask")

The camera which I am using is the default camera of panda3d.

Any help would be appreciated !

infiNity9819
  • 536
  • 2
  • 7
  • 19

1 Answers1

1

You should avoid using the "-repeat" handlers. They take just as long to trigger as more letters take to appear if you hold a key down in any textbox.

The usual way is to use a dict keeping key state:

class KeyHandler(DirectObject):
    keys = {"lookLeft": False, "lookRight": False}  # etcetera

    def __init__(self):
        DirectObject.__init__(self)
        self.accept('arrow_left', self.pressKey, ["lookLeft"])
        self.accept('arrow_left-up', self.releaseKey, ["lookRight"])
        taskMgr.add(self.set_cam, "setcamTask")

    def pressKey(self, key):
        self.keys[key] = True

    def releaseKey(self, key):
        self.keys[key] = False

    # Hopefully method will be passed bound 
    def set_cam(self, task):
        dt = globalClock.getDt()
        if self.keys["lookLeft"]:
            camera.setH(camera.getH() + 2 * dt)
        elif self.keys["lookRight"]:
            camera.setH(camera.getH() + 2 * dt)

a = KeyHandler()

This will also allow you to define user settings for keys more easily.

This is not the first or even most important issue with that code though. set_cam should really be a method of KeyHandler instead of declaring every variable global, and you should multiply movement by each frame's dt to keep the game looking the same speed with different framerates.

rhaps0dy
  • 1,236
  • 11
  • 13
  • 1
    Yes, Panda3D is being developed actively. Check out the commit history on GitHub if you need convincing. :) – Thane Brimhall May 29 '15 at 18:43
  • You can try `taskMgr.add(lambda t: self.set_cam(t), "setcamTask")` then, but I'm not sure if this is too hacky or it actually helps code organization. – rhaps0dy May 30 '15 at 20:12
  • Note you can use polling by doing base.mouseWatcherNode.isButtonDown("arrow_left") or something of the sort instead of keeping your own key map. Also, please remove the erroneous statement about Panda3D no longer being developed; the latest version was released just two months ago, and GitHub clearly shows frequent commits. – rdb Jul 03 '15 at 19:48