2d - 屏幕坐标到等距坐标

标签 2d coordinates projection tile isometric

我正在努力将鼠标/屏幕坐标转换为等距图 block 索引。我已经尝试了所有可以在这里或在互联网上找到的公式,但它们似乎都不起作用,或者我遗漏了一些东西。

enter image description here

这是一张图片,原点在左上角,一个图 block 的尺寸是 128x64px。

如果有任何帮助,我将不胜感激。

最佳答案

基本上,您需要应用一个带有其他一些位的旋转矩阵。下面是一些用 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;
}

我用几个不同的坐标对其进行了测试,结果似乎是正确的。

关于2d - 屏幕坐标到等距坐标,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19717770/

相关文章:

Java:按指定的度值围绕另一个旋转点

c++ - DX11 Sprite 问题

python - 在图形上绘制椭圆并获取坐标_Python

c# - XNA:匹配屏幕坐标的正交投影

java - 对角线上的矩阵元素

java - 是否可以通过这种方式检查二维数组中的所有元素是否相同?

ios - 在 Swift 中获取多个触摸的坐标

c# - 如何搜索坐标列表并根据其与当前位置之间的距离进行显示? (Windows 手机 7)

javascript - 将纬度/经度转换为像素坐标?

c++ - 变换从三焦点张量计算的投影矩阵以估计 3D 点