I’m really struggling with getting my matter physics to work as expected. I’ll try and put this in some order to help with seeing what I’m trying to do and hopefully someone can help me out.
I have a constants object I use to store values and they include the collision categories and groups
The relevant props are these:
PHYSICS: {
COLL_CAT: {
TILES: 0x0001,
FRIENDLIES: 0x0010,
ENEMIES: 0x0011,
BULLETS: 0x0100,
DOORS: 0x0101,
},
COLL_GRP: { // never collide if same neg val, always collide if same pos val
ENTITIES: -1,
BULLETS: -2,
}
}
I have 3 custom classes that extend Phaser.Physics.Matter.Sprite
Player extends Phaser.Physics.Matter.Sprite
...
// set collision info
this.setCollisionGroup(CST.PHYSICS.COLL_GRP.ENTITIES);
this.setCollisionCategory(CST.PHYSICS.COLL_CAT.FRIENDLIES);
Enemy extends Phaser.Physics.Matter.Sprite
...
// set collision info
this.setCollisionGroup(CST.PHYSICS.COLL_GRP.ENTITIES);
this.setCollisionCategory(CST.PHYSICS.COLL_CAT.ENEMIES);
A player and an enemy can spawn a bullet. When they do the bullet has the following check:
Bullet extends Phaser.Physics.Matter.Sprite
...
// set collision info
this.setCollisionGroup(CST.PHYSICS.COLL_GRP.BULLETS);
if (shooter.isEnemy() === true) {
// enemy fired bullet, collide players but not enemies
this.setCollisionCategory(CST.PHYSICS.COLL_CAT.ENEMIES);
this.setCollidesWith([CST.PHYSICS.COLL_CAT.FRIENDLIES, CST.PHYSICS.COLL_CAT.TILES, CST.PHYSICS.COLL_CAT.DOORS]);
} else {
// friendly fired bullet, collide with enemies but not friendlies
this.setCollisionCategory(CST.PHYSICS.COLL_CAT.FRIENDLIES);
this.setCollidesWith([CST.PHYSICS.COLL_CAT.ENEMIES, CST.PHYSICS.COLL_CAT.TILES, CST.PHYSICS.COLL_CAT.DOORS]);
}
My aim is to have:
- Bullets in the same group with a negative value so they never collide
- Entities (Player / Enemy) in the same group with a negative value so they never collide
- Bullets spawned by enemies not to collide with other enemies
- Bullets spawned by players not to collide with other players
I’m clearly not understanding collision groups and categories properly because enemy bullets collide with enemies and player bullets collide with players.
I could just handle the collisions and destroy the bullets but I want them to pass through the relevant shooter types instead of being destroyed.
I must have read the Phaser Types descriptions for setCollisionGroup()
, setCollisionCategory()
and setCollideWIth()
a million times and it’s just not sinking in. Can someone please shed some light as I’ve trawled the internet looking for more information and I can’t find anything other than 1 example on the P3 site [This one] and I can’t seem to find anything with more info to point me in the right direction.