Game Development Reference
In-Depth Information
// Cast to int makes sure that result is in whole numbers
float posX = (int)(inverseTileY + tilePosDiv.x - halfMapWidth);
float posY = (int)(inverseTileY - tilePosDiv.x + halfMapWidth);
// make sure coordinates are within isomap bounds
posX = MAX(0, posX);
posX = MIN(tileMap.mapSize.width - 1, posX);
posY = MAX(0, posY);
posY = MIN(tileMap.mapSize.height - 1, posY);
return CGPointMake(posX, posY);
}
Subtracting the tilemap position to take scrolling of the tilemap into account is the
same as in the orthogonal version of this method. Next I create a number of variables,
just to make the code a bit more readable and have less to type, and then divide the
map size width by half. I then create a CGPoint tilePosDiv , which is the pixel
location within the tilemap divided by the tilemap's width and height (in points, not
pixels), and an inverseTileY variable, which is simply the inverse of the tilemap's
y coordinates. This inversion is necessary because the tilemap y coordinates count from
top down, whereas screen y coordinates count from bottom up.
Now you can get to actually calculating the x,y coordinates of the touched tile. The cal-
culation starts with the inverse y coordinate, which will be in the range of 0 to 29 for a
tilemap that has a height of 30 tiles. It defines the vertical position in the tilemap from
which you'll be looking for the x and y tile coordinates horizontally.
This becomes clearer if you look at Figure 11-4 and locate tile coordinate (3,3). You'll
notice that when you move on a horizontal line to the left of tile coordinate (3,3), the x
coordinates decrease and the y coordinates increase: (2,4), (1,5), (0,6). Similarly, if you
move to the right of tile coordinate (3,3), the x coordinates increase while the y co-
ordinates decrease: (4,2), (5,1), (6,0).
That means you can get both x and y tile coordinates from the inverseTileY posi-
tion. In the case of the x tile coordinate, you add the tilePosDiv.x coordinate and
then subtract halfMapWidth . For the y tile coordinate, you subtract the sum of
tilePosDiv.x and halfMapWidth from inverseTileY .
Search Nedrilad ::




Custom Search