(转)Bullet 引擎 详解 碰撞事件 回调函数

在bullet中一个非常普遍的应用是 针对两个物体发生碰撞时调用开发者自己提供的回调函数

比如球体碰撞到另一个球体 发出声音

下面是一个例子, 场景中有3种物体 球(动态),地板(静态),墙壁(静态)

球碰到墙和地板发出的声音不同。

所以需要设置这三种对象为不同类别,利用 collisionobj的userpointer来完成

int objclass[3] ={0,1,2};
boxBody->setUserPointer(&objclass[0]);

ground->setUserPointer(&objclass[1]);

wall->setUserPointer(&objclass[2]);

然后在update or render 中调用如下回调函数

主要的想法是从btPersistentManifold 中去获取当前narrowphase的碰撞对

但是要检查接触点,如果接触点大于0,则开始检查碰撞对象类别

根据类别再做不同的动作。

例子视频  http://www.youtube.com/watch?v=XXEClJjR05w

声音没录好, 等最近两天更新

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"];  
            }             
        }  
    }  
      


本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/superwiles/archive/2010/03/28/5425842.aspx

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值