Phaser 3:物质物理检测碰撞
Phaser 3: Matter physics detect collision
我正在尝试检测两个物体何时相互碰撞,但我不确定该怎么做。
我有以下场景,它向场景中添加了两个物理图像。我只需要一种方法来检测两者何时发生碰撞。
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...
}
}
我想不通的是如何检测玩家何时与柱子发生碰撞。我搜索的所有内容都是如何使用街机物理学来做到这一点,但我使用的是物质物理学。
我找不到任何关于如何检测碰撞然后 运行 函数的信息。
查看示例后 here,
要在碰撞时调用函数,请像 this 示例中那样使用 'oncollisionStart' 事件。
this.matter.world.on('collisionstart', function (event, bodyA, bodyB) {
console.log('collision');
});
另一种方法是将碰撞事件回调添加到对象本身。
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 and also what a pair looks like: https://brm.io/matter-js/docs/files/src_collision_Pair.js.html#
您还可以监听特定的碰撞。
var paddle.setOnCollideWith(ball, pair => {
// Do something
});
我正在尝试检测两个物体何时相互碰撞,但我不确定该怎么做。
我有以下场景,它向场景中添加了两个物理图像。我只需要一种方法来检测两者何时发生碰撞。
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...
}
}
我想不通的是如何检测玩家何时与柱子发生碰撞。我搜索的所有内容都是如何使用街机物理学来做到这一点,但我使用的是物质物理学。
我找不到任何关于如何检测碰撞然后 运行 函数的信息。
查看示例后 here, 要在碰撞时调用函数,请像 this 示例中那样使用 'oncollisionStart' 事件。
this.matter.world.on('collisionstart', function (event, bodyA, bodyB) {
console.log('collision');
});
另一种方法是将碰撞事件回调添加到对象本身。
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 and also what a pair looks like: https://brm.io/matter-js/docs/files/src_collision_Pair.js.html#
您还可以监听特定的碰撞。
var paddle.setOnCollideWith(ball, pair => {
// Do something
});