Trying to figure out how collision handlers work, so I looked up the Event Example in the manual. But there’s a method in that example called initCollisionSphere, and I can’t seem to find any information about it on Google or in the API reference. It seems to just create a new CollisionSphere with different parameters, but what are those parameters? What does it return? Where do I find more information about this method?
As a side note, could someone give me a rundown on collision handlers? Been trying to figure it out for 3 days and I’m still stuck. All I understand is that traversers can be given objects to detect with addCollider(), and the handler gets a named event with accept() and calls a function when that event is triggered. How is it all connected?
That function is defined further down in the same code snippet.
A very quick runthrough of what you need to set up collisions:
- The traverser needs to exist and needs to have all the “from” object with their respective handler added to it. The traverser needs to be called
traverse(render) on every frame, except if you assign it to
base.cTrav, in which case this happens automatically.
- The handler is what decides what to do when a collision is detected. There are various types of handlers. The CollisionHandlerQueue just adds the collision to a queue for your own code to handle, CollisionHandlerEvent fires an event you can catch, and CollisionHandlerPusher automatically pushes objects back so that they are no longer colliding. Some handlers, like the pusher, require “from” objects to be explicitly added to it as well as to the traverser, so that they know which node to push back.
- The CollisionNodes are the actual objects, that have one or more solids added to them. They are usually parented to the models themselves. Only the “from” objects need to be explicitly added to the traverser (and the handler, in case of the pusher). The collision bitmasks act as a filter indicating which “from” objects can collide with which “into” objects.
To help you debug the collision system visually, you should call
traverser.showCollisions(render) as well as
show() on the NodePaths to your CollisionNodes.