Basic movement?

Hi there.

I am very new here, and need some help. I am trying to make a simple character walk around when I press the arrow keys. I have over and over again, but just cannot figure it out. I have done many searches but have not found anything. I have figured out how to load models, actors, animations, and put in lights, now for movement. So can anyone help me with this part?

Thanks.

RrrrrrrrrrrrrrrrrrroamingRalph

I tried that, but I am having some propblems figuring out a few parts. But I just looked over it real quick again, and I think I’m starting to understand it. I’ll look it over some more tomorrow.

Edit: Ok one more quick glance, and I almost got it. One part that I don’t really get is setH and elapsed*300. Could someone explain these a little bit to me?

Thanks

setH sets the heading (= the yaw). This is the direction the player looks into (in degrees).

The script stores in “elapsed” how much seconds have elapsed since last frame. s = v * t, in this case “t” is “elapsed” and “v” (the speed) is 300.

The movement code in Roaming Ralph is not the best, though. I might consider changing it.

Ok thank you far the explanation. Once I get a bit better I’ll see if I can improve on it.

YES PLEASE DO. Especially the part that uses the vector math. That was the most confusing part for e.

Okay, I’ll get it fixed for 1.6.0.

Here its not 100% but here the code to move the camera, or actor if you wish.

    def Controls(self): 
      self.keyMap = {"left":0, "right":0, "forward":0, "back":0,} 
      # Accept the control keys for movement 
      self.accept("escape", sys.exit) 
      self.accept("a", self.setKey, ["left",1]) 
      self.accept("d", self.setKey, ["right",1]) 
      self.accept("w", self.setKey, ["forward",1]) 
      self.accept("s", self.setKey, ["back",1])
      self.accept("a-up", self.setKey, ["left",0]) 
      self.accept("d-up", self.setKey, ["right",0]) 
      self.accept("w-up", self.setKey, ["forward",0]) 
      self.accept("s-up", self.setKey, ["back",0]) 

      taskMgr.add(self.move,"moveTask") 



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

    #keys- 
    def move(self, task): 
      elapsed = task.time - self.prevtime 
      if (self.keyMap["left"]!=0): 
          base.camera.setX(base.camera, - (elapsed*25)) 
      if (self.keyMap["right"]!=0): 
          base.camera.setX(base.camera, + (elapsed*25)) 
      if (self.keyMap["forward"]!=0): 
          base.camera.setY(base.camera, + (elapsed*25)) 
      if (self.keyMap["back"]!=0): 
          base.camera.setY(base.camera, - (elapsed*25)) 

      self.prevtime = task.time 
      return Task.cont

P.S. Just look up some of my stuff if your just starting, I posted some of my code in the past few days. I know i’m not all that good at panda/python but i’m just doing some simple stuff my self and thowing it all togather to make a simple fps style game.

Ok will do. Thanks very much for that code. That really simplifies some stuff.

Edit: So do I have to put that into a class and call it or what? I’m still learning how to really use python.

Show me your code so far?

Well so far all i have is loading some models, textures and animations, and adding a few test characters. So ya. You can tell I’m new from this. I just copied your code for now.

import direct.directbase.DirectStart
from pandac.PandaModules import *

from direct.task import Task
from direct.actor import Actor
from direct.interval.IntervalGlobal import *
import math

Cloth1 = loader.loadTexture("Models/Man cloth.png")
Cloth2 = loader.loadTexture("Models/Man cloth2.png")
Cloth3 = loader.loadTexture("Models/Man cloth3.png")
Cloth4 = loader.loadTexture("Models/Man cloth4.png")
Cloth5 = loader.loadTexture("Models/Man cloth5.png")
Cloth6 = loader.loadTexture("Models/Man cloth6.png")

Sky = loader.loadModel("Models/Skydome")
Sky.reparentTo(render)
Sky.setScale(500,500,500)
Sky.setPos(0,0,-9)

Ground = loader.loadModel("Models/Ground")
Ground.reparentTo(render)
Ground.setScale(500,500,500)
Ground.setPos(0,0,-9.5)

class man:
    def Controls(self): 
      self.keyMap = {"left":0, "right":0, "forward":0, "back":0,} 
      # Accept the control keys for movement 
      self.accept("escape", sys.exit) 
      self.accept("a", self.setKey, ["left",1]) 
      self.accept("d", self.setKey, ["right",1]) 
      self.accept("w", self.setKey, ["forward",1]) 
      self.accept("s", self.setKey, ["back",1]) 
      self.accept("a-up", self.setKey, ["left",0]) 
      self.accept("d-up", self.setKey, ["right",0]) 
      self.accept("w-up", self.setKey, ["forward",0]) 
      self.accept("s-up", self.setKey, ["back",0])
      global x

      taskMgr.add(self.move,"moveTask") 



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

    #keys- 
    def move(self, task): 
      elapsed = task.time - self.prevtime 
      if (self.keyMap["left"]!=0): 
          base.camera.setX(base.camera, - (elapsed*25))  
      if (self.keyMap["right"]!=0): 
          base.camera.setX(base.camera, + (elapsed*25)) 
      if (self.keyMap["forward"]!=0): 
          base.camera.setY(base.camera, + (elapsed*25)) 
      if (self.keyMap["back"]!=0): 
          base.camera.setY(base.camera, - (elapsed*25)) 

      self.prevtime = task.time 
      return Task.cont 

Man = Actor.Actor("Models/Test man",{"Waving":"Models/Test man-Waving"})
Man.reparentTo(render)
Man.loop("Waving")
Man.setTexture(Cloth5,1)

Man2 = Actor.Actor("Models/Test man",{"Trip":"Models/Test man-Trip"})
Man2.reparentTo(render)
Man2.loop("Trip")
Man2.setPos(10,-15,0.5)
Man2.setTexture(Cloth2,1)

Man3 = Actor.Actor("Models/Test man",{"Walking":"Models/Test man-Walking"})
Man3.reparentTo(render)
Man3.loop("Walking")
Man3.setPos(-10,-15, 1)
Man3.setTexture(Cloth3,1)

man = man()
run()

This is where you really need to know Python before diving into the Panda API. You really won’t learn much from putting random code snippets together. If you’re new to Python, and especially programming in general, you need to start smaller.

Here is the outline for how your code should go.

**imports here

class man(DirectObject):
     __init__(self):
          **load all actors, textures etc. and add tasks.

     def Controls(self):

     def SetKey(self, key, value):

     def move(self, task):

m = man()
run()

There should be nothing after your class except initializing the class, and the run() function.

So you need to know:
-how “init” functions work
-what “DirectObject” does for your class
-how to move an object based on its position (using getPos(), getX(), etc.)

Go over some of the samples and try to follow the program logic, and try to figure out why things were done the way they were. Again, it’s quite beneficial to learn Python before you work with Panda - you’re really only making your learning curve worse by trying to do both at once.

Ok thank you for that. I will try to go over some things, and try to learn more how to use python over the next while.

Also here, sorry it took me a while to get this up, with school and all.

#all things you'll need for a while
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 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 pandac.PandaModules import CollisionTraverser,CollisionNode
from pandac.PandaModules import CollisionHandlerQueue,CollisionRay
from direct.showbase.DirectObject import DirectObject

class World(DirectObject):
    def __init__(self):
    #Here we till the code to run the def. code we setup
    self.loadcloth()
    self.loadskyground()
    self.loadmananitmations()
    self.Controls()
    
    
###############################################################################
    #Then we bind the keys and set them up to do 
    def Controls(self):
      self.keyMap = {"left":0, "right":0, "forward":0, "back":0,} 
      # Accept the control keys for movement 
      self.accept("escape", sys.exit) 
      self.accept("a", self.setKey, ["left",1]) 
      self.accept("d", self.setKey, ["right",1]) 
      self.accept("w", self.setKey, ["forward",1]) 
      self.accept("s", self.setKey, ["back",1]) 
      self.accept("a-up", self.setKey, ["left",0]) 
      self.accept("d-up", self.setKey, ["right",0]) 
      self.accept("w-up", self.setKey, ["forward",0]) 
      self.accept("s-up", self.setKey, ["back",0]) 

      taskMgr.add(self.move,"moveTask")    
    
    
###############################################################################    
    def loadcloth(self):    
      Cloth1 = loader.loadTexture("Models/Man cloth.png")
      Cloth2 = loader.loadTexture("Models/Man cloth2.png")
      Cloth3 = loader.loadTexture("Models/Man cloth3.png") 
      Cloth4 = loader.loadTexture("Models/Man cloth4.png")
      Cloth5 = loader.loadTexture("Models/Man cloth5.png") 
      Cloth6 = loader.loadTexture("Models/Man cloth6.png")
      
      
###############################################################################      
    def loadskyground(self):
      Sky = loader.loadModel("Models/Skydome") 
      Sky.reparentTo(render) 
      Sky.setScale(500,500,500) 
      Sky.setPos(0,0,-9) 
      Ground = loader.loadModel("Models/Ground") 
      Ground.reparentTo(render) 
      Ground.setScale(500,500,500) 
      Ground.setPos(0,0,-9.5) 


###############################################################################      
    def loadmananitmations(self):      
      Man = Actor.Actor("Models/Test man",{"Waving":"Models/Test man-Waving"}) 
      Man.reparentTo(render) 
      Man.loop("Waving") 
      Man.setTexture(Cloth5,1) 

      Man2 = Actor.Actor("Models/Test man",{"Trip":"Models/Test man-Trip"}) 
      Man2.reparentTo(render) 
      Man2.loop("Trip") 
      Man2.setPos(10,-15,0.5) 
      Man2.setTexture(Cloth2,1) 

      Man3 = Actor.Actor("Models/Test man",{"Walking":"Models/Test man-Walking"}) 
      Man3.reparentTo(render) 
      Man3.loop("Walking") 
      Man3.setPos(-10,-15, 1) 
      Man3.setTexture(Cloth3,1) 


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

    #keys- 
    def move(self, task): 
      elapsed = task.time - self.prevtime 
      if (self.keyMap["left"]!=0): 
          base.camera.setX(base.camera, - (elapsed*25))  
      if (self.keyMap["right"]!=0): 
          base.camera.setX(base.camera, + (elapsed*25)) 
      if (self.keyMap["forward"]!=0): 
          base.camera.setY(base.camera, + (elapsed*25)) 
      if (self.keyMap["back"]!=0): 
          base.camera.setY(base.camera, - (elapsed*25)) 

      self.prevtime = task.time 
      return Task.cont 

World()
run() 

Should work…

Ok cool thanks for that, but there is one problem. It gives an error saying that World instance has no attribute prevtime. I don’t know what to do from here.

Edit: Ok I got it so that it runs, but it only moves a little bit then moves back when I release the key. Know why?

Another woe of Frankenstein code. You need to add

self.prevtime = 0

to your “init” function.

Yea sorry ^^: Hey not like I tested the code x.x; I just took what he had and wanted and put it togather…

Ok I did that, but it only moves a little bit then goes back when I let go.

The number “25” is your speed in a way, so just up that to move faster, but the goes back when you let go l don’t know.

Well it moves, but only to a point. So it jumps over a little bit, but then stops. So its like its just setting the cameras position instead of adding to it.