matter.js技术 | collisionFilter 设置碰撞规则

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,默认可以与所有物体发生碰撞。

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值