jorbascrumps

How to refresh Matter world after Tilemap update?

Recommended Posts

Hi all,

I've used Phaser.Physics.Matter.World#convertTilemapLayer to create physics bodies for tiles in my Tilemap. This is working beautifully¬†ūüĎĆWhat I'm wondering now is how can I update the world after updates to the Tilemap (say, addition¬†or removal of a Tile)?

Thanks

Share this post


Link to post
Share on other sites

Judging solely from the source code, convertTilemapLayer creates a MatterTileBody for each colliding tile in the layer. The MatterTileBody constructor destroys the tile's existing body, so you could theoretically just call convertTilemapLayer again to recreate all of the layer's physics bodies.

I can immediately see two possible problems with this, however. For one, convertTilemapLayer only processes colliding tiles, so it won't remove the body from a colliding tile which became non-colliding. In that case, you'd need to get the tile and manually call `tile.physics.matterBody.destroy()`... or you could just do that for the whole layer before calling convertTilemapLayer. Another problem I see is that you could end up needlessly recreating a lot of tile bodies, so I'd suggest looking at the source code of convertTilemapLayer (line 537 as of 4c4421c) and the related convertTiles (line 559 as of the same commit) method in the Matter World to update only a small region of tiles (in essence, you'd need to make a clone of convertTilemapLayer but with a changed call to getTilesWithin).

I don't use Matter physics in my projects so I've got no idea if this is the best way to do this or if it'll even work, but it doesn't hurt to try if no one else has suggestions.

Share this post


Link to post
Share on other sites

Thank you for the detailed response, @Telinc1!

I've been doing some digging and came across this Labs example which does what I need. Following that example I've been able to remove a Tile's body with `tile.physics.matterBody.destroy()`. However, for some reason despite having Tiles underneath the one I've removed, my player falls right through. My assumption at this point is that it's something wrong with how I've setup my character..

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

  • Recently Browsing   0 members

    No registered users viewing this page.