【问题标题】:Phaser 3: Matter physics detect collisionPhaser 3:物质物理检测碰撞
【发布时间】:2020-06-11 09:46:28
【问题描述】:

我正在尝试检测两个物体何时相互碰撞,但我不知道该怎么做。

我有以下场景,它在场景中添加了两个物理图像。我只需要一种方法来检测两者何时发生碰撞。

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

我不知道如何检测玩家何时与柱子发生碰撞。我搜索的所有内容都是如何使用街机物理来做到这一点,但我正在使用物质物理。

我找不到任何关于如何检测碰撞然后运行函数的信息。

【问题讨论】:

    标签: javascript phaser-framework matter.js


    【解决方案1】:

    看了例子here之后, 要在碰撞时调用函数,请使用 this 示例中的“oncollisionStart”事件。

    this.matter.world.on('collisionstart', function (event, bodyA, bodyB) {
        console.log('collision');
    });
    

    【讨论】:

      【解决方案2】:

      另一种方法是将碰撞事件回调添加到对象本身。

      var paddle = this.matter.add.image(400, 550, 'assets', 'paddle.png');
      var paddle.setOnCollide(pair => {
        // pair.bodyA
        // pair.bodyB
      });
      

      请参阅 enableCollisionEventsPlugin() 下的文档:https://photonstorm.github.io/phaser3-docs/Phaser.Physics.Matter.MatterPhysics.html 以及一对的外观:https://brm.io/matter-js/docs/files/src_collision_Pair.js.html#

      您还可以监听特定的碰撞。

      var paddle.setOnCollideWith(ball, pair => {
        // Do something
      });
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-09-29
        • 2018-01-28
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多