Hi for all
I’m trying to insert the collision detection in my program, but unhapilly it doesn’t works. I’ve followed the tutorials, but even I don’t have success.
Like you’ll notice, the “from” object is my Bee and the into object is the plate object.
I put the code of my 3 very simple classes. Some one can tell me what’s my error?
Thanks a lot for the support guys, this things drives me crazy.
class CollisionDetection(DirectObject.DirectObject):
def __init__(self):
#cria o objeto de evento de colisão para todos os objetos
self.CollisionEvent = CollisionHandlerEvent()
self.CollisionEvent.addInPattern("into-%in")
self.CollisionEvent.addOutPattern("outof-%in")
#Objeto para o evento de colisão com os pratos
self.BeeCollision = CollisionTraverser()
self.PlateCollision = CollisionTraverser()
#Objeto para o evento de colisão da abelha
taskMgr.add(self.RunAll, 'RunAll')
def RunAll(self, task):
self.PlateCollision.traverse(render)
self.BeeCollision.traverse(render)
return Task.cont
class Bee(DirectObject.DirectObject):
flying = uping = downing = righting = lefting = 0
def __init__(self, CollisionObject):
self.Collision = CollisionObject
self.LoadBee(0, 20, 0)
self.AddControllers()
#self.Rotate()
bounds = self.Bee.getChild(0).getBounds()
BeeNodeCollision = CollisionNode('Bee')
BeeNodeCollision.addSolid(CollisionSphere(bounds.getCenter(), bounds.getRadius()*0.5))
self.BeeNodePath = self.Bee.attachNewNode(BeeNodeCollision)
self.BeeNodePath.show()
BeeNodeCollision.setFromCollideMask(BitMask32.bit(1))
BeeNodeCollision.setIntoCollideMask(BitMask32.bit(0))
self.Collision.BeeCollision.addCollider(self.BeeNodePath, self.Collision.CollisionEvent)
self.accept('Bee', self.HandleBee)
taskMgr.add(self.BeeMovement, 'BeeMovement')
def LoadBee(self, x, z, y):
self.Bee = loader.loadModel("airblade.egg")
self.Bee.reparentTo(render)
class Plate(DirectObject.DirectObject):
def __init__(self, IDPlate, CollisionObject):
self.Collision = CollisionObject
self.IDPlate = str(IDPlate)
self.PlateModel = loader.loadModel("airblade.egg")
self.PlateModel.reparentTo(camera)
#Detecção de colisão:
#Cria-se o nodo de colisão
#Cria-se que tipo de forma vai ser a coisão
#Adiciona-se o tipo de colisão
bounds = self.PlateModel.getChild(0).getBounds()
PlateCollision = CollisionNode('Plate')
PlateCollision.addSolid(CollisionSphere(bounds.getCenter(), bounds.getRadius()*0.5))
self.PlatePath = self.PlateModel.attachNewNode(PlateCollision)
self.PlatePath.show()
PlateCollision.setFromCollideMask(BitMask32.bit(0))
PlateCollision.setIntoCollideMask(BitMask32.bit(1))
#Insere o objeto colisor, palavrinha feia viu
self.Collision.PlateCollision.addCollider(self.PlatePath, self.Collision.CollisionEvent)
self.PlateModel.setPos(self.PlateModel.getX()+0.3,self.PlateModel.getY()+10,self.PlateModel.getZ()+0.3)
self.accept('Plate' + self.IDPlate, self.HandleCollision)