title: Calculating iso tile co-ordinates for a TMX map when zoomed on a CCLayerPanZoom control
body: <p>I'm working on some code to place isometric CCTMXTiledMap onto a CCLayerPanZoom control and then convert a touch location into ISO tilemap co-ordinates. This all works perfectly well for me, so long as the scale of the CClayerPanZoom is 1 (i.e. if I don't zoom in or zoom out). I can pan the map around and still calculate the correct iso tile co-oridinates. However, as soon as I zoom the tiled map in or out the iso cordinates returned by my code are completely wrong. Please see below for my code to calculate the iso co-ordinates from the touch location.</p>
  
  <pre><code>-(CGPoint) tilePosFromLocation:(CGPoint)location tileMap:(CCTMXTiledMap*)thisTileMap panZoom:(CCLayerPanZoom*)thisPanZoom
  {   
  float midScale = (thisPanZoom.minScale + thisPanZoom.maxScale) / 2.0;
  
  float newScale = (thisPanZoom.scale &lt;= midScale) ? thisPanZoom.maxScale : thisPanZoom.minScale;
  if (thisPanZoom.scale &lt; 1)
  {        
      newScale = newScale + thisPanZoom.scale;
  }
  else
  {
      newScale = newScale - thisPanZoom.scale;
  }
  
  CGFloat deltaX = (location.x  - thisPanZoom.anchorPoint.x * (thisPanZoom.contentSize.width / CC_CONTENT_SCALE_FACTOR()) ) * (newScale);
  CGFloat deltaY = (location.y  - thisPanZoom.anchorPoint.y * (thisPanZoom.contentSize.height / CC_CONTENT_SCALE_FACTOR()) ) * (newScale);
  
  CGPoint position = ccp((thisPanZoom.position.x - deltaX) , (thisPanZoom.position.y - deltaY) );
  
  float halfMapWidth = thisTileMap.mapSize.width * 0.5f;
  float mapHeight = thisTileMap.mapSize.height;
  float tileWidth = thisTileMap.tileSize.width / CC_CONTENT_SCALE_FACTOR() * newScale;
  float tileHeight = thisTileMap.tileSize.height / CC_CONTENT_SCALE_FACTOR() * newScale;
  
  CGPoint tilePosDiv = CGPointMake(position.x / tileWidth, position.y / tileHeight );
  float inverseTileY = tilePosDiv.y - (mapHeight * CC_CONTENT_SCALE_FACTOR()) * newScale; //mapHeight + tilePosDiv.y;
  
  float posX = (int)(tilePosDiv.y - tilePosDiv.x + halfMapWidth);
  float posY = (int)(inverseTileY + tilePosDiv.x - halfMapWidth + mapHeight);
  
  
  // make sure coordinates are within isomap bounds
  posX = MAX(0, posX);
  posX = MIN(thisTileMap.mapSize.width - 1, posX);
  posY = MAX(0, posY);
  posY = MIN(thisTileMap.mapSize.height - 1, posY);
  
  return CGPointMake(posX, posY);
  }
  </code></pre>
  
  <p>Can anyone offer any insight into where I'm going wrong with this?</p>
  
  <p>Thanks,
  Alan</p>