No se puede encontrar la inversa de la función get coordenadas mundiales para el juego isométrica

StackOverflow https://stackoverflow.com/questions/1302086

  •  19-09-2019
  •  | 
  •  

Pregunta

Estoy escribiendo un juego isométrica, pero la escritura atascado por desgracia tengo con el algoritmo utilizado para asignar de nuevo a las coordenadas reales de coordenadas de la pantalla (o posiblemente viceversa). De todos modos no puedo averiguar las implementaciones que son el inverso de mis métodos GetScreenX / Y. Aquí hay algo de código. anchura y la altura representan la anchura / altura de la zona de visualización en azulejos.

Con la aplicación correcta, esto debe ejecutar a través sin ningún problema. Se puede ejecutar en 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;
}

Lo sentimos tener que pedir, pero estoy en mi juicio final!

¿Fue útil?

Solución

No entiendo su función WrappingMod. Pareces para calcular coordenadas de la pantalla, y luego llevarlo dimensión ventana de módulo (dos veces) sólo por el placer de hacerlo. Eso hace que su mundo-> mapeo pantalla no biyectiva, por lo que no tiene inversa.

¿Por qué estás dibujando varias baldosas ontop el uno del otro, para empezar?

En lugar de tomar el módulo, usted debe estar esperando para lanzar una excepción cuando la coordenada del mundo no cabe en la ventana gráfica.

Otros consejos

Se necesitan dos matrices: uno para convertir a la vista de las coordenadas del mundo, y su inversa para convertir a otro lado

.
// my matrices are in 4x4 column-major format

// projection matrix (world -> view)
this.viewTransform.identity()
    .translate(this.viewOrigin)
.scale(1, 0.5, 1)
.rotate(this.viewRotation, 2); // 2 = Z axis

var m = this.viewTransform.current().m;
this.context.setTransform(m[0], m[1], m[4], m[5], m[12], m[13]);

// construct inverse projection matrix (view -> world)
this.viewTransformInv.identity()
    .rotate(-this.viewRotation, 2)
    .scale(1, 2, 0)
    .translate(new Vect4(-this.viewOrigin.x, -this.viewOrigin.y));

// calculate world and map coordinates under mouse
this.viewTransformInv.project(this.mousePos, this.worldPos);

// convert coordinates from view (x, y) to world (x, y, z)
this.worldPos.z = this.worldPos.y;
this.worldPos.y = 0;
this.mapPos[0] = Math.floor(this.worldPos.x / this.TileSideLength);
this.mapPos[1] = Math.floor(this.worldPos.z / this.TileSideLength);
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top