A very common application in bullet is to call the callback function provided by the developer when two objects collide.
For example, if a sphere hits another sphere to make a sound
The following is an example of three types of objects in a scenario: ball (dynamic), floor (static), and wall (static)
The sound of the ball hitting the wall is different from that of the floor.
Therefore, you need to set these three objects to different categories and use the userpointer of collisionobj to complete
Int objclass [3] = {0, 1, 2 };
Boxbody-> setuserpointer (& objclass [0]);
Ground-> setuserpointer (& objclass [1]);
Wall-> setuserpointer (& objclass [2]);
Then, call the following callback function in update or render:
The main idea is to get the current narrowphase collision pair from btpersistentmanifold.
However, check the contact point. If the contact point is greater than 0, check the collision object category.
Perform different actions based on the category.
Example video http://www.youtube.com/watch? V = xxecljjr05w
Sound not recorded, wait for the last two days to update
View plaincopy to clipboardprint?
-(Void) collide_callback {
Int type_obj1, type_obj2;
Int nummanifolds = sdynamicsworld-> getdispatcher ()-> getnummanifolds ();
For (INT I = 0; I <nummanifolds; I ++)
{
Btpersistentmanifold * contactmanifold = sdynamicsworld-> getdispatcher ()-> getmanifoldbyindexinternal (I );
Int numcontacts = contactmanifold-> getnumcontacts ();
If (numcontacts> 0)
{
Btcollisionobject * Oba = static_cast <btcollisionobject *> (contactmanifold-> getbody0 ());
Btcollisionobject * OBB = static_cast <btcollisionobject *> (contactmanifold-> getbody1 ());
Type_obj1 = * (int *) Oba-> getuserpointer ();
Type_obj2 = * (int *) OBB-> getuserpointer ();
If (type_obj1 = 0) & (type_obj2 = 1 ))
{
[G_resmanager playsound: @ "test. caf"];
}
If (type_obj1 = 0) & (type_obj2 = 2 ))
{
[G_resmanager playsound: @ "test2.caf"];
}
}
}
}
This article from the csdn blog, reproduced please indicate the source: http://blog.csdn.net/superwiles/archive/2010/03/28/5425842.aspx