2017-05-05 3 views
0

So habe ich diese Methode ....Eine Methode umkehren?

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

und ich möchte auf der Rückseite Methode isoToCells(float x, float y)

ich das versucht, aber es funktioniert nicht für mich Sinn

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

Bitte klären Sie Ihr spezifisches Problem oder fügen Sie weitere Details hinzu, um genau zu markieren, was Sie brauchen. Wie es derzeit geschrieben wird, ist es schwer zu sagen, was genau Sie fragen. –

+0

Warum hast du etwas versucht, das für dich keinen Sinn ergibt? – shmosel

Antwort

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

Mit dieser Gleichung können wir

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

So row und column in Bezug auf x und y,

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

diese Methode in der inversen ersetzen row und column zurück zu bekommen.

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