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