I'm writing an isometric game, but unfortunately I've got stuck writing with the algorithm used to map back to world coordinates from screen coordinates (or possibly vice versa). Anyway I can't figure out the implementations which are the inverse of my GetScreenX/Y methods. Here's some code. width and height represent the width/height of the viewport area in tiles.
With the correct implementation, this should run through without any trouble. You can run it in Linqpad.
void Main()
{
for(int width = 1;width<15;width++)
{
for(int height = 1;height<10;height++)
{
for(int x = -50;x<50;x++){
for(int y = -50;y<50;y++){
var screenX = GetScreenX(x, y, width, height);
var screenY = GetScreenY(x, y, width, height);
var worldX = GetWorldX(screenX, screenY, width, height);
var worldY = GetWorldY(screenX, screenY, width, height);
if (worldX != x || worldY != y)
{
throw new Exception("Algorithm not right!");
}
}
}
}
}
}
protected int GetScreenX(int x, int y, int width, int height)
{
return WrappingMod(x + y, width);
}
protected int GetWorldX(int x, int y, int width, int height)
{
return 1; //needs correct implementation
}
protected int GetWorldY(int x, int y, int width, int height)
{
return 1; //needs correct implementation
}
protected int GetScreenY(int x, int y, int width, int height)
{
return WrappingMod((int) ((y - x)/2.0), height);
}
static int WrappingMod(int x, int m)
{
return (x % m + m) % m;
}
Sorry to have to ask but I'm at my wits end!