原文同步发布于我的wiki,查看原文或更新请移步:
点击打开链接
?
Collision groups let you specify an integral group index.
You can have all fixtures with the same group index always collide
(positive index) or never collide (negative index)
it have high priority than bit masks
PhysicsBody的Group
1.优先级比bit masks的优先级高(CategoryBitmask,ContactTestBitmask,CollisionBitmask)
2.group是正数且相等的话,就一定碰撞
3.group是负数且相等的话,就一定不会碰撞
4.group其他情况没说,应该是取决于bit masks了
A mask that defines which categories this physics body belongs to.
Every physics body in a scene can be assigned to up to 32
different categories, each corresponding to a bit in the bit mask.
You define the mask values used in your game.
In conjunction with the collisionBitMask and contactTestBitMask properties,
you define which physics bodies interact with each other
and when your game is notified of these interactions.
The default value is 0xFFFFFFFF (all bits set).
PhysicsBody的CategoryBitmask
1.CategoryBitmask用来标记这个PhysicsBody的所属类别
2.每个PhysicsBody最多可以属于32个类别,一个bit代表一个类别,0代表不是对应类别,1代表是对应类别
3.和另外两个属性(collisionBitMask and contactTestBitMask)配合着用
4.默认值0xFFFFFFFF,即这个PhysicsBody分别属于所有的32个类别
A mask that defines which categories of bodies cause intersection
notifications with this physics body.
When two bodies share the same space, each body’s category mask is
tested against the other body’s contact mask by performing a logical
AND operation. If either comparison results in a non-zero value,
an PhysicsContact object is created and passed to the physics
world’s delegate. For best performance, only set bits in the
contacts mask for interactions you are interested in.
The default value is 0x00000000 (all bits cleared).
PhysicsBody的ContactTestBitmask
1.ContactTestBitmask用来定义哪个类别的PhysicsBody可以和这个PhysicsBody产生相交通知
2.举个例子:有PhysicsBody A B占了同样的空间了。如何判断A B要产生相交通知呢,
if((A.CategoryBitmask & B.ContactTestBitmask) != 0 || (A.ContactTestBitmask & B.CategoryBitmask) != 0)
{/*条件成立,执行相交通知*/}
else{/*没有相交通知*/}
3.为了性能考虑,相交通知只应该设置给我们感兴趣的物体
4.默认值0x00000000 (all bits cleared),没有哪个类别的PhysicsBody可以产生相交通知
A mask that defines which categories of physics bodies
can collide with this physics body.
When two physics bodies contact each other, a collision may occur.
This body’s collision mask is compared to the other body’s category
mask by performing a logical AND operation. If the result is a
non-zero value, then this body is affected by the collision.
Each body independently chooses whether it wants to be affected
by the other body. For example, you might use this to avoid collision
calculations that would make negligible changes to a body’s velocity.
The default value is 0xFFFFFFFF (all bits set).
PhysicsBody的CollisionBitmask
1.CollisionBitmask是用来定义哪个类别的PhysicsBody可以和这个PhysicsBody产生碰撞
2.举个例子:有PhysicsBody A B彼此接触了,那碰撞就可能发生。
A受不受这个碰撞的影响(体现为物理效果)表达为:
if((A.CollisionBitmask & B.CategoryBitmask) != 0 ){/*A产生碰撞的物理效果*/}
else{/*A没效果*/}
3.至于B受不受这个碰撞的影响,则采用同样的表达式单独计算即可
if((B.CollisionBitmask & A.CategoryBitmask) != 0 ){/*B产生碰撞的物理效果*/}
else{/*B没效果*/}
4.默认值0xFFFFFFFF (all bits set),所有类别的PhysicsBody都能对this body产生碰撞效果
属性值:
group =0
CategoryBitmask =0xFFFFFFFF
ContactTestBitmask=0x00000000
CollisionBitmask =0xFFFFFFFF
(a.CategoryBitmask
& b.CollisionBitmask) != 0
true; 反过来也是; 故a b都有碰撞效果。】((a.CategoryBitmask
& b.ContactTestBitmask) != 0 || (a.ContactTestBitmask & b.CategoryBitmask) != 0)
等于false,故没回调。】属性值:
group =需要设置<-----
CategoryBitmask =0xFFFFFFFF
ContactTestBitmask=0x00000000
CollisionBitmask =0xFFFFFFFF
属性值:
group =0
CategoryBitmask =0xFFFFFFFF
ContactTestBitmask=0x00000001<-----change to this
CollisionBitmask =0xFFFFFFFF
(a.CollisionBitmask
& b.CategoryBitmask) != 0
非0, true;有回调的理由是((a.CategoryBitmask
& b.ContactTestBitmask) != 0 || (a.ContactTestBitmask & b.CategoryBitmask) != 0)
等于true】属性值:
group =0
CategoryBitmask =0xFFFFFFFF
ContactTestBitmask=0x00000001<-----change to this
CollisionBitmask =0x00000000<-----change to this
(a.CollisionBitmask
& b.CategoryBitmask) != 0
等于0,false,;无回调的理由是虽然((a.CategoryBitmask
& b.ContactTestBitmask) != 0|| (a.ContactTestBitmask & b.CategoryBitmask) != 0)
等于true,但是没有碰撞的前提】属性值:
group =0【A的group取-1;B的group取-2】
CategoryBitmask =0xFFFFFFFF
ContactTestBitmask=0xFFFFFFFF【AB都取这个值】
CollisionBitmask =0xFFFFFFFF
((a.CategoryBitmask
& b.ContactTestBitmask) != 0 || (a.ContactTestBitmask & b.CategoryBitmask) != 0)
等于true,但没碰撞的前提,故无回调】(a.CollisionBitmask
& b.CategoryBitmask) != 0
非0,true,b也是;回调理由是有碰撞且((a.CategoryBitmask
& b.ContactTestBitmask) != 0 || (a.ContactTestBitmask & b.CategoryBitmask) != 0)
等于true】属性值:
group =0【AB都保持默认0】
CategoryBitmask =【A的CategoryBitmask=0x01;B的CategoryBitmask=0x02】
ContactTestBitmask=【A的ContactTestBitmask=0x02;B的ContactTestBitmask=0x01】
CollisionBitmask =【A的CollisionBitmask=0x02;B的CollisionBitmask=0x01】
(a.CollisionBitmask
& b.CategoryBitmask) != 0
false; 无回调是((a.CategoryBitmask
& b.CollisionBitmask) != 0 || (a.CollisionBitmask & b.CategoryBitmask) != 0)
等于false故无回调】(a.CollisionBitmask
& b.CategoryBitmask) != 0
true,b也是;回调理由是有碰撞且((a.CategoryBitmask
& b.ContactTestBitmask) != 0 || (a.ContactTestBitmask & b.CategoryBitmask) != 0)
等于true】PhysicsBody a;
PhysicsBody b;
if ((a.group == b.group) && a.group > 0) {
/*能碰撞,有碰撞的物理效果*/
if (((a.CategoryBitmask & b.ContactTestBitmask) != 0)
|| ((a.ContactTestBitmask & b.CategoryBitmask) != 0)) {
/*条件成立,执行相交通知*/
}
} else if ((a.group == b.group) && a.group < 0) {
/*不能碰撞,没有碰撞的物理效果*/
} else {
if ((a.CollisionBitmask & b.CategoryBitmask) != 0) {
/*a能碰撞,a有碰撞的物理效果*/
if (((a.CategoryBitmask & b.ContactTestBitmask) != 0)
|| ((a.ContactTestBitmask & b.CategoryBitmask) != 0)) {
/*条件成立,执行相交通知*/
}
} else {
/*a不能碰撞,a没有碰撞的物理效果*/
}
if ((b.CollisionBitmask & a.CategoryBitmask) != 0) {
/*b能碰撞,b有碰撞的物理效果*/
if (((a.CategoryBitmask & b.ContactTestBitmask) != 0)
|| ((a.ContactTestBitmask & b.CategoryBitmask) != 0)) {
/*条件成立,执行相交通知*/
}
} else {
/*b不能碰撞,b没有碰撞的物理效果*/
}
}
cocos2d-x-3.3-021-碰撞检测3-物理引擎碰撞过滤
原文地址:http://blog.csdn.net/cheyiliu/article/details/43560717