matter.js QQ交流群:796728825
collisionFilter属性分析
Collisions between two bodies will obey the following rules:
* - If the two bodies have the same non-zero value of collisionFilter.group
,
* they will always collide if the value is positive, and they will never collide
* if the value is negative.
* - If the two bodies have different values of collisionFilter.group
or if one
* (or both) of the bodies has a value of 0, then the category/mask rules apply as follows:
*
* Each body belongs to a collision category, given by collisionFilter.category
. This
* value is used as a bit field and the category should have only one bit set, meaning that
* the value of this property is a power of two in the range [1, 2^31]. Thus, there are 32
* different collision categories available.
*
* Each body also defines a collision bitmask, given by collisionFilter.mask
which specifies
* the categories it collides with (the value is the bitwise AND value of all these categories).
*
* Using the category/mask rules, two bodies A
and B
collide if each includes the other’s
* category in its mask, i.e. (categoryA & maskB) !== 0
and (categoryB & maskA) !== 0
* are both true.
汉化解释:
collisionFilter有三个属性:group category mask
简单的碰撞规则可以用group解决:
group属性为数字类型。在两个物体的group属性值相同的前提下,如果这个数字为负数,则这两个物体不会碰撞,如果这个数字为正数,则这两个物体会碰撞。
如果两个物体的group属性不相同,或者有一个为0(或两个都为0),则需要根据其他两个属性来判断碰撞规则。
更复杂的碰撞关系:
category属性为16进制数字,默认为0x0001,其值只能为2的幂级数,如0x0002,0x0004,0x0008,(比如,0x0007是不合法的,不会触发碰撞规则)
mask属性设置可以与该物体碰撞的category。举例:
A物体的category为0x0002
,B物体的category为0x0004
C物体若要与A物体碰撞,其mask应为A的category,即0x0002
C物体若要与B物体碰撞,其mask应为B的category,即0x0004
C物体若要与A物体、B物体同时碰撞,其mask应同时包含为A和B的category,即0x0002 | 0x0004
category必须与mask配合使用才有效,因为mask的默认值包含了所有的category,默认可以与所有物体发生碰撞。