I have a player object (Matter/Sprite) and i want to add a bottom-sensor body in order to detect if the player hit the ground or a brick (enable jump for example).
i prepare the player body:
private preparePhysicsBody() {
const bodyPlayer = this.scene.matter.bodies.rectangle(
this.displayWidth / 2, this.displayHeight / 2,
this.displayWidth, this.displayHeight);
bodyPlayer.collisionFilter.category = PhysicsBodyCategories.PLAYER;
bodyPlayer.collisionFilter.mask = PhysicsBodyCategories.WORLD;
const bodyPlayerBottomSensor = this.scene.matter.bodies.rectangle(
bodyPlayer.position.x, bodyPlayer.position.y + (this.displayHeight / 2),
this.displayWidth - 6, 1, {
isSensor: true
});
bodyPlayer.collisionFilter.category = PhysicsBodyCategories.PLAYER_BOTTOM;
bodyPlayer.collisionFilter.mask = PhysicsBodyCategories.WORLD | PhysicsBodyCategories.BRICK;
const body = this.scene.matter.body.create({
parts: [
bodyPlayerBottomSensor,
bodyPlayer
]
});
this.setExistingBody(body);
this.setFixedRotation();
//on collisions
this.setOnCollide((obj: MatterCollisionData) => {
console.log(obj.bodyA.collisionFilter.category)
console.log(obj.bodyB.collisionFilter.category)
});
and a few bricks (also Matter/Sprite) in different positions:
class Brick extends Sprite {
constructor(scene: Scene, x: number, y: number) {
super(scene.matter.world, x, y, "brick", undefined, {
isStatic: true
});
this.setPosition(x, y);
this.setFriction(0.01)
this.setDisplaySize(100, 10)
this.setCollisionCategory(PhysicsBodyCategories.BRICK)
The first problem is, that the playerBody still collides with the bricks even though there is only the WORLD assigned to mask.
The second problem is, that setOnCollide doesn´t fire.
I tried also with the initial generated body of the playerObject and set the same values for category and mask and it worked, collision detection fired too.