如何更改对象的碰撞过滤器,使其不再与 MouseConstraint 交互(MatterJS)

How can I change the collisionFilter of an object so it can no longer interact with MouseConstraint (MatterJS)

我正在处理 Slingshot 演示。问题是石头发射后还是可以点击拖拽,我想禁用

我给岩石添加了一个过滤器:

var rockOptions = {
  density: 0.004,
  restitution: 0.75,
  collisionFilter: { mask: SOLID, category: NEXTBALL }
};

鼠标约束:

var mouse = Mouse.create(render.canvas),
  mouseConstraint = MouseConstraint.create(engine, {
    mouse: mouse,
    collisionFilter: { category: NEXTBALL },
    constraint: {
      stiffness: 0.2,
      render: {
        visible: true
      }
    }
  });

然后在单击事件中我尝试更改该过滤器,因此它不应再匹配鼠标类别:

Events.on(engine, "afterUpdate", function () {
  if (
    mouseConstraint.mouse.button === -1 &&
    (rock.position.x > shootPosition.x + 20 ||
      rock.position.y < shootPosition.y - 20)
  ) {
    Composite.remove(engine.world, elastic);

    rock.collisionFilter = {category: SOLID, mask: SOLID};
  }
});

但它仍然可以拖动。我猜问题出在我如何更改岩石上的过滤器,但我在文档中没有看到任何建议更改方法的内容。

我不认为这是因为我设置的类别,但这里它们只是为了以防万一(实体和图像的都有效,球不会与图像的碰撞:

const SOLID = 0x0001;
const IMAGE = 0x0002;
const NEXTBALL = 0x0003;

帮我让石头不再可点击

经过多次重新开始、调整和检查不同的演示后终于弄明白了。

首先,类别的位掩码必须是 2 的幂,因此 NEXTBALL 必须是 0x0004 而不是 0x0003

接下来,您不能将整个 collisionFilter 对象设置在已建立的物体上,否则会破坏碰撞。相反,您必须使用 rock.collisionFilter.category = NEXTBALL;