2013-01-21 3 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가 증가하는 내 디버그 정보에 따르면 여기

은 내가 사용하는 기능입니다 규모가 포함되어 있지 않습니다. 위 함수의 척도를 포함하여 시도했지만, 추가 할 때마다 문제가 더 악화되는 것 같습니다. 참고로, 스케일은 float로 저장됩니다. 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

:

마지막으로, 여기에 스크린 샷은 왼쪽 상단에서 작업을 설명하기 위해입니다

관련 문제