Collision Groups and Collision Categories

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:

  1. Bullets in the same group with a negative value so they never collide
  2. Entities (Player / Enemy) in the same group with a negative value so they never collide
  3. Bullets spawned by enemies not to collide with other enemies
  4. 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.

So, after more hours of searching I realised that there are examples on labs.phaser.io that arent found at the main phaser.io/examples/v3 page. Not sure why that is, but I stumbled across this which I’m not studying to get a better understanding. I’d still appreciate if anyone could help with my specific issue but I think this now makes more sense to me. If I figure out my specific issue, I will update the thread with my solution.

I’m no expert. I just want to help.

Here’s my code. Perhaps it can give you a better example in practice.
Save-a-Dino

Pay attention to lines 134-136 where I set up the collision detectors.
Lines 216, 264, 276 are the functions where those collision detectors lead you.

One of my functions, hitBySmallAsteroid, I had to do some math to make it work. When the character was hit by a small asteroid according to the game, it was hit multiple times while the asteroid passed him. Of course, that’s not fair; the player would lose multiple points from one strike.

The code has been updated with a merge, so you’re free to look at that version as well.

Hopefully this helps in some way either now or later.