Maybe this example will help explain the concept. You have to define a new class, derived from an existing abstract class. You override one of the abstract classes methods with your callback code. You then create an object of the derived class and pass that to the function you want to call the callback. It's a common enough C++ technique.
struct MyContactResultCallback : public ContactResultCallback
{
btScalar addSingleResult(btManifoldPoint& cp,
const btCollisionObjectWrapper* colObj0Wrap,
int partId0,
int index0,
const btCollisionObjectWrapper* colObj1Wrap,
int partId1,
in index1)
{
// your callback code here
}
};
MyContactResultCallback callback;
world.contactPairTest(bodyA, bodyB, callback);
I should add that I nothing whatsoever about this library. I've just read the docs.
EDIT
Showing how to add a context member to MyContactResultCallback
.
struct MyContactResultCallback : public ContactResultCallback
{
MyContactResultCallback(some_type* ptr) : context(ptr) {}
btScalar addSingleResult(btManifoldPoint& cp,
const btCollisionObjectWrapper* colObj0Wrap,
int partId0,
int index0,
const btCollisionObjectWrapper* colObj1Wrap,
int partId1,
in index1)
{
context->currentPoints += 10;
}
some_type* context;
};
MyContactResultCallback callback(ptr_to_some_object);
world.contactPairTest(bodyA, bodyB, callback);
ptr_to_some_object
is the pointer to the object with the currentPoints
that you want to increment. I don't know what type of object that is, so I've just said some_type
, you can replace that with whatever the real type is.
This is the point of using an object as a callback instead of a function. If the callback is an object you can add data members to it for whatever purpose you want, you cannot do that to a function.