c# - 世界点到等距点 - 需要帮助理解

标签 c# unity-game-engine isometric

现在我知道这个问题已经在这个网站和许多其他网站上被问过和回答了数百次,但我找不到关于为什么计算有效以及如何实际更改它以满足我的需要的解释。

这是我当前的功能:

//Converts X, Y from the 2D World to a Point in the Isometric Grid
public static Vector2 WorldToIsometric(float x, float y, int scale)
{
    Vector2 point = new Vector2(x, y);
    Vector2 iso_point = new Vector2(point.x, point.y);
    float half_scale = scale / 2;

    iso_point.x = point.x - (point.y * 2) + half_scale;
    iso_point.y = point.x + (point.y * 2) - half_scale;

    iso_point.x /= scale;
    iso_point.y /= scale;

    iso_point.x = Mathf.Floor (iso_point.x);
    iso_point.y = Mathf.Floor (iso_point.y);

    return iso_point;
}

关键:

        x  =  mouse x in world
        y  =  mouse y in world
    scale  =  width of isometric tile
    point  =  mouse point in world
iso_point  =  mouse point on isometric grid

我想要的输出如下:

enter image description here

输出实际上是什么样的:

enter image description here

我只是无法理解更改代码的概念,我可能已经花了很长时间并且喝了太多咖啡,但无论我如何更改,它似乎都不像我想象的那样有效会的。

请随意链接一个有关其数学工作原理的详细教程,但我找不到任何真正可以帮助我理解如何更改代码的内容。

提前干杯

最佳答案

如果您必须将整个公式放在一起,

iso_point.x = point.x/scale - (point.y + 1) / (scale / 2);

在您的情况下,point.x/scale 是当您将鼠标向右移动时增加 X 的值。如果你向上移动鼠标,你可以看到 iso_point.x 将减少 point.y/(scale/2)。

scale/2 来自图 block 的高度 = 宽度/2

当您希望 iso_point.x 随着 point.x 和 point.y 的增加而增加时,您将需要类似的内容

iso_point.x = point.x + (point.y * 2) - half_scale;

以同样的方式,iso_point.y 必须随着 point.x 的减小和 point.y 的增大而增大:

iso_point.y = -point.x + (point.y * 2) - half_scale;

关于c# - 世界点到等距点 - 需要帮助理解,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32163735/

相关文章:

android - Unity3d FB.canvas.pay Android 和 iOS 的替代方案

opencv - Unity中如何识别非矩形图像?

java - 等距瓷砖绘制和拾取 - JAVA

C# 将控件变灰并在其上绘制另一个控件

c# - 在没有报表查看器的情况下将 SSRS 报表嵌入网页

c# - Unity (5.5) 插件在导出 Windows 应用程序文件 (EXE) 时使用 SQLite 数据库错误

c++ - 如何在 opengl 中为 3d 对象设置原点

javascript - 等距屏幕映射

c# - C#从文本文件中读取Json数据

c# - .NET 中的双重检查锁定需要什么样的 'volatile' 操作