My character is not moving

Hi Guys,

I’m new to panda3d and to python, so be gentle :wink:

I took some code from the roaming-ralph tutorial and other stuff and put all in my own script. Everything works fine, but my character is not moving when there is a move-key pressed. Also, the animation is looping and never stops.

import direct.directbase.DirectStart  #Initialize Panda and create a window
import random, sys, os, math, time, threading, socket          #libs
from direct.gui.OnscreenText import OnscreenText
from direct.gui.OnscreenImage import OnscreenImage
from direct.gui.DirectGui import *
from direct.task.Task import Task     #Task run
from direct.actor.Actor import Actor
from pandac.PandaModules import Fog
from pandac.PandaModules import *
from pandac.PandaModules import WindowProperties
from pandac.PandaModules import Shader
from pandac.PandaModules import Point3,Vec4
from pandac.PandaModules import NodePath
from direct.filter.CommonFilters import CommonFilters
from pandac.PandaModules import CollisionTraverser,CollisionNode
from pandac.PandaModules import CollisionHandlerQueue,CollisionRay
from direct.showbase.DirectObject import DirectObject

class Avatar(DirectObject):
    def  __init__(self):

        environ = loader.loadModel("models/world/world")
        environ.reparentTo(render)
        self.ralph = Actor("models/ralph/ralph",{"run":"models/ralph/ralph-run","walk":"models/ralph/ralph-walk"})
        self.ralph.reparentTo(render)
        self.ralph.setPos(0,20,5)
               
        self.keyMap = {"left":0, "right":0, "forward":0, "back":0,}
        # Accept the control keys for movement
        self.accept("escape", sys.exit)
        self.accept("w", self.setKey, ["forward",1])
        self.accept("a", self.setKey, ["left",1])        
        self.accept("s", self.setKey, ["back",1])
        self.accept("d", self.setKey, ["right",1])        
        self.accept("w-up", self.setKey, ["forward",0])
        self.accept("a-up", self.setKey, ["left",0])
        self.accept("s-up", self.setKey, ["back",0])
        self.accept("d-up", self.setKey, ["right",0])        
        
        #Dummy over the avatars head
        self.dummy = NodePath(PandaNode("floater"))
        self.dummy.reparentTo(render)
        
        # Enable a 'light ramp' - this discretizes the lighting,
        # which is half of what makes a model look like a cartoon.
        # Light ramps only work if shader generation is enabled,
        # so we call 'setShaderAuto'.
        tempnode = NodePath(PandaNode("temp node"))
        tempnode.setAttrib(LightRampAttrib.makeSingleThreshold(0.5, 0.4))
        tempnode.setShaderAuto()
        base.cam.node().setInitialState(tempnode.getState())
        
        # Use class 'CommonFilters' to enable a cartoon inking filter.
        # This can fail if the video card is not powerful enough, if so,
        # display an error and exit.
        self.separation = 1 # Pixels
        self.filters = CommonFilters(base.win, base.cam)
        filterok = self.filters.setCartoonInk(separation=self.separation)
        
        # Create a non-attenuating point light and an ambient light.
        plightnode = PointLight("point light")
        plightnode.setAttenuation(Vec3(1,0,0))
        plight = render.attachNewNode(plightnode)
        plight.setPos(30,-50,0)
        alightnode = AmbientLight("ambient light")
        alightnode.setColor(Vec4(0.8,0.8,0.8,1))
        alight = render.attachNewNode(alightnode)
        render.setLight(alight)
        render.setLight(plight)
        
        #position of the dummy
        def dummyPos(task):
            pos = self.ralph.getPos()
            z = self.ralph.getZ() + 5
            self.dummy.setPos(pos)
            self.dummy.setZ(z)
            return task.cont

        taskMgr.add(dummyPos, "dummyPos")
        
        #the camera is bound to the dummy over the avatars head
        def camPosition(task):
            base.camera.reparentTo(self.dummy)
            base.camera.setPos(0, 25, 5)
            base.camera.lookAt(self.dummy)
            base.camLens.setFar(10000)
            base.disableMouse()
            return task.cont
        
        taskMgr.add(camPosition, "camPosition")

    def setKey(self, key, value):
        self.keyMap[key] = value
        
        #movement of the avatar
        def move(task):
            elapsed = globalClock.getDt()
            if (self.keyMap["left"]!=0):
                self.ralph.setY(self.ralph, -1)
            if (self.keyMap["right"]!=0):
                self.ralph.setY(self.ralph, 1)
            if (self.keyMap["forward"]!=0):
                self.ralph.setY(self.ralph, 1) 
            if (self.keyMap["back"]!=0):
                self.ralph.setY(self.ralph, -1)
            return task.cont

        self.isMoving = False
        
        if (self.keyMap["forward"]!=0) or (self.keyMap["left"]!=0) or (self.keyMap["right"]!=0):
            if self.isMoving is False:
                self.ralph.loop("run")
                self.isMoving = True
        else:
            if self.isMoving:
                self.ralph.stop()
                self.ralph.pose("walk",5)
                self.isMoving = False

avatar = Avatar()
run() 

I know, ralph is “flying” in the air and there is no collision detection implemented. But this shouldn’t be the reason for this problem, right? It is just for testing and i want to go step by step and really understand what i am doing… I already tried to figure out what is wrong, by looking at other codes and the tutorials, but I have no clue…

If you could help me, i would be very happy!

Thanks!

Alex.

You need to add move to the task manager.

taskMgr.add(move,‘move’)

edit: fix capitalization

Check your indenting as well. It looks like everything is defined inside init up to setKey, which is out-dented. What that does is defines move() inside of setKey now, which is probably not where you want it.

Oh, i feel like a dork! This error was so obvious and I did not see it, very embarassing :-/

Thanks for the fast help! And you are right, the indenting is not very clever, I am working on it. Still work in progress.

Alex.