转换等距坐标

时间:2012-04-15 10:34:22

标签: c++ ios cocos2d-x

我决定使用c ++和cocos2d-x制作等距游戏。 每个等距图块具有X和Y坐标,每个图块移动1个。这就是我将平铺转换为屏幕坐标的方式。

cocos2d::CCPoint WorldPos::convertToScreen(){
    cocos2d::CCPoint posScreen;; 
    posScreen.x=(this->x)*(TILE_WIDTH/2) + (this->y)*-TILE_HEIGHT;
    posScreen.y=(this->x)*(TILE_HEIGHT/2) + (this->y)*(TILE_HEIGHT/2);

    return posScreen;
}

瓷砖全宽40px,高20px。

现在我需要一个函数将这些世界坐标(等距坐标)转换回屏幕坐标。

之类的东西
WorldPos* WorldPos::convertToWorld(cocos2d::CCPoint &point)

我似乎无法弄明白这一点,我做错了,我应该做一个不同的定位方式,还是有某种我无法弄清楚的计算?

1 个答案:

答案 0 :(得分:0)

尝试:

WorldPos WorldPos::convertToWorld(cocos2d::CCPoint &point) {
  int x = (point.x + 2*point.y)/40;
  int y = (2*point.y - point.x)/40;
  return WorldPos(x, y);
}

逻辑:

Px = 20*Wx - 20*Wy
Py = 10*Wx + 10*Wy, thus

Px + 2*Py = 40*Wx -> Wx = (Px + 2*Py) / 40
Px - 2*Py = -40*Wy -> Wy = (2*Py - Px) / 40

其中PxPyWxWyCCpoint::xCCPoint::yWorldPos::x,{{1}分别

相关问题