Frage

I'm trying to convert isometric tile coordinates to screen coordinates. I seem to have problem especially with the Y coordinates, looks like the X part works just fine. here is what I got so far.

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

my player is added as a child to the Tile map itself and the map is added to the layer at screenSize.x/2, scrrensize.y/2 with an anchor point of 0.5

I have done the same thing successfully with an orthogonal map but seem to struggle with the isometric one.

Thank you

War es hilfreich?

Lösung 2

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

Andere Tipps

really its look like this:

  // 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);
    }
Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top