ios - 将等距平铺 map 坐标转换为屏幕坐标

标签 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 map 本身, map 添加到 screenSize.x/2、scrrensize.y/2 的图层, anchor 为 0.5

我用正交 map 成功地做了同样的事情,但似乎在等距 map 上挣扎。

谢谢

最佳答案

真的是这样的:

  // 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);
    }

关于ios - 将等距平铺 map 坐标转换为屏幕坐标,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18095689/

相关文章:

objective-c - 以毫秒为单位获取当前时间 Cocos2d

iphone - 在多个类之间共享 UILabel 中的相同文本

ios - 真的很奇怪的 drawRect 行为??

ios - 如何计算真实世界的对象大小?

iphone - 如何在 iPhone 上制作无限长的 ScrollView ?

objective-c - PDF 的页面高度和宽度

cocoa-touch - 第一次点击 UITextField 时发生奇怪的崩溃

ios - 是否可以 "pause"线程并让另一个操作先进行?

ios - 在 Swift 中调整 UIWebview 的高度/宽度

iphone - 如何防止我的 iPhone 4 手电筒应用程序在关闭时闪烁?