如标题中所述,我正在寻找一种方法来禁用 Matter.js 中的每次 body 碰撞。它应该仍然可以与约束链接,并且应该有可能在一段时间后再次启用碰撞。有没有办法做到这一点?难点在于,该对象不应与任何其他对象发生碰撞,但所有其他对象应相互碰撞。
最佳答案
您可以像这样使用碰撞过滤器:
const body = Matter.Bodies.rectangle(100, 100, 50, 50);
// turns off collisions
body.collisionFilter = {
'group': -1,
'category': 2,
'mask': 0,
};
来自文档:
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.Using the category/mask rules, two bodies
A
andB
collide if each includes the other's category in its mask, i.e. (categoryA & maskB) !== 0
and(categoryB & maskA) !== 0
are both true.
关于JavaScript,Matter.js : Disable collision for one body,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32683832/