Isometric map + scroll and zoom + getting tile coordinates from touch location

I’m trying to make a game with large isometric map and user should zoom and scrool that map, and also he should be able to place new sprites by touch. For that, I need to get tile coordinates from touch location. I tried to make it with this function:

CCPoint tilePosFromLocation(CCPoint location, CCTMXTiledMap* tileMap)
{
>
CCPoint pos = ccpSub(location, tileMap~~>getPosition);
>
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;
float inverseTileY = mapHeight~~ tilePosDiv.y;
>
float posX = (int)(inverseTileY + tilePosDiv.x - halfMapWidth);
float posY = (int)(inverseTileY - tilePosDiv.x + halfMapWidth);
>
posX = MAX (0, posX);
posX = MIN (tileMap->getMapSize().width - 1, posX);
posY = MAX (0, posY);
posY = MIN (tileMap->getMapSize().height - 1, posY);
>
pos = CCPointMake(posX, posY);
>
return pos;
}
>

And it is working fine until I include and start using CCLayerPanZoom extension (for zooming and scrolling). Then it shows weird coordinates. Can you please suggest me how to make large isometric map with scrolling and zoom avaliable, and also the way how I could place a new sprite with tapping map.

Thank you in advance

bump, anybody?

Here is the code that I was using with CCLayerPanZoom:

CCPoint touchLocation = touch->getLocationInView();
        touchLocation = CCDirector::sharedDirector()->convertToGL(touchLocation);
        touchLocation = this->convertToNodeSpace(touchLocation);
        CCLog("convertToNodeSpace: X: %0.2f, Y: %0.2f", touchLocation.x, touchLocation.y);
        CCPoint tileGridLocation = this->tileCoordForPosition(ccpSub(touchLocation, this->getPanZoomLayer()->getPosition()));

        CCLog("TOUCHED: X: %0.2f, Y: %0.2f", tileGridLocation.x, tileGridLocation.y);
        CCLog("PAN ZOOM: X: %0.2f, Y: %0.2f", this->getPanZoomLayer()->getPosition().x, this->getPanZoomLayer()->getPosition().y);

        CCPoint correctedLocation = ccpSub(touchLocation, this->getPanZoomLayer()->getPosition());

        int collision = _collision->tileGIDAt(tileGridLocation);

Sorry, missed your question. Hope this code snippet helps … it was working in my game.

Can you please also post your

tileCoordForPosition

method

Thank you

I left that method out because I thought it would be confusing the topic at hand. It doesn’t really matter which way you calculate it. My way might be bad, but here goes:

//
// this method gets a tileCoord from a ccp
//
CCPoint STERegionMap::tileCoordForPosition(cocos2d::CCPoint position)
{
    int x = position.x / _tileMap->getTileSize().width;
    int y = ((_tileMap->getMapSize().height * _tileMap->getTileSize().height) - position.y) / _tileMap->getTileSize().height;
    //CCLog("tileCoordForPosition [X: %d, Y: %d]", x, y);
    return ccp(x, y);
}

Also, I was not dealing with Zoom, only Pan. Should be a place to start tho.

Unfortunately I have scrolling feature working, zoom is what mess coordinates :frowning:

Should be trivial to add to tileCoordForPosition()

I’ll be doing zoom in the next 5-6 days so I’ll either be using your solution, stuck with you or posting code :slight_smile: