【问题标题】:Getting tile from coordinates on staggered isometric grid从交错等距网格上的坐标获取平铺
【发布时间】:2018-01-29 17:53:58
【问题描述】:

我无法弄清楚数学。我有一个交错的二维等距网格,将网格单元转换为世界坐标没有问题,但现在我不知道如何反转这个过程。

这是 CellToCoord 方法:

public static Vector2 CellToCoord(int x, int y) {
    return new Vector2() {
        x = x + ((y % 2) * 0.5f),
        y = y * -0.25f
    };
}

非常简单,显示的网格与i wanted it to be 完全一样,我现在想从世界坐标中获取图块。

编辑:
Image,我从 CellToCoord() 方法获得的世界坐标给了我一个代表单元中心的世界位置。

【问题讨论】:

    标签: c# 2d grid-layout isometric staggeredgridlayout


    【解决方案1】:
    public static Point CoordToCell(Vector2 coord) {
        // Divide the cell coordinate the value again (or multiply by -4)
        // Then Math.Round() to eliminate any floating point inaccuracies
        var cellY = (int)Math.Round(coord.y / -0.25f);
    
        // Armed with the original Integer Y, we can undo the mutation of X (again, accounting for floating point inaccuracies)
        var cellX = (int)Math.Round(coord.x - ((cellY % 2) * 0.5f));
    
        return new Point() {
            x = cellX,
            y = cellY
        };
    }
    

    这应该扭转这个过程。我使用 Point 作为整数 X 和 Y 坐标的通用容器,您可以酌情替换它。

    我认为最大的问题是 X 突变最初是基于 Y 的整数值,然后您从中存储了一个浮点值。如果没有 Math.Round()s 将浮点结果可靠地强制转换回整数值,我无法找到一种方法来扭转这种情况。

    更新计算包含给定世界坐标的单元格坐标:

    看看你的世界坐标是如何排列的,我们可以看到每个单元格的中心都在给定的对角线上:

    • y = ( x - i ) / 2 ... 或 ... x - 2y = i
    • y = ( j - x ) / 2 ... 或... x + 2y = j

    其中 i 和 j 是整数。此图显示了 x - 2y = i 对角线的情况。

    使用这些,我们可以获取给定点的世界坐标,计算出 i 和 j,然后将它们四舍五入到最接近的整数,得到单元格的中心,即 i 和 j。

    然后我们可以使用 i 和 j 来计算单元格中心的 x 和 y,并使用我之前发布的函数将它们转换回单元格坐标。

    public static Point CoordToCell(Vector2 coord)
    {
        // Work out the diagonal i and j coordinates of the point.
        // i and j are in a diagonal coordinate system that allows us
        // to round them to get the centre of the cell.
        var i = Math.Round( coord.x - coord.y * 2 );
        var j = Math.Round( coord.x + coord.y * 2 );
    
        // With the i and j coordinates of the centre of the cell,
        // convert these back into the world coordinates of the centre
        var centreX = ( i + j ) / 2;
        var centreY = ( j - i ) / 4;
    
        // Now convert these centre world coordinates back into the
        // cell coordinates
        int cellY = (int)Math.Round(centreY * -4f);
        int cellX = (int)Math.Round(centreX - ((cellY % 2) * 0.5f));
    
        return new Point() {
            x = cellX,
            y = cellY
        };
    }
    

    我使用以下坐标点作为测试用例

    { x = 1.5f, y = -0.25f } => ( 1, 1 )        // Centre of 1, 1
    { x = 1.9f, y = -0.25f } => ( 1, 1 )        // Near the right of 1, 1
    { x = 1.5f, y = -0.374f } => ( 1, 1 )       // Near the bottom of 1, 1
    { x = 1.1f, y = -0.25f } => ( 1, 1 )        // Near the left of 1, 1
    { x = 1.5f, y = -0.126f } => ( 1, 1 )       // Near the top of 1, 1
    { x = 2.5f, y = -0.25f } => ( 2, 1 )        // Centre of 2, 1
    { x = 2f, y = -0.5f } => ( 2, 2 )           // Centre of 2, 2
    

    希望对你有帮助

    【讨论】:

    • 这就是我最终得到的结果(在朋友的帮助下),但是当我导航鼠标(我使用世界坐标)时,它无法正常工作,尤其是在 Y 部分
    • 您是想将坐标转换回来,还是想弄清楚给定世界坐标在哪个单元格中?如果您试图确定该点在哪个单元格中,您希望将四个点中的哪一个作为单元格的“代表”点?
    • 我想从世界坐标中找到一个单元格,所以我有一个由 2 个浮点 x 和 y 组成的向量或点,并且我想最终得到代表单元格的 2 个整数。跨度>
    • 我还没有完全关注。你的 X 和 Y 整数不代表一个调用,它们代表一个点,那么组成一个单元格的四个点中你想得到哪个点?比如最下面的那一个?你有一些输入和预期输出的测试用例吗?
    • i.imgur.com/kRd0KAE.png,我从 CellToCoord() 方法获得的世界坐标给了我一个代表单元中心的世界位置。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-10-08
    • 2013-08-08
    • 2012-06-01
    • 1970-01-01
    • 1970-01-01
    • 2018-10-28
    相关资源
    最近更新 更多