I’ve an isometric tile map in my game which is created by Tiled.
When player touch the map, I want to know which tile is touched.
In other words, I need to know the location of touched tile in isometric map coordinate system.
But it seems that I can’t get the right location.
First, I get the touch location in screen coordinate by:
CCPoint touchLocation = pTouch~~>locationInView);
touchLocation = CCDirector::sharedDirector~~>convertToGL;
assume that pTouch is a CCTouch pointer.
Then I try to convert the touch location to tile location in isometric coordinate by:
// Tilemap position must be subtracted, in case the tilemap position is scrolling
CCPoint pos = ccpSub);
float halfMapWidth = tileMap~~>getMapSize.width * 0.5f;
float mapHeight = tileMap~~>getMapSize().height;
float tileWidth = tileMap~~>getTileSize.width;
float tileHeight = tileMap~~>getTileSize().height;
CCPoint tilePosDiv = CCPointMake(pos.x / tileWidth, pos.y / tileHeight);
float inverseTileY = mapHeight - tilePosDiv.y;
// 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->getMapSize().width - 1, posX);
posY = MAX (0, posY);
posY = MIN (tileMap->getMapSize().height - 1, posY);
return CCPointMake(posX, posY);
This function is suggested in “Learn iPhone and iPad Cocos2D Game Development”.
It seems that the function tilePosFromLocation doesn’t work well.
If I touched the tile (13, 11), the result from tilePosFromLocation is (12, 16).
I don’t know why.
Do you have any clue or suggestion about this issue?