728x90 AdSpace

Latest Article



Converting isometric coordinates to world coordinates




Converting isometric coordinates to world coordinates in c++ coco2d-x Example code


Here is a pretty good explanation for how the mapping can be done with basis vectors and your grid's origin: http://www.wildbunny.co.uk/blog/2011/03/27/isometric-coordinate-systems-the-modern-way/

But I realize this might be tricky for some people so here's some sample code I just whipped up that can help you convert the isometric tile coordinates to screen space coordinates: 

    // create and position your tile map so it is centered in your scene
    CCTMXTiledMap* map = CCTMXTiledMap::tiledMapWithTMXFile("game.tmx");
    CCSize winSize = CCDirector::sharedDirector()->getWinSize();
    map->setAnchorPoint(ccp(0.5f, 0.5f));
    map->setPosition(ccp(winSize.width/2.0f, winSize.height/2.0f));
    this->addChild(map);
    
    // get the spawn object x and y location
    // note that I've just added new object properties xVal and yVal to store the
    // tile coordinate values as some people are having problems using
    // the x,y position values set by default by Tiled Qt.

    CCTMXObjectGroup *objectGroup = map->objectGroupNamed("objectLayer");
    CCStringToStringDictionary *dict = objectGroup->objectNamed("spawnPoint");
    float x = ((CCString *) dict->objectForKey(std::string("xVal")))->toFloat();
    float y = ((CCString *) dict->objectForKey(std::string("yVal")))->toFloat();

    // get the tile width and height from the map
    CCTMXLayer *tileLayer = map->layerNamed("tileLayer");
    float tileWidth = tileLayer->getMapTileSize().width;
    float tileHeight = tileLayer->getMapTileSize().height;

    // calculate the actual point where the 0,0 iso tile lies.
    float mapOriginX = map->getPosition().x;
    float mapOriginY = map->getPosition().y + 
                                   ((map->getMapSize().height / 2.0f) * tileHeight);
    mapOriginY -= tileHeight/2.0f;
    
    // from this you can easily calculate the screen coordinate of any tile on the
    // isometric map. Think of how the x,y screen values change as you move 
    // across your tile map on either axis. 
    
    CCPoint pos = ccp(mapOriginX, mapOriginY);

    float halfTileWidth = tileWidth / 2.0f;
    float halfTileHeight = tileHeight / 2.0f;    
    pos.x += (x * halfTileWidth) - (y * halfTileWidth);
    pos.y -= (y * halfTileHeight) + (x * halfTileHeight);
    
    enemy->setPosition(pos);

no image
  • Title : Converting isometric coordinates to world coordinates
  • Posted by :
  • Date : 11:05
  • Labels :






  • Blogger Comments
  • Facebook Comments

0 comments:

Post a Comment