2017-05-05 66 views
0

所以我有這個方法....反轉方法?

public static Vector2 cellsToIso(float row, float col) { 
    float halfTileWidth = tileWidth *0.5f; 
    float halfTileHeight = tileHeight *0.5f; 

    float x = (col * halfTileWidth) + (row * halfTileWidth); 
    float y = (row * halfTileHeight) - (col * halfTileHeight); 

    return new Vector2(x,y); 
} 

,我要到反向方法isoToCells(float x, float y)

我想這一點,但它沒有任何意義,我

public static Vector2 isoToCell(float x, float y) { 
    float halfTileWidth = tileWidth * 0.5f; 
    float halfTileHeight = tileHeight * 0.5f; 

    float row = (y/halfTileWidth) - (x/halfTileWidth); 
    float col = (x/halfTileHeight) + (y/halfTileHeight); 

    return new Vector2(row,col); 
} 
+1

請說明您的具體問題或添加其他詳細信息,以確切地突出顯示您的需求。正如目前所寫,很難確切地說出你在問什麼。 –

+0

你爲什麼要嘗試一些對你沒有意義的事情? – shmosel

回答

2
float x = (col * halfTileWidth) + (row * halfTileWidth); 
float y = (row * halfTileHeight) - (col * halfTileHeight); 

有了這兩個方程我們可以寫出

x/halfTileWidth = row + col; 
y/halfTileHeight = row - col; 

所以rowcolumnxy方面,

row = (1.0/2) * (x/halfTileWidth + y/halfTileHeight); 
column = (1.0/2) * (x/halfTileWidth - y/halfTileHeight); 

在逆方法取代它來獲取rowcolumn回。

public static Vector2 isoToCell(float x, float y) { 
    float halfTileWidth = tileWidth * 0.5f; 
    float halfTileHeight = tileHeight * 0.5f; 

    float row = (1.0/2) * (x/halfTileWidth + y/halfTileHeight); 
    float col = (1.0/2) * (x/halfTileWidth - y/halfTileHeight); 

    return new Vector2(row,col); 
}