Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JavaScript, Matter.js: Disable collision for one body

As already mentioned in the title, I am looking for a way, to disable every collision of a body in Matter.js. It should still be linkable with Constraints, and there should be the possibility of enabling the collision again after some time. Is there a way to do this? The hard thing about it, is that the object should not collide with any other object, but all the other objects should collide with each other.

like image 442
d0n.key Avatar asked Sep 20 '15 20:09

d0n.key


2 Answers

You can use collision filters, like so:

const body = Matter.Bodies.rectangle(100, 100, 50, 50);
// turns off collisions
body.collisionFilter = {
  'group': -1,
  'category': 2,
  'mask': 0,
};

From the docs:

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 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.

like image 157
Jatin Mathur Avatar answered Nov 09 '22 07:11

Jatin Mathur


see

Matter.IBodyDefinition.isSensor

in order to disable physical collisions for the Body. The Body can still be used as a sensor for collisions.

like image 5
Christopher Stock Avatar answered Nov 09 '22 08:11

Christopher Stock