0

画面上のマウスの位置を地図上の特定のタイルに変換しようとしています。以下の関数を使用すると、私は正しい線に沿っていると思いますが、ズームすると正しくスケーリングできません。なぜ何かアイデアはありますか?

これが私が使用している関数です:

    Vector2 TranslationVectorFromScreen(Vector2 toTranslate)
    {
        Vector2 output = new Vector2();

        toTranslate -= offset; // Offset is the map's offset if the view has been changed

        toTranslate.X /= tileBy2; // tileBy2 is half of the each tile's (sprite) size
        toTranslate.Y /= tileBy4; // tileBy2 is a quarter of the each tile's (sprite) size

        output.X = (toTranslate.X + toTranslate.Y) / 2;
        output.Y = (toTranslate.X - toTranslate.Y) / 2;

        return output;
    }

デバッグ情報によると、タイルの線に沿ってマウスを動かすとXとYが増加しますが、スケールが考慮されていないため、値はすべて間違っています。上記の関数にスケールを含めてみましたが、どこに追加しても、事態はさらに悪化するようです。参考までに、スケールはfloatとして保存されます。ここで、1.0fは、スケーリングがないことを意味します(これが関係する場合に備えて)。

これは、光を当てるのに役立つ場合のスクリーンショットです。

ここに画像の説明を入力してください

編集:

関数を以下に変更することにより、数は同じポイントで増加しているように見えます(つまり、タイルごとに関連する軸に沿って1ずつ増加または1減少します)が、結果は依然として大きすぎるように見えます。たとえば、結果が100、100の場合、ズームインすると、マウスが同じタイル上にある場合でも、結果が50、50に変わる可能性があります。

新機能:

    Vector2 TranslationVectorFromScreen(Vector2 toTranslate)
    {
        Vector2 output = new Vector2();

        toTranslate -= offset;

        toTranslate.X /= (tileBy2 * scale); // here are the changes
        toTranslate.Y /= (tileBy4 * scale); // 

        output.X = (toTranslate.X + toTranslate.Y) / 2;
        output.Y = (toTranslate.X - toTranslate.Y) / 2;

        return output;
    }
4

1 に答える 1

1

コードを少しいじった後、私は解決策を見つけたようです。他の人に役立つ場合に備えて、ここに残しておきます。

    Vector2 TranslationVectorFromScreen(Vector2 toTranslate)
    {
        Vector2 output = new Vector2();
        Vector2 tempOffset = offset; // copy of the main screen offset as we are going to modify this

        toTranslate.X -= GraphicsDevice.Viewport.Width / 2;
        toTranslate.Y -= GraphicsDevice.Viewport.Height / 2;
        tempOffset.X /= tileBy2;
        tempOffset.Y /= tileBy4;

        toTranslate.X /= tileBy2 * scale;
        toTranslate.Y /= tileBy4 * scale;

        toTranslate -= tempOffset;

        output.X = (toTranslate.X + toTranslate.Y) / 2;
        output.Y = (toTranslate.X - toTranslate.Y) / 2;

        output += new Vector2(-1.5f, 1.5f); // Normaliser - not too sure why this is needed

        output.X = (int)output.X; // rip out the data that we might not need
        output.Y = (int)output.Y; // 

        return output;
    }

ノーマライザーが必要な理由は完全にはわかりませんが、マップのスケールとサイズを合計して試してみましたが、これはこの値が必要なものに影響を与えるようには見えません。

最後に、左上で機能することを示すスクリーンショットを次に示します。

ここに画像の説明を入力してください

于 2013-01-21T15:32:14.947 に答える