Sign in to follow this  
xDan

Tilemap Location

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
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

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this