【问题标题】:Translating screen location to isometric tile location with scaling通过缩放将屏幕位置转换为等距平铺位置
【发布时间】:2013-01-21 13:08:08
【问题描述】:

我正在尝试将鼠标在屏幕上的位置转换为地图上的特定图块。使用下面的功能,我相信我是正确的,但是当我缩放时我无法正确缩放。任何想法为什么?

这是我正在使用的函数:

    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 正在递增,但是它们的值都是错误的,因为没有考虑比例。我尝试在函数中包含比例上面,但无论我添加它,它似乎只是让事情变得更糟。作为参考,比例存储为浮点数,其中 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;
    }

【问题讨论】:

    标签: c# xna


    【解决方案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;
        }
    

    我不完全确定为什么规范化器需要在那里,但我一直在玩弄地图的比例和大小,这似乎不会影响这个值需要是什么。

    最后,这是一个屏幕截图来说明它在左上角的工作:

    【讨论】:

      猜你喜欢
      • 2012-06-13
      • 2012-03-28
      • 2013-11-22
      • 2013-08-08
      • 1970-01-01
      • 2015-02-09
      • 2012-07-17
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多