将等距瓷砖地图坐标转换为屏幕坐标

时间:2013-08-07 05:58:09

标签: ios objective-c cocos2d-iphone isometric tmx

我正在尝试将等距瓷砖坐标转换为屏幕坐标。 我似乎有问题,尤其是Y坐标,看起来X部分工作得很好。这是我到目前为止所得到的。

// calculate screen coordinates from tile coordinates

- (CGPoint)positionForTileCoord:(CGPoint)pos {

float halfMapWidth = _tileMap.mapSize.width*0.5;
float mapHeight = _tileMap.mapSize.height;
float tileWidth = _tileMap.tileSize.width;
float tileHeight = _tileMap.tileSize.height;


int x = halfMapWidth*tileWidth + tileWidth*pos.x*0.5-tileWidth*pos.y*0.5;

int y =  ............


return ccp(x, y);

我的播放器作为孩子添加到Tile地图本身,地图将添加到screenSize.x / 2的图层,scrrensize.y / 2,锚点为0.5

我已经使用正交映射成功完成了同样的事情,但似乎很难与等距映射。

谢谢

2 个答案:

答案 0 :(得分:4)

真的看起来像这样:

  // calculate screen coordinates from tile coordinates
    - (CGPoint)positionForTileCoord:(CGPoint)pos {

        float halfMapWidth = _tileMap.mapSize.width*0.5;
        float mapHeight = _tileMap.mapSize.height;
        float tileWidth = _tileMap.tileSize.width;
        float tileHeight = _tileMap.tileSize.height;


        int x = halfMapWidth*tileWidth + tileWidth*pos.x*0.5-tileWidth*pos.y*0.5;

        int y = (pos.y + (mapHeight * tileWidth/2) - (tileHeight/2)) - ((pos.y + pos.x) *   tileHeight/2) + tileHeight;   

        return ccp(x, y);
    }


    // calculating the tile coordinates from screen location
    -(CGPoint) tilePosFromLocation:(CGPoint)location
    {
        CGPoint pos = location;
        float halfMapWidth = _tileMap.mapSize.width*0.5;
        float mapHeight = _tileMap.mapSize.height;
        float tileWidth = _tileMap.tileSize.width;
        float tileHeight = _tileMap.tileSize.height;

        CGPoint tilePosDiv = CGPointMake(pos.x/tileWidth, pos.y/tileHeight);
        float invereseTileY = mapHeight - tilePosDiv.y;

        // Cast int to make sure that result is in whole numbers

        float posX = (int)(invereseTileY + tilePosDiv.x - halfMapWidth);
        float posY = (int)(invereseTileY - tilePosDiv.x + halfMapWidth);

        return CGPointMake(posX, posY);
    }

答案 1 :(得分:1)

int y = (pos.y + (mapHeight * tileWidth/2) - (tileHeight/2)) - ((pos.y + pos.x) *   tileHeight/2) + tileHeight;