屏幕坐标到等轴坐标

Hnu*_*nus 1 2d projection tile coordinates isometric

我正在努力将鼠标/屏幕坐标转换为等距瓷砖索引.我已经尝试了我在这里或在互联网上找到的每一个公式,但它们似乎都没有用,或者我错过了一些东西. http://i.imgur.com/HnKpYmG.png 这是一张图片,原点位于左上角,一个图块的尺寸为128x64像素.

我要感谢任何帮助,谢谢.

acf*_*cis 5

基本上,您需要应用带有一些其他位的旋转矩阵.这是一些用AWK编写的示例代码,应该很容易移植到任何其他语言:

END {
   PI = 3.1415;
   x = 878.0;
   y = 158.0;

   # Translate one origin to the other 
   x1 = x - 128*5;
   # Stretch the height so that it's the same as the width in the isometric
   # This makes the rotation easier
   # Invert the sign because y is upwards in math but downwards in graphics
   y1 = y * -2;

   # Apply a counter-clockwise rotation of 45 degrees
   xr = cos(PI/4)*x1 - sin(PI/4)*y1;
   yr = sin(PI/4)*x1 + cos(PI/4)*y1;

   # The side of each isometric tile (which is now a square after the stretch) 
   diag = 64 * sqrt(2);
   # Calculate which tile the coordinate belongs to
   x2 = int(xr / diag);
   # Don't forget to invert the sign again
   y2 = int(yr * -1 / diag);

   # See the final result
   print x2, y2;
}
Run Code Online (Sandbox Code Playgroud)

我用几个不同的坐标测试它,结果似乎是正确的.

  • 挑剔评论.PI的第四个小数位在实践中无关紧要,但你应该做对:3.14159 ......轮到3.1416. (2认同)