setCollisionFromCollisionGroup not working how I think it does

Hi everyone,

I’ve been having a TON of fun with Phaser 3 and getting to know how everything works. I’m building a little RPG for my friends and I to play and I’m using TILED to make the maps.

Since I only want my player to collide with the base of the tree, I’ve added collision boxes in the Tiled Collision Editor and I can confirm that they show up in the tilemap because I see them when I check the ‘Show Tiled Collision Shapes’ box in the view of TILED’s GUI.

It seemed to me that using setCollisionFromCollisionGroup() would be a nice function to set the collisions. setCollisionByExclusion([-1]) works and I assumed this function would work similarly, but use the collision bounding boxes I set in TILED instead of the entire tile.

Can someone help me understand what I’m doing wrong? It seems I either misunderstand what setCollisionFromCollisionGroup() is supposed to do, or how to use it properly. See below for the relevant code in the create function:

var map = this.make.tilemap({ key: 'map' });
var tiles = map.addTilesetImage('Extereior_Nature_Combined', 'tiles');
var tiles_2 = map.addTilesetImage('Trees_2', 'tiles_2');
this.Collisions = map.createLayer('CollisionByExclusionLayer', tiles, 0, 0); //this works
this.Collisions.setCollisionByExclusion([-1]); //this works

//Below this comment I do not get an error, but I do not get any physics interactions
//this.players is a physics group
//the callback was added to see if the collisions were happening and I just didn't see them, but nothing //logs. I'm using arcade physics and all the bounding boxes are rectangles.
this.Object_Collisions = map.createLayer('CollisionByObjectLayer', tiles, 0, 0);
this.Object_Collisions.setCollisionFromCollisionGroup(true);
this.physics.add.collider(this.players, this.Object_Collisions, ()=>console.log('collided'),null,this);

Any help here would be greatly appreciated. Being able to set the bounding boxes for these collisions from tiled would make getting the levels put together so much easier :).

I haven’t used this feature but I think that setCollisionFromCollisionGroup() makes each whole tile collidable (or not), just like setCollision() etc.

After setCollisionFromCollisionGroup() add:

this.Object_Collisions.renderDebug(this.add.graphics());

Hi there Samme,

Thanks for your reply! I’ve tried your suggestion and I think you are right, it looks like it is trying to set the whole tile. I took a screenshot so you could see what the result was. Nice trick btw, that’s going to come in handy.

These trees are placed as single 48x48 tiles on my 16x16 tile grid map in Tiled. The collision bounding box I set in tiled is at the bottom of the tree. The darker orange square seems to be representing the 16x16 tile I placed the tree on in Tiled.

Interestingly enough, though the debug highlights this part of the tree, I’m still not getting collisions anywhere in the tree. My guess is this is because the bounding box I set does not overlap with the tile that’s highlighted in orange? Either way, this is not the effect I’m going for.

It looks like the best way to solve my problem is to loop through the tiles in this layer, get their objectgroup properties, then create physics objects for all the boxes I find…

If you or anyone else here has a more elegant solution, I’d love to hear it :).

I’ll make a new post if I get stuck on that approach. In the meantime, do you happen to know how to make a physics object without adding graphics to it since the bounding boxes don’t need graphics?

this.physics.add.existing(this.add.zone(x, y, w, h));
1 Like

Thanks Samme!