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中监测
参考:
碰撞点位置检测相关文章推荐
- Android下2d物理引擎Box2d用法简单实例
- Unity3D上路_01-2D太空射击游戏
- Unity3D上路_02-第一视角射击游戏
- Unity3D上路_03-塔防游戏
- Unity3D上路_04-基础资源介绍
- Unity3D上路_05-网络相关
- LevelHelper Cocos2d-x with Box2d(一)自定义精灵
- 碰撞过滤
- Cocos2d-x加Box2D制作弹弓类游戏
- Box2D在VS2010上面的配置
- VertexHelper使用细节
- Cocos2d-x unresolved external symbol "public: __thiscall GLESDebugDraw::GLESDebugDraw(float)
- [ IOS-Cocos2d-x (Box2d)游戏开发之一] -简单创建一个Box2D
- [ IOS-Cocos2d-x (Box2d)游戏开发之二] -Box2D如何开启debugDraw
- [ IOS-Cocos2d-x (Box2d)游戏开发之三] -Cocos2d-x 结合Box2D开发Android游戏配置方法
- [ IOS-Cocos2d-x (Box2d)游戏开发之四] -Cocos2d-x for android 使用Box2d报错解决
- COCOS2DX-BOX2D游戏开发 Box2d概念详解
- cocos2d-x 2.14 运行 PhysicsEditor Demo(更新GB2ShapeCache-x)
- 一,开篇,一个完整的LibGDX小游戏
- 四,主要菜单界面的创建