Tilemap Location

Started by
0 comments, last by Krisc 18 years ago
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
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.

This topic is closed to new replies.

Advertisement