• Advertisement
Sign in to follow this  

Tilemap Location

This topic is 4346 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 was just going to post asking a question, but then I decided to use my initiative and find out the answer myself.
    float pos = 144;
    float tile_width = 100;
    int coord;
    if (pos >= 0) {
        coord = (int)(pos/tile_width);
    }else{
        coord = (int)(pos/tile_width) - 1;
    }
Is this correct for finding the current coordinate you are in a tilemap? Seems to work - could anyone who has done a tile engine confirm it is the correct way? The problem was I didn't know if negative numbers rounded up or down... they seem to go up. Cheers... :D

Share this post


Link to post
Share on other sites
Advertisement
If your tile width was 100, and your position on the X axis was 144, you would be in the second tile. So you need to allow for the fact that anything over that width is the next tile over.

Your code would end up giving coord a value of one because the cast to integer truncates the extra bit you need.

Share this post


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

  • Advertisement