Matter physics setOnCollideWith()

Hello! I have two bodies and I want to detect when they collide. I’m not sure if I should be using SAT but I decided to try using setOnCollideWith() from https://photonstorm.github.io/phaser3-docs/Phaser.Physics.Matter.Components.Collision.html. Here is my code:

function create ()
{
    cursors = this.input.keyboard.createCursorKeys();
    let floor = this.matter.add.image(400, 600, 'floor', null, { isStatic: true }).setScale(2, 0.5).setAngle(8);

    let mainCollisionCategory = this.matter.world.nextCategory();
    floor.setCollisionCategory(mainCollisionCategory);

    pogoStick = new PogoStick('pogo_stick_peg', 'player', this, this.matter, mainCollisionCategory);
    pogoStick.lowerBody.setOnCollideWith(floor.body, collisionCallback)
}

function collisionCallback ()
{
    console.log('collided');
}

The callback is never hit :(. Both pogoStick.lowerBody and floor.body are Matter bodies so I am unsure why this happening.

Anyone have any insight?

Would you mind sharing the PogoStick class? That all looks correct to me.

I actually found the issue! I was creating a body using Body.fromVertices() and a path which will create a body with a bunch of child bodies. This means the collision was actually happening with a child body instead of the parent. I used this method Phaser.Physics.Matter.Matter.Query.collides(bodyA, bodyB.parts) to get it to work :slight_smile:

1 Like

same problem here. For future reference, I solved the problem with an extended version of setOnCollide()

setOnCollide(callback: Function): Phaser.GameObjects.GameObject {
if(this.body && this.body.parts && this.body.parts.length){
  for(let part of this.body.parts){
    part.onCollideCallback = callback;
  }
}
return super.setOnCollide(callback);

}