hello all, ive been trying to get the correct collisions for what i need to work. I have a pusher working but i dont want or need a pusher. All i want is a print statement during every collision describing what object collided with what. heres the pusher code.
import direct.directbase.DirectStart
from pandac.PandaModules import *
from direct.gui.OnscreenText import OnscreenText
from pandac.PandaModules import WindowProperties
from pandac.PandaModules import CollisionTraverser, CollisionHandlerEvent
from pandac.PandaModules import CollisionNode, CollisionSphere
from direct.showbase.ShowBase import ShowBase
import sys
#this class has colisions with pushers and regular colliders
class Game(ShowBase, object):
speed = .3 # controls the speed of the ship
ammospeed = 8
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
collision = False #True if somthing is colliding
life = 10
def __init__(self):
base.disableMouse() # disables the default mouse camera controls
base.accept( "escape" , sys.exit) # quits when escape is pressed
self.collisionsHandler()#This should be before ANY node loading/setting
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.shoot()
taskMgr.add(self.updateShipPos, 'update position')
taskMgr.add(self.mouseUpdate, 'update mouse')
def collisionsHandler(self):
# Initialize the traverser.
base.cTrav = CollisionTraverser()
# Initialize the handler.
self.pusher = CollisionHandlerEvent()
self.pusher.addInPattern('into-%in')
self.pusher.addOutPattern('outof-%in')
# Make a variable to store the unique collision string count.
self.collCount = 0
def addCollision(self, model):
# Setup a collision solid for this model.
pushColl = self.initCollisionSphere(model, .7, False)#List variable for pusher
pushaColl = pushColl[0]#Non-list varible for the pusher
# Add this object to the traverser.
# Accept the events sent by the collisions.
self.accept('into-' + pushColl[1], self.collideIn)
self.accept('outof-' + pushColl[1], self.collideOut)
self.pusher.addCollider(pushaColl, model, base.drive.node())
base.cTrav.addCollider(pushaColl, self.pusher)
print(pushColl[1])
def collideIn(self, collEntry):
print("Explosion!")
self.life -= 1
print self.life
self.collision = True
def collideOut(self, collEntry):
print("Collision ended")
self.collision = False
def initCollisionSphere(self, obj, sizeMult, show=False):
# Get the size of the object for the collision sphere.
bounds = obj.getChild(0).getBounds()
center = bounds.getCenter()
radius = bounds.getRadius() * sizeMult
# Create a collision sphere and name it something understandable.
collSphereStr = 'CollisionHull' + str(self.collCount) + "_" + obj.getName()
self.collCount += 1
cNode = CollisionNode(collSphereStr)
cNode.addSolid(CollisionSphere(center, radius))
cNodepath = obj.attachNewNode(cNode)
if show:
cNodepath.show()#Shows the collision model
print collSphereStr
# Return a tuple with the collision node and its corrsponding string so
# that the bitmask can be set.
return (cNodepath, collSphereStr)
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)
self.addCollision(self.ship)#adds a collision/pusher to the ship model
def loadAI(self):
self.ai = loader.loadModel('ai')
self.ai.reparentTo(render)
self.ai.setTwoSided(True)
self.ai.setScale (.5)
self.addCollision(self.ai)#adds a collision/pusher to the ai model
def shoot(self):
self.accept('mouse1', self.loadAmmo)
self.accept('mouse3', self.loadAmmo)
def loadAmmo(self):
nodepath = NodePath("nodepath")
nodepath.reparentTo(render)
self.ammo = loader.loadModel('bullet')
self.ammo.reparentTo(nodepath)
self.ammo.setScale(.2)
self.ammo.setPos(self.ship.getPos())
ammo = nodepath.getChild(0)
ammoH = self.ship.getH()
ammoP = self.ship.getP()
ammoR = self.ship.getR()
self.ammo.setHpr(self.ammo, ammoH, ammoP, ammoR)
self.addCollision(ammo)
taskMgr.add(self.updateAmmoPos, 'MyTaskName', extraArgs=[ammo], appendTask=True)
def loadWorld(self): # loads world
self.world = loader.loadModel('level')
self.world.reparentTo(render)
self.world.setPos(0, 0, -300)
self.world.setScale(80)
#self.addCollision(self.world)#adds a collision/pusher to the world model. commented out cause 'you dont need world collisions' :P
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 updateShipPos(self,task): # task to move the ship and camera
self.ship.setPos(self.ship,self.walk*self.speed)
if self.life == 0:
self.ai.removeNode()
return task.cont
def updateAmmoPos(self, ammo ,task):
ammo.setPos(ammo,self.FORWARD*self.ammospeed)
return task.cont
Game()
run()
ive tried collisionhandlerqueue() and event() with no success. could anyone poine me in the right direction?