Box2d 物体互相碰撞的条件

来源:互联网 发布:vivo手机库存软件 编辑:程序博客网 时间:2024/05/16 11:40

Box2d 物体互相碰撞的条件

  1. 两个物体的刚体属性都不是感应器: fixture.isSensor=false
  2. 两个物体的category和mask都是相互对应的
  3. 两个物体都是一个groupIndex下的
  4. 两个物体至少有一个必须是可动物体 b2_dynamicBody
  5. BOX2D_CategoryBits 和 BOX2D_MaskBits 最多支持16组数据, 所以超出的部分是无法执行碰撞的
  6. 如果想侦测碰撞, 必须配置b2ContactListener类侦测
  7. 监测碰撞点位置
b2WorldManifold *manifold = new b2WorldManifold();contact->GetWorldManifold(manifold);b2Vec2 contactPoint = manifold->points[0];//记住获得的是绝对点, 所以如果用到内部坐标需要自行转换

判断是否碰撞优先级

groupIndex > maskBit, 两个无任何关系
进行groupIndex判断的条件是:

  1. groupIndex必须相同且不为0, 此时设置任何maskBit无效
  2. groupIndex为正, 则相互碰撞, groupIndex为负则互相不碰撞
  3. 自定义碰撞过滤 Custom b2ContactFilter, 自定义碰撞过滤可通过重写 b2ContactFilter 来修改已知检测碰撞效果, 比如把 groupIndex 和 maskBits 揉捏在一起来实现更多种类的碰撞

可以从代码中明显看到:

if (filterA.groupIndex == filterB.groupIndex && filterA.groupIndex != 0) {     return filterA.groupIndex > 0; } bool collide = (filterA.maskBits & filterB.categoryBits) != 0 && (filterA.categoryBits & filterB.maskBits) != 0; return collide; 

碰撞注意事项:

  1. box2d的世界中创建和删除对象禁止发生在碰撞过程中
  2. 如果想通过碰撞删除body或者fixture, 请标记, 标记删除则在碰撞外执行, 譬如在world的update中监测

参考:

碰撞点位置检测

0 0