【问题标题】:How to make a follow Ai work on a toroidal 2D grid based map如何使跟随 Ai 在基于环形 2D 网格的地图上工作
【发布时间】:2019-06-17 22:39:59
【问题描述】:

我有一些 NPC 在 2D 矩阵中环绕,我必须让他们使用环形地图跟随敌方派系 NPC。

我尝试了几种解决方案,但它们都给了我奇怪的行为,只对网格的某个 x 或 y 值起作用,然后它们不跟随它们返回一个图块。

这就是我现在要决定的方向:

public Position GetNextStepTowards(Position origin, Position target)
{
    Position nextStep = new Position(0, 0);

    float dx = MathF.Abs(target.X - origin.X);
    float dy = MathF.Abs(target.Y - origin.Y);

    if (dx > mapXSize / 2) nextStep.X = -1;
    else if (dx < mapXSize / 2) nextStep.X = 1;

    if (dy > mapYSize / 2) nextStep.Y = 1;
    else if (dy < mapYSize / 2) nextStep.Y = -1;

    return nextStep;
}

一个职位是:

public struct Position
{
    public int X { get; set; }
    public int Y { get; set; }

    public Position(int x, int y)
    {
        this.X = x;
        this.Y = y;
    }
}

NPC 只能移动一个单元格(Moore's),因此移动向量只能是-1 和1 之间的值。

提前感谢您的帮助!

【问题讨论】:

  • @user3386109 解决了它,将尽快发布我提出的解决方案,如果更多人分享他们的做法会有所帮助。

标签: c# grid 2d artificial-intelligence console-application


【解决方案1】:

如果我们考虑X轴,有两种情况如下图所示:

在第一种情况下(顶部),目标位于原点的右侧。在这种情况下,向右移动是直接的,向左移动是环形的。

在第二种情况下(底部),目标位于原点的左侧。在这种情况下,向左移动是直接的,向右移动是环形的。

所以代码需要检查原点和目标的相对位置,然后适当的计算左右距离。较小的距离决定了 deltaX 的方向和大小。同样的逻辑也适用于 deltaY。

然后如果 deltaX 和 deltaY 具有相同的大小,我们沿着对角线移动。否则,我们会朝 delta 大的方向移动。

private int ComputeDelta(int src, int dst, int mapSize)
{
    int increasing, decreasing;
    if (dst >= src)
    {
        increasing = dst - src;               // increasing direction is direct
        decreasing = (mapSize + src) - dst;   // decreasing direction is toroidal
    }
    else
    {
        increasing = (mapSize + dst) - src;   // increasing direction is toroidal
        decreasing = src - dst;               // decreasing direction is direct
    }

    if (increasing <= decreasing) { return  increasing; }
    else                          { return -decreasing; }
}

public Position GetNextStepTowards(Position origin, Position target)
{
    Position nextStep = new Position(0, 0);

    // compute the distances
    int dx = ComputeDelta(origin.X, target.X, mapXSize);
    int dy = ComputeDelta(origin.Y, target.Y, mapYSize);

    // keep the dominant distance, and clear the other distance
    // keep both if they're equal
    if      (dx*dx > dy*dy) { dy = 0; }
    else if (dx*dx < dy*dy) { dx = 0; }

    // normalize the distances so they are -1, 0, or 1
    nextStep.X = dx.CompareTo(0);
    nextStep.Y = dy.CompareTo(0);

    return nextStep;
}

【讨论】:

  • 感谢您分享这个解决方案,创造奇迹,而且比我想出的更优雅
【解决方案2】:

所以过了一会儿,我想出了这个解决方案,在我看来有点笨拙:

public Position GetNextStepTowards(Position origin, Position target)
{
    // Returned Position
    Position nextStep = new Position(0, 0);

    int dx = target.X - origin.X;
    int dy = target.Y - origin.Y;

    // Toroidal distance
    if (dx > mapXSize / 2) dx = mapXSize - dx;
    if (dy > mapYSize / 2) dy = mapXSize - dy;

    // First verify whether the difference in positions is 
    // greater on the X or Y axis.
    // Then check if the target is lower/higher/forwards/backwards
    if (MathF.Pow(dx, 2) > MathF.Pow(dy, 2))
    {
        if (dx > 0) nextStep.X = 1;
        else if (dx < 0) nextStep.X = -1;
    }
    else if (MathF.Pow(dy, 2) > MathF.Pow(dx, 2))
    {
        if (dy > 0) nextStep.Y = 1;
        else if (dy < 0) nextStep.Y = -1;
    }

    // If the difference in the X and Y axis are the same, 
    // move diagonally
    // use CompareTo do decide what direction in specific.
    else if ((int)MathF.Pow(dx, 2) == (int)MathF.Pow(dy, 2))
    {
        nextStep.X = 1 * target.X.CompareTo(origin.X);
        nextStep.Y = 1 * target.Y.CompareTo(origin.Y);
    }

    return nextStep;
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-12-15
    • 1970-01-01
    • 2022-01-14
    • 1970-01-01
    • 1970-01-01
    • 2023-03-17
    相关资源
    最近更新 更多