Little Tutorial on Contact Callbacks (In Progress)

User avatar
Dr.Shepherd
Posts: 168
Joined: Tue Jan 04, 2011 11:47 pm

Little Tutorial on Contact Callbacks (In Progress)

Post by Dr.Shepherd »

Hi, all, this is a small tutorial on Contact Callbacks, that is how to handle the contact when it happens. Definitely bullet has done the default contact callbacks for us, but what if you want your boxes meet each other, stick together but not bounce away.

If you want to control what happens next after the contact, you need to customize your contact callbacks. There is already a tutorial on bullet wiki:
http://bulletphysics.org/mediawiki-1.5. ... d_Triggers

I will only talk about what it does not cover:

Many people would find that the DestroyedCallbacks cannot be called, but the AddedContactCallbacks can be. So check this post:
http://bulletphysics.org/Bullet/phpBB3/ ... f=9&t=2243
Please Follow what Bbilz said in the post to reproduce this "fancy"

The trick lies in userPersistentData, this pointer shouldn't be NULL, so you need to specify a value for it. And pay attention that it is a mutable variable. So set a value as you like:

Code: Select all

int gGlobalValue = 42;

static bool sContactDestroyedCallback( void* userData ) {
   printf( "custom destroyed callback!\n");
   btAssert( userPersistentData == &gGlobalValue );
   return false;
}

static bool sContactAddedCallback( btManifoldPoint& cp, const btCollisionObject* colObj0,int partId0, int index0,const btCollisionObject* colObj1,int partId1,int index1 ) {
   cp.m_userPersistentData = &gGlobalValue;
   printf( "custom added callback!\n" );
   return false;
}
By this way, your destroyed Callbacks can be called.

Even though you make your callbacks working out, and you can control what happens next to collision. The CollisionObject with a custom Contact Callbacks are still possessing the dynamic collision properties as other collision objects, in other words, they are still colliding with each other and bouncing off. You can totally make this original collision disappear by

Code: Select all

Body->setCollisionFlags(mBody->getCollisionFlags() |  btCollisionObject::CF_NO_CONTACT_RESPONSE))
In this way, your box will just go through each other like "Ghost", and nothing happens. The contact only serves as a Trigger to your custom Callbacks, and You can totally control what will happen to them after they contact with each other.

Attached is a revised BasicDemo, so replace it with your original BasicDemo.cpp, then it should be able to make.
You do not have the required permissions to view the files attached to this post.