如何让精灵不与另一个精灵发生碰撞,而只与玩家发生碰撞。 Phaser 3 和物质 js

How to make sprite not collide with another sprite but only with player. Phaser 3 and matter js

长话短说:我将 matter js 与 phaser3 一起使用,并且我有播放器、平台、锯子和树。就像在普通的平台游戏中一样,锯子应该位于地面和 air/space 之间的中间位置。

问题是平台是物理体,锯也是。玩家必须与锯、树、平台发生碰撞。并且锯只需要与玩家碰撞而不是平台。想过用collisionfilters,但是不知道放在哪一层。任何帮助将不胜感激。下面给出了最小可重现示例的 jsfiddle。谢谢!

Jsfiddle: jsfiddle.net/prateek_1/6Lsjf9w4/29

我假设锯子应该击中玩家而不是平台。这篇文章解释得很好 https://blog.ourcade.co/posts/2020/phaser-3-matter-physics-collision-filter/
或者查看官方示例,这个不具体https://phaser.io/examples/v3/view/physics/matterjs/collision-group

这是一个简短的例子(它是如何工作的):

let playertouchingground = true
const config = {
  type: Phaser.AUTO,
  width: 400,
  height: 180,
  physics: {
    default: "matter",
    matter: { debug: true}
  },
  scene: {
    create,
    update
  },
};

var game = new Phaser.Game(config);

function create() {
  ground = this.matter.add.sprite(0, 180, 'platform')
      .setStatic(true)
      .setScale(100, 1)
      .setOrigin(0)

  ground.body.label = 'platform'
  ground.setCollisionCategory(1)

  player = this.matter.add.sprite(125, 140, 'player')
  player.body.label = 'player'
  player.setCollisionGroup(2)
  player.setFixedRotation()
  
  saw = this.matter.add.sprite(140, 40, 'saw')
      .setBody('circle')
  
  saw.setCollisionGroup(2)
  saw.setCollidesWith(0)
  
  this.matter.world.on("collisionactive", (e, o1, o2) => {
      console.log(o1.label, o2.label)
      playertouchingground = true;
  });

  this.matter.world.on("collisionend", (e, o1, o2) => {
      playertouchingground = false;
    }
  )
  this.cursors = this.input.keyboard.createCursorKeys();
}


function update() {

  if (this.cursors.left.isDown ) {
      player.setVelocityX(-3)
  } else if (this.cursors.right.isDown ) {
      player.setVelocityX(3)
  } else {
      player.setVelocityX(0);
  }

  if (this.cursors.up.isDown && playertouchingground ) {
      player.setVelocityY(-8)
  }
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/phaser/3.23.0/phaser.min.js" ></script>