您的位置:首页 > 其它

Box2d 物体互相碰撞的条件

2015-12-19 15:48 330 查看

Box2d 物体互相碰撞的条件

两个物体的刚体属性都不是感应器: fixture.isSensor=false

两个物体的category和mask都是相互对应的

两个物体都是一个groupIndex下的

两个物体至少有一个必须是可动物体 b2_dynamicBody

BOX2D_CategoryBits 和 BOX2D_MaskBits 最多支持16组数据, 所以超出的部分是无法执行碰撞的

如果想侦测碰撞, 必须配置b2ContactListener类侦测

监测碰撞点位置

b2WorldManifold *manifold = new b2WorldManifold();
contact->GetWorldManifold(manifold);
b2Vec2 contactPoint = manifold->points[0];
//记住获得的是绝对点, 所以如果用到内部坐标需要自行转换


判断是否碰撞优先级

groupIndex > maskBit, 两个无任何关系

进行groupIndex判断的条件是:

groupIndex必须相同且不为0, 此时设置任何maskBit无效

groupIndex为正, 则相互碰撞, groupIndex为负则互相不碰撞

自定义碰撞过滤 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;


碰撞注意事项:

box2d的世界中创建和删除对象禁止发生在碰撞过程中

如果想通过碰撞删除body或者fixture, 请标记, 标记删除则在碰撞外执行, 譬如在world的update中监测

参考:

碰撞点位置检测
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  box2d