Jump to content

Tilemap collision when replacing a tile


Recommended Posts

I have a sprite with arcade physics


I also have a tilemap and have set up a collision with a ground tile:




In my update loop I have:


game.physics.arcade.collide(player, layer);


At a point in my game I replace the ground tile with another one:


map.replace(22, 131, 16, 9, 2, 1, layer);


Tile 22 gets replaced by tile 131, which doesn't have collisions set, however the player still collides with the ground where tile 22 used to be.


Is there something I need to do to update the collision locations?

Link to comment
Share on other sites

  • 1 year later...

See http://phaser.io/examples/v2/tilemaps/csv-map-with-p2

Run "map.replace(57,25)" either in the console, or in the code box at the end of the create() function just after the help text. map.replace() is definitely not updating collision information *on it's own*.

Running "map.removeTile(1,2); map.removeTile(1,3); map.removeTile(1,4);" or "map.putTile(25,1,2); map.putTile(25,1,3); map.putTile(25,1,4);" shows that map.removeTile() and map.putTile() also do not update collision information on their own.

However, calling "game.physics.p2.convertTilemap(map, layer)" again after running map.removeTile() and map.putTile() does update P2 collision - although it still does not update P2 collision after map.replace(). What gives?

=== convertTilemap() only updates P2 physics based on the collision information about each individual Tile object. removeTile() and putTile() literally remove/replace Tile objects whereas map.replace() only updates Tile index values. ===

In order to mass-update physics as well as graphics utilizing map.replace(), we must update Tile collision information as well as index values. Running "map.setCollision(25, false);" after map.replace(), and then also calling "game.physics.p2.convertTilemap(map, layer)" again will properly update P2 physics collision.

Link to comment
Share on other sites


  • Recently Browsing   0 members

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