Ralph Problem

this works

import direct.directbase.DirectStart
from pandac.PandaModules import CollisionTraverser,CollisionNode
from pandac.PandaModules import CollisionHandlerQueue,CollisionRay
from pandac.PandaModules import Filename
from pandac.PandaModules import PandaNode,NodePath,Camera,TextNode
from pandac.PandaModules import Vec3,Vec4,BitMask32
from direct.actor.Actor import Actor
from direct.task.Task import Task
from pandac.PandaModules import Material,LRotationf,NodePath
from pandac.PandaModules import AmbientLight,DirectionalLight
from pandac.PandaModules import LightAttrib,TextNode
from direct.gui.OnscreenText import OnscreenText
from direct.showbase.DirectObject import DirectObject
from direct.interval.MetaInterval import Sequence,Parallel
from direct.interval.LerpInterval import LerpFunc
from direct.interval.FunctionInterval import Func,Wait
from direct.task.Task import Task
import random, sys, os, math


ACCEL = 70         #Acceleration in ft/sec/sec
MAX_SPEED = 5      #Max speed in ft/sec
MAX_SPEED_SQ = MAX_SPEED ** 2  #Squared to make it easier to use lengthSquared
                               #Instead of length
UP = Vec3(0,0,1)

SPEED = 0.5

# Figure out what directory this program is in.


def addInstructions(pos, msg):
    return OnscreenText(text=msg, style=1, fg=(1,1,1,1),
         pos=(-1.3, pos), align=TextNode.ALeft, scale = .05)

# Function to put title on the screen.
def addTitle(text):
    return OnscreenText(text=text, style=1, fg=(1,1,1,1),
                   pos=(1.3,-0.95), align=TextNode.ARight, scale = .07)

class World(DirectObject):
  def __init__(self):
    self.keyMap = {"left":0, "right":0, "forward":0, "cam-left":0, "cam-right":0}
    base.win.setClearColor(Vec4(0,0,0,1))

        # Post the instructions

    self.title = addTitle("Shaun Keeling: 05007427")
    self.inst1 = addInstructions(0.95, "[ESC]: Quit")
    self.inst2 = addInstructions(0.90, "[Left Arrow]: Rotate Ralph Left")
    self.inst3 = addInstructions(0.85, "[Right Arrow]: Rotate Ralph Right")
    self.inst4 = addInstructions(0.80, "[Up Arrow]: Run Ralph Forward")
    self.inst6 = addInstructions(0.70, "[A]: Rotate Camera Left")
    self.inst7 = addInstructions(0.65, "[S]: Rotate Camera Right")
         
         #Escape quits
      #Place the camera

    #Load the maze and place it in the scene
    self.environ = loader.loadModel("models/world")     
    ralphStartPos = self.environ.find("**/start_point").getPos()
    self.ralph = Actor("models/ralph",
                                 {"run":"models/ralph-run",
                                  "walk":"models/ralph-walk"})
    self.ralph.reparentTo(render)
    self.ralph.setScale(.2)
    self.ralph.setPos(ralphStartPos)

        # Create a floater object.  We use the "floater" as a temporary
        # variable in a variety of calculations.
       
    self.floater = NodePath(PandaNode("floater"))
    self.floater.reparentTo(render)
   
    base.disableMouse()
   
 
    self.keyMap = {"left":0,
                       "right":0,
                       "forward":0,
                       "backward":0,
                       "run":0,
                       "changeView":0,
                       "primFire":0,
                       "secFire":0,
                       "cam-left":0,    # added cam-left and cam-right
                       "cam-right":0}

        # Accept the control keys for movement and rotation
        # (primFire and secFire are not used, but reserved for future use)
    self.accept("escape", sys.exit)
    self.accept("w", self.setKey, ["forward",1])
    self.accept("a", self.setKey, ["left", 1])
    self.accept("s", self.setKey, ["backward", 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, ["backward", 0])
    self.accept("d-up", self.setKey, ["right", 0])
    self.accept("shift", self.setKey, ["run", 1])
    self.accept("shift-up", self.setKey, ["run", 0])
    self.accept("shift-w", self.setKey, ["forward", 1])
    self.accept("shift-a", self.setKey, ["left", 1])
    self.accept("shift-s", self.setKey, ["backward", 1])
    self.accept("shift-d", self.setKey, ["right", 1])
    self.accept("shift-w-up", self.setKey, ["forward", 0])
    self.accept("shift-a-up", self.setKey, ["left", 0])
    self.accept("shift-s-up", self.setKey, ["backward", 0])
    self.accept("shift-d-up", self.setKey, ["right", 0])
    self.accept("mouse2", self.setKey, ["changeView", 1])
    self.accept("mouse2-up", self.setKey, ["changeView", 0])
    self.accept("mouse1", self.setKey, ["primFire", 1])
    self.accept("mouse1-up", self.setKey, ["primFire", 0])
    self.accept("mouse3", self.setKey, ["secFire", 1])
    self.accept("mouse3-up", self.setKey, ["secFire", 0])
    # Added cam-left and cam-right
    self.accept("z", self.setKey, ["cam-left", 1])
    self.accept("z-up", self.setKey, ["cam-left", 0])
    self.accept("x", self.setKey, ["cam-right", 1])
    self.accept("x-up", self.setKey, ["cam-right", 0])        
    # Accept the control keys for movement and rotation

    # Moved taskMgr.add(self.move,"moveTask") & self.prevtime = 0, self.isMoving = False, base.camera.setPos(self.ralph.getX(),self.ralph.getY()+20,15), and collision stuff out of def setKey 
    taskMgr.add(self.move,"moveTask")

        # Game state variables
    self.prevtime = 0
    self.isMoving = False


    base.camera.setPos(self.ralph.getX(),self.ralph.getY()+20,15)

        # We will detect the height of the terrain by creating a collision
        # ray and casting it downward toward the terrain.  One ray will
        # start above ralph's head, and the other will start above the camera.
        # A ray may hit the terrain, or it may hit a rock or a tree.  If it
        # hits the terrain, we can detect the height.  If it hits anything
        # else, we rule that the move is illegal.

    self.cTrav = CollisionTraverser()

    self.ralphGroundRay = CollisionRay()
    self.ralphGroundRay.setOrigin(0,0,1000)
    self.ralphGroundRay.setDirection(0,0,-1)
    self.ralphGroundCol = CollisionNode('ralphRay')
    self.ralphGroundCol.addSolid(self.ralphGroundRay)
    self.ralphGroundCol.setFromCollideMask(BitMask32.bit(0))
    self.ralphGroundCol.setIntoCollideMask(BitMask32.allOff())
    self.ralphGroundColNp = self.ralph.attachNewNode(self.ralphGroundCol)
    self.ralphGroundHandler = CollisionHandlerQueue()
    self.cTrav.addCollider(self.ralphGroundColNp, self.ralphGroundHandler)

    self.camGroundRay = CollisionRay()
    self.camGroundRay.setOrigin(0,0,1000)
    self.camGroundRay.setDirection(0,0,-1)
    self.camGroundCol = CollisionNode('camRay')
    self.camGroundCol.addSolid(self.camGroundRay)
    self.camGroundCol.setFromCollideMask(BitMask32.bit(0))
    self.camGroundCol.setIntoCollideMask(BitMask32.allOff())
    self.camGroundColNp = base.camera.attachNewNode(self.camGroundCol)
    self.camGroundHandler = CollisionHandlerQueue()
    self.cTrav.addCollider(self.camGroundColNp, self.camGroundHandler)

        # Uncomment this line to see the collision rays
        #self.ralphGroundColNp.show()
        #self.camGroundColNp.show()
       
        #Uncomment this line to show a visual representation of the
        #collisions occuring
        #self.cTrav.showCollisions(render)

  def setKey(self, key, value):
      self.keyMap[key] = value
        # Records the state of the arrow keys
   

    # Accepts arrow keys to move either the player or the menu cursor,
    # Also deals with grid checking and collision detection
  def move(self, task):

        elapsed = task.time - self.prevtime

        # If the camera-left key is pressed, move camera left.
        # If the camera-right key is pressed, move camera right.

        base.camera.lookAt(self.ralph)
        camright = base.camera.getNetTransform().getMat().getRow3(0)
        camright.normalize()
        if (self.keyMap["cam-left"]!=0):
            base.camera.setPos(base.camera.getPos() - camright*(elapsed*20))
        if (self.keyMap["cam-right"]!=0):
            base.camera.setPos(base.camera.getPos() + camright*(elapsed*20))

        # save ralph's initial position so that we can restore it,
        # in case he falls off the map or runs into something.

        startpos = self.ralph.getPos()

        # If a move-key is pressed, move ralph in the specified direction.

        if (self.keyMap["left"]!=0):
            self.ralph.setH(self.ralph.getH() + elapsed*300)
        if (self.keyMap["right"]!=0):
            self.ralph.setH(self.ralph.getH() - elapsed*300)
        if (self.keyMap["forward"]!=0):
            backward = self.ralph.getNetTransform().getMat().getRow3(1)
            backward.setZ(0)
            backward.normalize()
            self.ralph.setPos(self.ralph.getPos() - backward*(elapsed*5)) # you can get backwards by + backwards

        # If ralph is moving, loop the run animation.
        # If he is standing still, stop the animation.

        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

        # If the camera is too far from ralph, move it closer.
        # If the camera is too close to ralph, move it farther.

        camvec = self.ralph.getPos() - base.camera.getPos()
        camvec.setZ(0)
        camdist = camvec.length()
        camvec.normalize()
        if (camdist > 10.0):
            base.camera.setPos(base.camera.getPos() + camvec*(camdist-10))
            camdist = 10.0
        if (camdist < 5.0):
            base.camera.setPos(base.camera.getPos() - camvec*(5-camdist))
            camdist = 5.0

        # Now check for collisions.

        self.cTrav.traverse(render)

        # Adjust ralph's Z coordinate.  If ralph's ray hit terrain,
        # update his Z. If it hit anything else, or didn't hit anything, put
        # him back where he was last frame.

        entries = []
        for i in range(self.ralphGroundHandler.getNumEntries()):
            entry = self.ralphGroundHandler.getEntry(i)
            entries.append(entry)
        entries.sort(lambda x,y: cmp(y.getSurfacePoint(render).getZ(),
                                     x.getSurfacePoint(render).getZ()))
        if (len(entries)>0) and (entries[0].getIntoNode().getName() == "terrain"):
            self.ralph.setZ(entries[0].getSurfacePoint(render).getZ())
        else:
            self.ralph.setPos(startpos)

        # Keep the camera at one foot above the terrain,
        # or two feet above ralph, whichever is greater.
       
        entries = []
        for i in range(self.camGroundHandler.getNumEntries()):
            entry = self.camGroundHandler.getEntry(i)
            entries.append(entry)
        entries.sort(lambda x,y: cmp(y.getSurfacePoint(render).getZ(),
                                     x.getSurfacePoint(render).getZ()))
        if (len(entries)>0) and (entries[0].getIntoNode().getName() == "terrain"):
            base.camera.setZ(entries[0].getSurfacePoint(render).getZ()+1.0)
        if (base.camera.getZ() < self.ralph.getZ() + 2.0):
            base.camera.setZ(self.ralph.getZ() + 2.0)
           
        # The camera should look in ralph's direction,
        # but it should also try to stay horizontal, so look at
        # a floater which hovers above ralph's head.
       
        self.floater.setPos(self.ralph.getPos())
        self.floater.setZ(self.ralph.getZ() + 2.0)
        base.camera.lookAt(self.floater)

        # Store the task time and continue.
        self.prevtime = task.time
        return Task.cont


w = World()
run()