您的位置:首页 > 移动开发 > Cocos引擎

cocos2dx tiledmap 45度地图 世界坐标转换 格子坐标

2017-11-04 13:40 666 查看
Size mapSize = m_map->getMapSize();
Size tileSize = m_map->getTileSize();
Vec2 pos = position;

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

Vec2 tilePosDiv = CCPointMake(pos.x / tileWidth, pos.y / tileHeight);
float inverseTileY = mapHeight - tilePosDiv.y;

// Cast to int makes sure that result is in whole numbers, tile coordinates will be used as array indices
float posX = (int)(inverseTileY + tilePosDiv.x - halfMapWidth);
float posY = (int)(inverseTileY - tilePosDiv.x + halfMapWidth);

// make sure coordinates are within isomap bounds
posX = MAX(0, posX);
posX = MIN(mapSize.width - 1, posX);
posY = MAX(0, posY);
posY = MIN(mapSize.height - 1, posY);

pos = CCPointMake(posX, posY);
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: