Hello! I am creating a map in tiled, and I used the tiled collision editor to add some custom collisions. However, this isn’t working, and it works as normal as if I hadn’t put any custom collisions. Here is the line of code I am using: map.setCollisionFromCollisionGroup(true, false, environment); Thank you!
I had a similar issue. @samme helped me get a sense for how this function works. Unfortunately, I don’t think this function sets the collision bounding box from Tiled’s collision editor (at least not pixel by pixel anyway). I still need to run tests to confirm how it actually works and I have plans to dig into the source code for my own edification, but it was easier to solve this problem a different way for me.
I’ve solved it by putting the objects I want to have custom bounding boxes for in an object layer and getting the bounding box info by iterating through those objects.
I can share my code this evening after work if you want to see it.
Yes, sure! You can post your code here when you have time. Thank you for the response!
var data = map.getTileset('Trees_3')
this.firstgid = data.firstgid;
var Collision_Objects = map.getObjectLayer('CollisionByObjectLayer')['objects']
this.collision_obj_grp = this.physics.add.group();
Collision_Objects.forEach(object =>{
console.log(object)
let index = object.gid-data.firstgid;
let obj = self.collision_obj_grp.create(object.x+(object.width/2), object.y-(object.height/2), 'tiles_2', index);
console.log(obj)
try{
tile_body = data.tileData[index].objectgroup.objects[0]
obj.body.setOffset(tile_body.x, tile_body.y)
obj.body.width = tile_body.width;
obj.body.height = tile_body.height;
obj.body.setImmovable(true);
console.log('it worked seamlessly')
}
catch(error){
console.log(error)
}
});
this.collision_obj_grp.setDepth(9);
this.physics.add.collider(this.players, null, this);
So the above code is heavily borrowed from stuff I’ve seen around here. Unfortunately, I wasn’t able to find the key reference after searching, so I’m sorry I can’t point you to the original.
I highly recommend you poke around in these different objects to see what properties they hold. Particularly the “data” variable and the array of objects held in “Collision_Objects”. You’ll notice that the gid for the object you find with getObjectLayer(args)[‘objects’] will be the firstgid of the tileset for that layer + the ID of that specific tile within that tileset (hence the let index object.gid-data.firstgid line to get the tile ID in the tileset, so we can go digging for it’s x & y offset to make the custom bounding box offset from the sprite location. Note: this only works if the layer only uses assets from a single sprite sheet, though it should be possible to change the code to make it work for those situations.
TMX Map Format — Tiled 1.8.0 documentation is a really helpful resource because the “id” you see scattered throughout the JSON of your map and the data in Phaser (which often mirrors the JSON perfectly) will depend on where you are in the document…
There may be a better way to do this, but this is what worked for me :). Please feel free to drop back in with any questions.