Help with Collision Event Handler


I have been looking at the example of using the collision event handler in Panda3d. For my project, I would love to be able to have every collision fire an event and then assign and allow the necessary object(s) to handle each message appropriately and perform the appropriate action for that collision.

I would like each game object to contain a ‘handleCollision’ function that is called when the appropriate event is active. (like below)

        # Accept the events sent by the collisions.
        self.accept('into-someGameObject', self.collide)
        self.accept('outof-SomeOtherGameObject', self.collide2)

However, I would like to have only one function for each object that handles ALL collision events that pertains to a specific game object, and thus need a unique way to extract the collision ‘description’ when the method is called. This would allow for a bit of encapsulation within existing game objects.

Is there a way to either extract the descriptions (in the example above, ‘into-someGameObject’/‘outof-SomeOtherGameObject’) from a given collision entry, or pass an additional argument that?

On another note, is it possible, or a good idea to use events for player-terrain collisions? The little bit I have experimented, it seems like I only receive one collision event when the player initially ‘collides’ with the terrain.

Will the event handler, continuously fire ‘player/terrain’ collision events, or is this where the ‘again’ pattern would need to be implemented?

Thanks and sorry for the lengthy/wordy post.


Your collision handler function receives one parameter, a CollisionEntry, which contains all of the pertinent information about the collision, including the from and to nodes. So you can vector all collision events into one function that then examines the CollisionEntry to decide what it should do.

If you want to get continued notifications of an ongoing collision, that is the purpose of the “again” event. When two objects come into contact, the system sends the “in” event. Each frame thereafter, as long as they remain in contact, the system sends the “again” event, but not the “in” event. The first frame they are no longer in contact, the system sends the “out” event.