Retrieving CollisionEntry from CollisionHandlerEvent

Hi,

I have tried figuring out how to obtain the CollisionEntry from a CollisionHandlerEvent in C++, but I have not figured it out. I know how to invoke a collision event using add_hook, but I have no idea how to get the information I need contained inside of a CollisionEntry.

_collider_Ptr->add_in_pattern( "into-%in" );
global::_framework.get_event_handler().add_hook( "into-" + _collider_Ptr->get_name(), &testFunction );

_collider_Ptr is a special class I made up combining the CollisionNode and CollisionHandlerEvent. The code above should be simple enough to illustrate my point though.

I am aware of the other add_hook function with 3 parameters, but I would like to be able to differentiate between a ‘from’ object and ‘into’ object. Currently, when “testFunction” gets called, I have no information about the ‘from’ and ‘into’ NodePaths.

I can always use the CollisionHandlerQueue instead (and get the CollisionEntry), but I would have more flexibility if I can use the CollisionHandlerEvent and obtain the CollisionEntry.

I would appreciate any help, but this matter is not urgent or a big deal to me. Thank you.

Your callback function receives an Event object as its first (or only) parameter.

This Event object itself contains a list of parameters that were passed when the event was thrown. In the case of a collision event, its one parameter is the CollisionEntry.

So, your function should look something like this:

void testFunction(const Event *event) {
  TypedWritableReferenceCount *value = event->get_parameter(0)->get_typed_ref_count_value();
  PT(CollisionEntry) entry = DCAST(CollisionEntry, value);
  nassertv(entry != NULL);

  cerr << "Collision from " << entry->get_from_node_path()
       << " into " << entry->get_into_node_path() << "\n";
}

Thank you, drwr!

It works great! Here is the modified code for those who may run into the same problem:

  1. Changed variable name, “event,” to “e” because “event” is recognized as a keyword in MS Visual Studio 2008 (albeit, “event” is not a standard keyword of C++).
  2. Changed get_typed_ref_count_value() to get_ptr()
void testFunction( const Event * e )
 {
    TypedWritableReferenceCount * value = e->get_parameter(0).get_ptr();
    PT(CollisionEntry) entry = DCAST(CollisionEntry, value); 
    nassertv(entry != NULL);
    
    cerr << "Collision from " << entry->get_from_node_path() 
         << " into " << entry->get_into_node_path() << "\n";
 }