Hi,
I guess you do not ask you anymore longer question (2 years after: p) but maybe some visitors like me will ask this question.
I'm beginner with Bullet and now I wanted to use as callbacks. It is true that bullet lacks some tutorials and examples (but it is our job to write it ;-), but I find that the source code of the engine is very clear.
Here is my conclusion from the Doxygen documentation:
gContactDestroyedCallback, typedef:
Code: Select all
typedef bool (* ContactDestroyedCallback) (
userPersistentData void *);
receives "userPersitentData" parameter. This is your user data. It will especially allow you, for example, to find the two objets that were in contact in your gContactDestroyedCallback. So yes, userPersitentData must be non-null. Else gContactDestroyedCallback is considered useless.
Regarding your code:
Code: Select all
p1 = colObj0->getCollisionShape()->parentID; //Custom variables in btCollisionShape to store parent object ID for my class
p2 = colObj1->getCollisionShape()->parentID;
cp.parentID0 = p1; //Custom variables in btManifoldPoint created by me to store index of colliding objects
cp.parentID1 = p2; //for that particular point
Here you wish to find the class that owns your object.
You do not need to create these custom variables, Bullet already contains everything you need so that you can find your personal data.
The btCollisionShape may contain user data using get / setUserPointer.
Example usage to find parent class:
Code: Select all
class MyObjectClass
{
public:
uint numContacts; // Your numContacts variable
// Somewhere where you create your collision shape
void Example()
{
myCollisionShape->setUserPointer(this); // Add Just this line in your object class
}
};
bool sContactAddedCallback(btManifoldPoint& cp, const btCollisionObject* colObj0, int partId0,int index0,const btCollisionObject* colObj1,int partId1,int index1)
{
MyObjectClass* physObject = static_cast<MyObjectClass*>(colObj0->getCollisionShape()->getUserPointer());
physObject->numContacts++;
cp.m_userPersistentData = physObject;
return false; // "If your function returns false, then Bullet will assume that you did not modify the contact point properties at all."
}
bool sContactDestroyedCallback(void* userPersistentData)
{
MyObjectClass* physObject = static_cast<MyObjectClass*>(userPersistentData);
physObject->numContacts--;
return false;
}
PS: sorry for my English language skills .... I'm french ^ ^