Sign in to follow this  

Tilemap Location

This topic is 4274 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
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

This topic is 4274 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.

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