'MatterJS allow bodies to pass through yet still trigger collision
I want certain rigid bodies to be able to pass through eachother, i.e. no collision response. Though I still want the collision to detect, and trigger an event for collisionsActive: Events.on('collisionActive')
so that I can execute some code when they collide with eachother.
Events.on(engine, 'collisionActive', (event) => {
for (const body of event.pairs) {
if (body.a collides with body.b) doSomeStuff(body.a, body.b)
}
});
These rigid bodies sit on 'floors' or 'platforms', so they need to collide with these walls or floors, but pass through other bodies.
One option is to give the bodies a category
in the collisionFilter
but this doesn't seem to trigger the collisionActive event.
What can I do here?
Solution 1:[1]
I solved this by manually going over the Bodies and comparing their bounds using Bounds.contains
.
if (Bounds.contains(bodyA.bounds, bodyB.position)) doSomeStuff()
Solution 2:[2]
When you create a body you can pass the option isSensor
as true, that way the body will pass through anything and will trigger the collision event
{
isSensor:true
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | flooblebit |
Solution 2 | Mark Vaaz |