Panda3D Manual: Collision HandlersYou will need to create a CollisionHandler that specifies what to do when a collision event is detected. Each object can only have one collision handler associated with it. There are several possible kinds of CollisionHandler available. CollisionHandlerQueueThe simplest kind of CollisionHandler, this object simply records the collisions that were detected during the most recent traversal. You can then iterate through the list using
queue = new CollisionHandlerQueue();
By default, the Collision Entries appear in the queue in no particular order. You can arrange them in order from nearest to furthest by calling CollisionHandlerEventThis is another simple kind of CollisionHandler. Rather than saving up the collisions, it generates a Panda event when collision events are detected. There are three kinds of events that may be generated: the "in" event, when a particular object collides with another object that it didn't in the previous pass, the "out" event, when an object is no longer colliding with an object it collided with in the previous pass, and the "again" event, when an object is still colliding with the same object that it did in the previous pass. For each kind of event, the CollisionHandlerEvent will construct an event name out of the names of the from and into objects that were involved in the collision. The exact event name is controlled by a pattern string that you specify. For instance:
C_handler.add_in_pattern("%fn-into-%in");
In the pattern string, the following sequences have special meaning:
You may use as many of the above sequences as you like, or none, in the pattern string. In the tag-based sequences, the parentheses around (tag) are literal; the idea is to write the name of the tag you want to look up, surrounded by parentheses. The tag is consulted using the In any case, the event handler function that you write to service the event should receive one parameter (in addition to self, if it is a method): the CollisionEntry. For example: Note that all of the following versions of CollisionHandler also inherit from CollisionHandlerEvent, so any of them can be set up to throw events in the same way. CollisionHandlerPusherThis is the first of the more sophisticated handlers. The CollisionHandlerPusher, in addition to inheriting all of the event logic from CollisionHandlerEvent, will automatically push back on its from object to keep it out of walls. The visual effect is that your object will simply stop moving when it reaches a wall if it hits the wall head-on, or it will slide along the wall smoothly if it strikes the wall at an angle. The CollisionHandlerPusher needs to have a handle to the NodePath that it will push back on, for each from object; you pass this information to
smiley = window->load_model(framework.get_models(),"smiley.egg");
CollisionTraverser traverser.add_collider(fromObject,pusher);
If you are using Panda's drive mode to move the camera around (or some other node), then you also need to tell the pusher about the drive node, by adding it into the
fromObject = cam.attach_new_node(CollisionNode("colNode"))
PhysicsCollisionHandlerThis kind of handler further specializes CollisionHandlerPusher to integrate with Panda's Physics Engine. It requires that the NodePath you pass as the second parameter to
anp = window->get_render().attach_new_node(ActorNode("actor"));
Whenever you have an ActorNode that you want to respond to collisions, we recommend that you use a PhysicsCollisionHandler rather than an ordinary CollisionHandlerPusher. The PhysicsCollisionHandler will keep the object out of walls, just like the CollisionHandlerPusher does, but it will also update the object's velocity within the physics engine, which helps to prevent the physics system from becoming unstable due to large accumulated velocities. CollisionHandlerFloorThis collision handler is designed to serve one very specialized purpose: it keeps an object on the ground, or falling gently onto the ground, even if the floor is not level, without involving physics. It is intended to be used with a Using the CollisionHandlerFloor can be an easy way to simulate an avatar walking over uneven terrain, without having to set up a complicated physics simulation (or involve physics in any way). Of course, it does have its limitations.
smiley = window->load_model(framework.get_models(), "smiley.egg");
© Carnegie Mellon University 2010 |