Jump to content

  • Log In with Google      Sign In   
  • Create Account


[Question] Regarding calculating tile position from GID


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
4 replies to this topic

#1 jarredkenny   Members   -  Reputation: 107

Like
0Likes
Like

Posted 14 February 2014 - 11:53 AM

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, 14 February 2014 - 11:55 AM.


Sponsor:

#2 dejaime   Crossbones+   -  Reputation: 3972

Like
2Likes
Like

Posted 14 February 2014 - 11:57 AM

Can this thread help you?


Edited by dejaime, 14 February 2014 - 12:19 PM.


#3 jarredkenny   Members   -  Reputation: 107

Like
1Likes
Like

Posted 14 February 2014 - 11:59 AM

Can this thread can help you?

 

That is the thread I pulled my initial formula from, however, I had no such luck..



#4 Nick Gravelyn   Members   -  Reputation: 846

Like
1Likes
Like

Posted 14 February 2014 - 12:06 PM

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.



#5 jarredkenny   Members   -  Reputation: 107

Like
0Likes
Like

Posted 14 February 2014 - 12:11 PM

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. :)






Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS