Jump to content

Colliding with a Group of TilemapLayers?


johnfn
 Share

Recommended Posts

So if I have a Phaser.TilemapLayer and I do game.physics.arcade.collide(player, myTilemapLayer), everything works fine. But then I add myTilemapLayer to a Phaser.Group and do game.physics.arcade.collide(player, myTilemapLayerGroup), the player doesn't collide with the group any more!

 

This appears to be intentional; the collide function only collides against things in a group that have a body, which TilemapLayers certainly don't. However, it seems highly counterintuitive to me that you could collide with an object, but not with a group that contains that object, and it took me quite a while to figure out what was wrong.

 

What's the rationale here? Is there some way to get what I want? 

Link to comment
Share on other sites

The overlap and collide methods don't recursively check inside groups or group-like objects within the object you pass to them - this is on purpose to maintain good performance. If you need to collide with a specific thing (such as a tilemap layer) then just keep a reference to that layer and call the collide method with that reference. It may seem slightly more work than is necessary to have to specifically create overlap/collide calls for each thing you want to collide but it ensures you are being specific about what collides with what, keeps performance high and reduces debugging headaches that would inevitably arise when people got lazy and started, for instance, colliding the world with itself.

Link to comment
Share on other sites

The overlap and collide methods don't recursively check inside groups or group-like objects within the object you pass to them - this is on purpose to maintain good performance. If you need to collide with a specific thing (such as a tilemap layer) then just keep a reference to that layer and call the collide method with that reference. It may seem slightly more work than is necessary to have to specifically create overlap/collide calls for each thing you want to collide but it ensures you are being specific about what collides with what, keeps performance high and reduces debugging headaches that would inevitably arise when people got lazy and started, for instance, colliding the world with itself.

 

The problem I have with this answer is that groups *do* check inside themselves one-deep, but only for some types of objects, like Sprites. I don't see any special attributes of Tilemaps that distinguish them from Sprites sufficiently that collision against a group of Sprites would work, but collision against a group of Tilemaps wouldn't. 

Link to comment
Share on other sites

Collide and overlap are definitely not recursive - I've created a test to demonstrate this here: http://jsfiddle.net/stxcZ/3/ - it's crudely written but it demonstrates the problem clearly. I create a sprite on the stage, a group, a group within that group and then an enemy within that group, and do three separate tests. Two of them work (direct sprite-to-sprite collision and sprite-to-group collision of the group directly containing the sprite) whereas the last one, which would rely on recursion, fails.

 

Tilemaps are equivalent to groups in that they contain objects with bodies, but do not have bodies themselves. You may only pass the collide or overlap callbacks either an object with a body, or an array-like object that directly contains objects with bodies. I have submitted a PR which has been merged into the dev branch which explicitly states this to avoid further confusion.

Link to comment
Share on other sites

Collide and overlap are definitely not recursive - I've created a test to demonstrate this here: http://jsfiddle.net/stxcZ/3/ - it's crudely written but it demonstrates the problem clearly. I create a sprite on the stage, a group, a group within that group and then an enemy within that group, and do three separate tests. Two of them work (direct sprite-to-sprite collision and sprite-to-group collision of the group directly containing the sprite) whereas the last one, which would rely on recursion, fails.

 

Tilemaps are equivalent to groups in that they contain objects with bodies, but do not have bodies themselves. You may only pass the collide or overlap callbacks either an object with a body, or an array-like object that directly contains objects with bodies. I have submitted a PR which has been merged into the dev branch which explicitly states this to avoid further confusion.

 

Alright, I suppose that's enough to get me to stop complaining - for now, anyways ;-) (Describing Tilemaps as pseudo-Groups was what helped clarify it for me, though it's unfortunate that they don't actually inherit from Group.) It would be really nice to have a recursive flag to pass into collide though! 

Link to comment
Share on other sites

You're not the first person to request it, though looking at the collide/overlap code there seems to be a branch of functions equipped to deal with each specific type of collision. I'm not sure how easy it'd be to add in a check on each object to see if it has children, and if so, call the collide/overlap handler again for that object... I suspect it could get messy. Maybe it's worth creating a feature request on Github and seeing what Rich has to say on the subject?

Link to comment
Share on other sites

 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...