Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Phaser 3: Matter physics detect collision

I am trying to detect when two object collide with each other, but I am not sure how to do it.

I have the following scene, which adds two physics images to the scene. I just need a way to detect when the two collide.

export class MainGame extends Scene {
  public create() {
    // Create the player
    this.player = this.matter.add.image(300, 100, 'player')

    // Create a pillar
    let pillar = this.matter.add.image(500, 0, 'pillar1', null, { isStatic: true })

    // Somehow detect collision between the two...
  }
}

What I cannot figure out is how to detect when the player collides with the pillar. Everything I have searched for is how to do this using arcade physics, but I am using matter physics.

I cannot find any information on how to detect collision and then run a function.

like image 616
Get Off My Lawn Avatar asked May 07 '18 22:05

Get Off My Lawn


1 Answers

After looking at examples here, To call a function on collision, use the 'oncollisionStart' event like in this example.

this.matter.world.on('collisionstart', function (event, bodyA, bodyB) {
    console.log('collision');
});
like image 166
Fabadiculous Avatar answered Sep 21 '22 20:09

Fabadiculous