Thanks, those tutorials and some others that I overlooked somehow really helped out. I finally have simple walking around and collision working, so perhaps someone may help me figure out whats going wrong in my script. I use a ray to make sure the character is on the ground, and a sphere for wall collision. The sphere is causing god awful issues when it collides with things, sliding along, throwing me all over the place, glitching out when I approach the corner of a wall etc etc.
filedropper.com/dclfps
from direct.directbase import DirectStart
from direct.showbase.DirectObject import DirectObject
import sys
from pandac.PandaModules import *
class Main(DirectObject):
def __init__(self):
self.accept("escape", sys.exit)
self.MouseSen = 0.1
#Make some light to see
plight = PointLight('plight')
plight.setColor(VBase4(0.2, 0.2, 0.2, 1))
plightNP = render.attachNewNode(plight)
plightNP.setPos(0, 4, 6)
alight = AmbientLight("ambientLight")
alight.setColor(Vec4(.1, .1, .1, 1.0))
alightNP = render.attachNewNode(alight)
render.clearLight()
render.setLight(alightNP)
render.setLight(plightNP)
#Create the world
self.colCount = 0
self.world = loader.loadModel("./NewWorld.egg")
self.world.reparentTo(render)
self.world.setPos(0,0,0)
#Ground collision
cGround = self.world.find("/Ground/groundColl")
cGround.node().setIntoCollideMask(BitMask32.bit(1))
cGround.show()
#Stair Collision
cStairs = self.world.find("/Stairs/StairsCol")
cStairs.node().setIntoCollideMask(BitMask32.bit(1))
cStairs.show()
#Wall collision
cWall = self.world.find("**/Room")
cWall.node().setIntoCollideMask(BitMask32.bit(0))
cWall.show()
#Set up the collision handlers
self.initCollision()
#Create the player model
self.initPlayer()
#Muck the mouse countrols and stuff
props = WindowProperties()
props.setCursorHidden(True)
base.win.requestProperties(props)
base.disableMouse()
def initCollision(self):
"""Setup the collision pushers and traverser"""
#Generic traverser
self.cTrav = CollisionTraverser('Collision Traverser')
#Pusher Handler for walls
self.cPush = CollisionHandlerPusher()
#Queue Handler for the floor
self.cQueue = CollisionHandlerQueue()
def initPlayer(self):
"""Setup the player collision and camera setup"""
#Move us
startPos = self.world.find("**/StartPos").getPos()
base.camera.setPos(startPos + Vec3(-1,-1,5))
#Player varyiables
self.height = 2.0
#Add player controls
self.keyMap = {"forward":0,"backward":0,"strafeleft":0,"straferight":0}
self.accept("w",self.setKey, ["forward",1])
self.accept("s",self.setKey, ["backward",1])
self.accept("a",self.setKey, ["strafeleft",1])
self.accept("d",self.setKey, ["straferight",1])
self.accept("w-up",self.setKey, ["forward",0])
self.accept("s-up",self.setKey, ["backward",0])
self.accept("a-up",self.setKey, ["strafeleft",0])
self.accept("d-up",self.setKey, ["straferight",0])
taskMgr.add(self.move, "moveTask")
#A collision ray for floor detection
self.pGroundRay = CollisionRay()
self.pGroundRay.setOrigin(0,0,10)
self.pGroundRay.setDirection(0,0,-1)
self.pGroundCol = CollisionNode('GroundRay')
self.pGroundCol.addSolid(self.pGroundRay)
self.pGroundCol.setFromCollideMask(BitMask32.bit(1))
self.pGroundCol.setIntoCollideMask(BitMask32.allOff())
self.pGroundColNP = base.camera.attachNewNode(self.pGroundCol)
#Sphere for wall collision
self.pSphere = CollisionSphere(0,0,0,1)
self.pSphereNP = base.camera.attachNewNode(CollisionNode('pSphere'))
self.pSphereNP.node().setFromCollideMask(BitMask32.bit(0))
self.pSphereNP.node().setIntoCollideMask(BitMask32.allOff())
self.pSphereNP.node().addSolid(self.pSphere)
#Putting the nodes to the right collision handlers
self.cTrav.addCollider(self.pGroundColNP,self.cQueue)
self.cPush.addCollider(self.pSphereNP,base.camera)
self.cTrav.addCollider(self.pSphereNP, self.cPush)
def setKey(self, key, value):
self.keyMap[key] = value
def groundColHandler(self, entrys):
newZ = None
for entry in entrys:
Z = entry.getSurfacePoint(render).getZ()
if Z > newZ:
newZ = Z
base.camera.setZ(newZ + self.height)
def wallColHandler(self, entry):
pass
def move(self, task):
elapsed = globalClock.getDt()
if base.mouseWatcherNode.hasMouse():
# get the current location. it's in the range (-1 to 1)
md = base.win.getPointer(0)
x = md.getX()
y = md.getY()
# rotate the camera based on the mouse coordinates
if base.win.movePointer(0, base.win.getXSize()/2, base.win.getYSize()/2):
base.camera.setP((base.camera.getP() - (y-base.win.getYSize()/2)*self.MouseSen) % 360)
base.camera.setH((base.camera.getH() - (x-base.win.getXSize()/2)*self.MouseSen) % 360)
#Move the player around
if (self.keyMap["strafeleft"]!=0):
base.camera.setX(base.cam, -(elapsed*4))
if (self.keyMap["straferight"]!=0):
base.camera.setX(base.cam, (elapsed*4))
if (self.keyMap["forward"]!=0):
base.camera.setY(base.cam, (elapsed*5))
if (self.keyMap["backward"]!=0):
base.camera.setY(base.cam, -(elapsed*3))
self.cTrav.traverse(render)
groundCol = []
for entry in self.cQueue.getEntries():
name = entry.getIntoNode().getTag("col_type")
if name == "ground_col": groundCol.append(entry)
else: self.wallColHandler(entry)
self.groundColHandler(groundCol)
return task.cont
if __name__ == '__main__':
w = Main()
run()
If I left out any pertinent information, let me know, I really just want a nice collision system to expand upon.