hey ive been doing the bvw scripting tutorial up to part 12 collision dection but i have a bug
console
C:\Documents and Settings\Ben>DirectStart: Starting the game.
Warning: DirectNotify: category 'Interval' already exists
Known pipe types:
wglGraphicsPipe
(3 aux display modules not yet loaded.)
:util(warning): Adjusting global clock's real time by 0.483396 seconds.
:express(warning): Filename is incorrect case: /c/Panda3D-1.3.2/models/Environme
nt.egg instead of /c/Panda3D-1.3.2/models/environment.egg
:pgraph(warning): Using deprecated LightAttrib interface.
:pgraph(warning): Using deprecated LightAttrib interface.
Traceback (most recent call last):
File "basic.py", line 168, in ?
world = World()
File "basic.py", line 25, in __init__
self.setupCollisions()
File "basic.py", line 151, in setupCollisions
self.cTrav.addCollider(cNode,self.cHandler)
TypeError: CollisionTraverser::add_collider() argument 1 must be NodePath, not C
ollisionNode
script
#Get nodes, modules etc
import direct.directbase.DirectStart
from pandac.PandaModules import *
from direct.showbase.DirectObject import DirectObject
from direct.actor.Actor import Actor
from direct.interval.IntervalGlobal import *
from direct.task import Task
import random
import sys
import math
class World(DirectObject):
# constructor
def __init__(self):
base.disableMouse()
camera.setPosHpr(Vec3(0,-15,7),Vec3(0,-15,0))
taskMgr.add(self.cameraFollowTask,'cameraFollowTask')
self.loadModels()
self.setupLighting()
self.accept("escape",sys.exit)
self.setupCollisions()
# setup intervals
self.pandaWalk = self.panda.posInterval(1.0,Vec3(0,-5,0))
self.pandaBack = self.panda.posInterval(1.0,Vec3(0,5,0))
# setup key controls
self.accept("escape",sys.exit)
self.accept("arrow_up",self.PandaMove)
self.accept("arrow_down",self.PandaBack)
self.accept("arrow_left",self.turn,[-1])
self.accept("arrow_right",self.turn,[1])
# end __init__
def cameraFollowTask(self,task):
camera.lookAt(self.panda,Point3(0,0,4))
return Task.cont
# end cameraFollowTask
def loadModels(self):
# load a model
self.panda = Actor("models/panda-model",{"walk":"models/panda-walk4","eat":"models/panda-eat"})
self.panda.reparentTo(render)
self.panda.setScale(.005)
self.panda.setH(180)
# load targets
self.targets = []
for i in range(5):
target = loader.loadModel("models/Bambooshoot")
target.setScale(.1)
target.setPos(Vec3(random.uniform(-20,20),random.uniform(-15,15),0))
target.reparentTo(render)
self.targets.append(target)
# load an environment
self.environ = loader.loadModel("models/Environment")
self.environ.reparentTo(render)
self.environ.setScale(0.25,0.25,0.25)
self.environ.setPos(-8,42,0)
# end loadModels
def eat(self,cEntry):
self.targets.remove(cEntry.getIntoNodePath().getParent())
cEntry.getIntoNodePath().getParent().remove()
pandaEat = self.panda.actorInterval("eat")
pandaEat.start()
# end eat
def PandaMove(self):
dist = 2.0
angle = self.panda.getH()*math.pi/180.0
dx = dist*math.sin(angle)
dy = dist*-math.cos(angle)
pandaWalk = Parallel(
self.panda.posInterval(1.0,Vec3(self.panda.getX()+dx,self.panda.getY()+dy,0)),
self.panda.actorInterval("walk",loop=1,duration=1.0)
)
pandaWalk.start()
# end PandaMove
def turn(self,dir):
pandaTurn = self.panda.hprInterval(.2,Vec3(self.panda.getH()-(10*dir),0,0))
pandaTurn.start()
# end turn
def PandaBack(self):
dist = -2.0
angle = self.panda.getH()*math.pi/180.0
dx = dist*math.sin(angle)
dy = dist*-math.cos(angle)
pandaWalk = Parallel(
self.panda.posInterval(1.0,Vec3(self.panda.getX()+dx,self.panda.getY()+dy,0)),
self.panda.actorInterval("walk",loop=1,duration=1.0)
)
pandaWalk.start()
# end PandaBack
def setupLighting(self):
# start with a blank light attribute
lightAttribute = LightAttrib.makeAllOff()
# some directional light
dirLight = DirectionalLight('dirLight')
dirLight.setColor(Vec4(0.6,0.6,0.6,1.0))
dirLightNP = render.attachNewNode(dirLight.upcastToPandaNode()) # crashes without upcast
dirLightNP.setPos(Vec3(0.0,-10.0,10.0))
dirLightNP.setHpr(Vec3(0.0,-26.0,0.0))
lightAttribute = lightAttribute.addLight(dirLight) # add to attribute
# add a little extra ambient lighting just because
ambientLight = AmbientLight('ambientLight')
ambientLight.setColor(Vec4(0.25,0.25,0.25,1.0))
ambientLightNP = render.attachNewNode(ambientLight.upcastToPandaNode())
lightAttribute = lightAttribute.addLight(ambientLight)
# apply to world
render.node().setAttrib(lightAttribute)
# end setupLighting
def setupCollisions(self):
# use an event collision handler (sends events on collisions)
self.cHandler = CollisionHandlerEvent()
# set the pattern for the event sent on collision
# "enter" plus the name of the object collided into
self.cHandler.setInPattern("ate-%in")
# make a traverser and make it the default traverser
self.cTrav = CollisionTraverser()
base.cTrav = self.cTrav
# add a sphere around the panda to be collide with bamboo
# get the bounding sphere of the panda
bounds = self.panda.getChild(0).getBounds()
center = bounds.getCenter()
radius = bounds.getRadius()
cSphere = CollisionSphere(center,radius)
cNode = CollisionNode("Panda")
cNode.setIntoCollideMask(BitMask32.allOff())
cNodePath = self.panda.attachNewNode(cNode)
# enable this next line if you want to see the area that the collision sphere covers
cNodePath.show()
# add the Panda's collision node to the traverser as a collider
self.cTrav.addCollider(cNode,self.cHandler)
# add sphere around the bamboos to be collided into
for target in self.targets:
cSphere = CollisionSphere(Point3(0,0,15),15)
cNode = CollisionNode("bamboo")
cNode.addSolid(cSphere)
cNodePath = target.attachNewNode(cNode)
cNodePath.show()
# listen for collisions from the panda into the bamboo
self.accept("ate-bamboo",self.eat)
# end setupCollisions
# end class World
world = World()
run()