• Advertisement
Sign in to follow this  

[Question] Regarding calculating tile position from GID

This topic is 1439 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hello,

 

I am working on my first game using JS and HTML5 Canvas and have written a class which will parse and draw maps based on the .tmx format exported from Tiled. 

 

This is working, almost. 

 

The problem I having is this, It is calculating the position of the tile to cut from the tileset based on the GID parsed from the .tmx file. This works for tiles in the first 15 columns of my tileset, but once we get more than 15 columns in it begins to grab tiles from the wrong row (one row more than it should). 

 

Here are the calculations I am using to get the tile from the tileset based on GID. 

var sx = Math.floor((tile.gid - tileSet.firstGid) % (tileSet.image.width / tile.width));
var sy = Math.floor(((tile.gid) / (tileSet.image.height / tile.height)) / 2);

To illustrate my issue, I logged tile data to console as they are drawn, here is what I see. 

 

 

slice: 0,8 gid: 489

slice: 1,8 gid: 490 map.js:140
slice: 2,8 gid: 491 map.js:140
slice: 3,8 gid: 492 map.js:140
slice: 4,8 gid: 493 map.js:140
slice: 5,8 gid: 494 map.js:140
slice: 6,8 gid: 495 map.js:140
slice: 7,8 gid: 496 map.js:140
slice: 8,8 gid: 497 map.js:140
slice: 9,8 gid: 498 map.js:140
slice: 10,8 gid: 499 map.js:140
slice: 11,8 gid: 500 map.js:140
slice: 12,8 gid: 501 map.js:140
slice: 13,8 gid: 502 map.js:140
slice: 14,8 gid: 503 map.js:140
slice: 15,9 gid: 504 map.js:140
slice: 16,9 gid: 505 map.js:140
slice: 17,9 gid: 506 map.js:140
slice: 18,9 gid: 507 map.js:140
slice: 19,9 gid: 508 map.js:140
slice: 20,9 gid: 509 map.js:140
slice: 21,9 gid: 510
 
As you can see, as soon as we get to tile 504 is shifts to the next row in the tileset. However, tile 504 is in the same row as tile 503 and tile 505.
 
If anyone can see where I have gone wrong, it is much appreciated.
 
Thanks!
Edited by jarredkenny

Share this post


Link to post
Share on other sites
Advertisement

Notice in that thread they're using the width in both the x and y calculations whereas you swapped in the height. I believe that could be causing you issues.

Share this post


Link to post
Share on other sites

Notice in that thread they're using the width in both the x and y calculations whereas you swapped in the height. I believe that could be causing you issues.

 

That was it! I can't believe I overlooked that! 

Thank you very much. :)

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement