Jump to content

get tile from body position


Recommended Posts


im trying to get tile my character is colidateing with, function that i call on colision loks like that:

hitTile = function(character_body,tile_body){    var tile =  map.getTile(layer_walls.getTileX(tile_body.x),layer_walls.getTileX(tile_body.y));}

and I get tile, but not one I was expecting.... it's more like random tile, sometimes its this tile, sometimes it's different tile . in most cases it's not tile my character is standing on.


also I have camera fallow on my character and maby I'm wrong but I think that on camera starting position I'm getting right tiles but when camera starts moving to fallow my character i'm getting just some random tiles.

Link to comment
Share on other sites

I don't know specifically the solution to your problem, but just glancing at your code, it looks like you wrote .getTileX(tile_body.y), where you used the getTileX method twice.

OMG now I'm feeling like idiot :) 

ofc. it's supose to be:

    var tile =  map.getTile(layer_walls.getTileX(target.x),layer_walls.getTileY(target.y));

I have this big hope it will solve my problem but it wont. problem still exist I'm affraid.

Link to comment
Share on other sites

ok a little more explanation.
on colide function i did:


i was walking on this tiles so console will show me wat tile i'm colide with to compare it to tiled editor. this is a result:
It's show that whole area i was walking (with I marked on image) is 25,21 so I compared this to tiled editor, :
my code looks like this

    map = this.add.tilemap('map');    map.addTilesetImage('bricks');    layer_walls = map.createLayer('walls');    layer_walls.resizeWorld();    map.setCollisionBetween(1, 999);    var tiles = this.physics.p2.convertTilemap(map, layer_walls);        tilesGroup = this.physics.p2.createCollisionGroup();    arrowsGroup = this.physics.p2.createCollisionGroup();    playerGroup = this.physics.p2.createCollisionGroup();for (index = 0; index < tiles.length; ++index) {        var tile = tiles[index];        //tile.debug=true;        tile.setCollisionGroup(tilesGroup);        tile.collides([playerGroup, arrowsGroup]);        tile.kind='tile';        tile.tile = map.getTileWorldXY(tile.x,tile.y, 15,15,'walls');        //tile.tile = map.getTile(layer_walls.getTileX(tile.x),layer_walls.getTileY(tile.y));    };


so I turn on debug for p2 bodies and it's looking quite funny:




it's like he is taking first tile for each row and make a big body for it. so it's doesn't matter if I'm standing on the beginning or end of the group of tiles it's one body and x,y position of it will get first tile :)


I think i may have solution for this. 

Link to comment
Share on other sites

knowing that i used my character X instead of tile body X . tile size is 15x15 so :

var tile =  map.getTile(layer_walls.getTileX(Math.floor(a.x/15)*15),layer_walls.getTileY(target.y));

and it's working... at least I think it is :)

Link to comment
Share on other sites


  • Recently Browsing   0 members

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