Im using ColiisionHandlerQueue() to get some simple collision detection going as this is my first project with collision detection. And so far its failed, HORRIBLY lol. after hours of trying to get just simple “object hit other object” print statements i got nothing. Then finally i got this code
import direct.directbase.DirectStart
from pandac.PandaModules import *
from direct.gui.OnscreenText import OnscreenText
from pandac.PandaModules import WindowProperties
import sys
class Game(object):
speed = 250 # controls the speed of the ship
FORWARD = Vec3(0, 2, 0) # sets direction and speed of forward direction
STOP = Vec3(0, 0, 0)# sets the speed and direction of stop
walk = STOP # makes walk = stop
def __init__(self):
base.disableMouse() # disables the default mouse camera controls
base.accept( "escape" , sys.exit) # quits when escape is pressed
self.addWinProps()
self.addlight() # load the light
self.loadShip() #calls up the function to load the ship
self.loadAI()
self.loadWorld()
self.createControls()
self.setCamera()
self.initCollisions()
taskMgr.add(self.updatePos, 'update position')
taskMgr.add(self.mouseUpdate, 'update mouse')
def addlight(self): #create the light
#create a directional light to give a sense of 3d ness
dlight = DirectionalLight('dlight')
dlight.setColor(VBase4(0.8, 0.8, 0.8, 1))
dlight.setShadowCaster(True)
dlnp = render.attachNewNode(dlight)
dlnp.setHpr(0, -60, 0)
render.setLight(dlnp)
render.setShaderAuto()
def addWinProps(self):
#defines the windows properties aka title and hide cursor etc
props = WindowProperties()
props.setCursorHidden(True)
props.setTitle('Space Fighter')
base.win.requestProperties(props)
def loadShip(self): # just loads the ship model
self.ship = loader.loadModel('ship')
self.ship.reparentTo(render)
self.ship.setTwoSided(True)
self.ship.setScale(.5)
def loadAI(self):
self.ai = loader.loadModel('ai')
self.ai.reparentTo(render)
self.ai.setTwoSided(True)
self.ai.setScale (.5)
def initCollisions(self): # init collisions
base.cTrav = CollisionTraverser() #create traverser
self.colHand = CollisionHandlerQueue()
cn = CollisionNode('player')
cn.addSolid(CollisionSphere(0,0,0,3))
solid = self.ship.attachNewNode(cn)
solid.show()
cd = CollisionNode('ai')
cd.addSolid(CollisionSphere(0,0,0,3))
solidd = self.ai.attachNewNode(cd)
solidd.show()
base.cTrav.addCollider(solid, self.colHand)
base.cTrav.traverse(render)
for i in range(self.colHand.getNumEntries()):
entry = self.colHand.getEntry(i)
print entry
def loadWorld(self): # loads world
self.world = loader.loadModel('level')
self.world.reparentTo(render)
self.world.setPos(-1, 30, 3)
self.world.setScale(80)
def setCamera(self): # sets the camera lense
place = base.cam.node().getLens()
place.setFov(70)
base.cam.node().setLens(place)
base.camera.reparentTo(self.ship)
base.camera.setY(-30)
base.camera.setZ(5)
def mouseUpdate(self,task): # updates the mouse and changes ships P and H accordingly
md = base.win.getPointer(0)
x = md.getX()
y = md.getY()
if base.win.movePointer(0, base.win.getXSize()/2, base.win.getYSize()/2):
self.ship.setP(self.ship.getP() - (y - base.win.getYSize()/2)*0.1)
self.ship.setH(self.ship.getH() - (x - base.win.getXSize()/2)*0.1)
return task.cont
def createControls(self): # changes the value of walk to either STOP or Forward depending on the keypress
base.accept( "w" , self.__setattr__,["walk",self.FORWARD])
base.accept( "w-up" , self.__setattr__,["walk",self.STOP] )
def updatePos(self,task): # task to move the ship and camera
self.ship.setPos(self.ship,self.walk*globalClock.getDt()*self.speed)
return task.cont
Game()
run()
and it gives me a TON of cillision info when i load up the game but then it doesnt record any of my collisions with the world or ai ship. I just want to be able to detect when my ship (and later a simple bullet) is colliding with another ship. Any suggestions? heres a screenie of my overloaded console when i load up my game ( the game lags too =/)