Configuring a Collision Detection Query

If the cdet_collided_faces structure configuration options are insufficient, then C3D geometry core user can create a custom query. To do this, you need to define the cdet_query class inheritor and implement for it a special call-back function of the following type:

typedef cback_res (*cback_func)(cdet_query *, message, cback_data & ).  

Below you can find C++ example code for cdet_query_result structure that inherits the cdet_query query structure. The example works until the first intersection is found and it determines whether there is at least one intersection in the control body set. You can find the example in cdet_data.h file.

//------------------------------------------------------------
struct cdet_query_result: public cdet_query
{
cdet_result result;
cdet_query_result()
    : cdet_query(QueryFunc)
    , result(CDM_RESULT_NoCollisionDetected) {} 
private:
static cback_res QueryFunc(cdet_query * query, message code, cback_data &)
{
    cdet_query_result * q = static_cast<cdet_query_result*>(query);
    switch( code )
    {
        case CDET_QUERY_STARTED: // The collision query is started for all solids of the set
        {
            q->result = CDM_RESULT_NoCollisionDetected;
            return CBACK_VOID_RESULT;
        }
        case CDET_INTERSECTED: // First intersection is founded.
        {
            q->result = CDM_RESULT_CollisionDetected;
            return CBACK_SUFFICIENT;
        }
        case CDET_FINISHED: // A pair of solids is finished.
        return (q->result == CDM_RESULT_CollisionDetected) ? CBACK_BREAK:CBACK_VOID_RESULT;
        default:
        return CBACK_VOID;
        }
    }
};

The query is based on the call-back function

static cback_res QueryFunc(cdet_query * query, message code, cback_data & cData),   

the user can implement this function in any way he/she likes. This function is passed to the CheckCollisions algorithm via the cdet_query structure and it can be called by one of the following events (enum cdet_query::message):

  • CDET_QUERY_STARTED (The collision detection started in the control body group),

  • CDET_STARTED (The collision detection started for object pair),

  • CDET_FINISHED (The collision detection for object pair was ended),

  • CDET_INTERSECTED (The intersection of faces for a pair of objects was found),

  • CDET_TOUCHED (The contacting faces for a pair of objects was detected).

The QueryFunc function has the following input parameters:

  • query is a pointer to the query structure,

  • code is an integer encoding the detection event,

  • cData is a data pass structure.

The function returns one of the cback_res enumeration values:

  • CBACK_SUFFICIENT (Client application response to the CDET_INTERSECTED or CDET_TOUCHED events. The search is interrupted for this pair of bodies, but is continued for other pairs in the control body set),

  • CBACK_SKIP (Skipping the detection for this body based on CDET_STARTED event: the detection for other pairs in the set will be continued),

  • CBACK_BREAK (Interrupts further intersection detection for all events),

  • CBACK_VOID (Does not impact the collision detection query and continues the detection), this enumeration value is passed to the C3D core by the client application to configure detection algorithm.

code is an integer that defines the detection event, it takes one of the following values:

  • CDET_QUERY_STARTED,

  • CDET_STARTED,

  • CDET_FINISHED,

  • CDET_INTERSECTED,

  • CDET_TOUCHED.

The cData data structure is used to pass the cdet_query::geom_element, it should have the following form:

struct cback_data
{
geom_element first, second;
};  

where an element of cdet_query::geom_element pair is a detection feature structure, it contains the following components:

  • geom_element::appItem is a geometric object in the client application,

  • geom_element::refItem is a MbRefItem type geometric object, usually it is MbFace face,

  • geom_element::wMatrix is a matrix that defines the conversion from the local CS of the body to the general global CS.

Various implementations of the QueryFunc call-back method permit to use the following detection process configuration methods:

  • Collect all face pairs that take part in collisions of the bodies;

  • Remove certain bodies or pairs of bodies from collision analysis at any moment;

  • Distinguish between face tangent case and face intersection case;

  • Interrupt the detection in case of a certain result;

  • Interrupt the detection if tangency or intersection is found;

  • Group bodies to avoid detection of intersections within the group.