Search code examples
iosobjective-ccocos2d-iphonetile

Tile to CGPoint conversion with Retina display


I have a project that uses a tilemap. I have a separate tilemap for low-res (29x29 Tilesize) and high-res (58x58). I have these methods to calculate tileCoord to position and back again.

    - (CGPoint)tileCoordForPosition:(CGPoint)position {
    int x = position.x / _tileMap.tileSize.width;
    int y = ((_tileMap.mapSize.height * _tileMap.tileSize.height) - position.y) / _tileMap.tileSize.height;
    return ccp(x, y);
}

- (CGPoint)positionForTileCoord:(CGPoint)tileCoord {
    int x = (tileCoord.x * _tileMap.tileSize.width) + _tileMap.tileSize.width/2;
    int y = (_tileMap.mapSize.height * _tileMap.tileSize.height) - (tileCoord.y * _tileMap.tileSize.height) - _tileMap.tileSize.height/2;
    return ccp(x, y);
}

I got this from RayWenderLich and I do honeslty not understand how it works, and why it has to be so complicated. But this doesn't work when I use retina tilemaps, only on 480x320. Can someone clever come up with a way to make this work for HD? Does not have to work on low-res either, I do not plan on supporting sub-iOS 7.

I want the output to be in the low-res coordinate scale tho, as you might know, cocos2d does the resizing to HD for you. (By multiplying by two)


Solution

  • i think this will work

    - (CGPoint)tileCoordForPosition:(CGPoint)position {
        int x = position.x/29;
        int y = ((11*29)-position.y) / 29;
        
        return ccp(x, y);
    }
    
    - (CGPoint)positionForTileCoord:(CGPoint)tileCoord {
        double x = tileCoord.x * 29 + 14.5;
        double y = (11*29) - (tileCoord.y * 29) - 14.5;
    
        return ccp(x, y);
    }