2013-01-21 55 views
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,当我沿着瓦线移动鼠标的增加,但是它们的值都是错误的,因为规模并没有被考虑在内。我已经尝试过在上面的函数中包含规模,但是无论我在哪里添加它,它似乎都会让事情变得更糟。作为参考,量表存储为浮点数,其中1.0f表示没有缩放(只要这是相关的)。

下面是情况下的截图,帮助有何启示:

enter image description here

编辑:

通过改变功能,下面,人数似乎仍然在积分相同递增(即沿每个瓷砖的相关轴上升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; 
    } 

回答

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; 
    } 

我不完全知道为什么规范器需要存在,但我已经用磅秤和的大小玩耍了地图,这似乎不会影响这个值需要。

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

enter image description here