Collision detection problem

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)

Hello,

At first glance, the issue may be that you use 2 different traversers for Bee and Plate.

Object can only collide within the traverser they are declared in.

So for Bee and Plate to collide (if their collision mask are compatible), they need to be in the same traverser .
ex BeePlateTraverser.

Using several traverser is usefull when you manage performance or want to manage objects that cannot collide together …

well, it doesn’t works.
I put them in one collisiokn traverser and nothing occurs

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.Collisions = CollisionTraverser()
      taskMgr.add(self.RunAll, 'RunAll')
   def RunAll(self, task):
      self.Collisions.traverse(render)
      return Task.cont

When you say “So for Bee and Plate to collide (if their collision mask are compatible)”. would you mind to explain a little more for me? I’m very, very new in panda, and I really don’t understand very well this topic in the docs.

Again thanks

Hello,

Some quick additional Checks:

A) you need to register your “colliding” object into the Traverser
here: YourCollisionTraverser.addCollider(yourBeeNodePath,YourCollisionHandler )

Note: add only your moving object to the traverser.
All other objects are by default “into” object and the traverser will detect
collision with them.

Note2: if you add this
yourBeenodePath.node().setFromCollideMask(GeomNode.getDefaultCollideMask())
then all collisions between your Bee and every visible Node in the Scene will be detected.

B) you need to register a function to treat the collision events if you want to see them somewhere
eg: add to your main object

self.accept('Bee-into-Plate', handleBeePlateCollision)

def handleBeePlateCollision(self, entry):
print entry

Entry is an object that knows at least the colliding object and the collided object when a collision occurs:
entry.getFromNodePath()
entry.getIntoNodePath()

Here getFrom… should return the nodePath to your Bee
getInto… should return the nodePath to your Plate

Hope this can help you a bit further
(it may not be 100% exhaustive because i have no access to panda right now)

Hello, Manakel;

Anywho I was wondering if i could ask you something. In one of your post above you said that C#er had his objects in two different traverser, i was wondering if you could explain that a little bit. I would find it very helpful if you did, since am currently trying to code a game, pacman like, and i only want the main character to collide with an object, but as things are now all actors collide with objects. I know i need to put them in different traverser but i don’t know how.

you are right shabooboo.
Can you explain for us Manakel?
Thaks for your support.