游戏中的随机路径生成

Random Path generation in a game

我想制作一个 2D 游戏,在 2D 屏幕上的两点之间生成频繁的随机路径。我已经阅读了 A* 算法 + 随机障碍物生成来创建路径,但是该算法学习起来似乎有点耗时。

我的问题是"What are the other types of random path generation algorithms that are appropriate for my situation?"(在两个固定点之间生成随机路径)

我能想到的最简单的解决方案是生成知道它们连接到哪些其他节点的路点节点,然后随机选择要遵循的连接(可能有一些启发式方法来实现您的目标)

例如,

using System.Linq;
public class Waypoint : MonoBehaviour{
    public Waypoint[] Connections;

    public Waypoint Next( Waypoint previous, Waypoint finalDestination) {

        if (this == finalDestination) return null; // You have arrived

        var possibleNext = Connections.Where(m => m != previous && CheckHeuristic(m, finalDestination)); // Dont go backwards, and apply heuristic

        if (possibleNext.Count() == 0) throw new System.ApplicationException("No exitable paths from Waypoint"); // Error if no paths available

        possibleNext = possibleNext.OrderBy( m => Random.Range(0f, 1f)); // 'shuffle'

        return possibleNext.First(); // Grab first 'random' possible path
    }

    private bool CheckHeuristic(Waypoint candidate, Waypoint finalDestination) {
        // Basic 'is not farther' check
        return Vector3.Distance(candidate.transform.position, finalDestination.transform.position) <= Vector3.Distance(this.transform.position, finalDestination.transform.position);
    }
}

此外,"there's no such thing as a free lunch" 适用于此。建造这样的东西总是要付出代价的。您要么花时间学习 A*,要么花时间手动创建路径...