0

私は等角ゲームを書いていますが、残念ながら、画面座標から世界座標に(またはその逆に)マップするために使用されるアルゴリズムで書き続けることができません。とにかく、GetScreenX/Yメソッドの逆である実装を理解することはできません。ここにいくつかのコードがあります。widthとheightは、タイルのビューポート領域の幅/高さを表します。

正しい実装で、これは問題なく実行されるはずです。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;
}

質問する必要がありますが、私は私の知恵の終わりにいます!

4

2 に答える 2

0

WrappingMod 関数がわかりません。あなたはスクリーン座標を計算しているようです。これにより、世界 - >画面マッピングが非全単射になるため、逆はありません。

そもそも、なぜ複数のタイルを重ねて描画するのですか?

モジュラスを取得する代わりに、ワールド座標がビューポートに収まらない場合に例外を発生させたいはずです。

于 2009-08-19T19:37:08.943 に答える
0

ビューからワールド座標に変換するための行列と、逆に変換するための逆行列の 2 つの行列が必要です。

// 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);
于 2011-10-10T06:52:23.033 に答える