【问题标题】:Random Path generation in a game游戏中的随机路径生成
【发布时间】:2019-11-06 05:14:21
【问题描述】:

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

我的问题是“还有哪些其他类型的随机路径生成算法适合我的情况?” (生成2个固定点之间的随机路径)

【问题讨论】:

  • 你的意思是在两个随机点之间使用最佳路径,还是在两个固定点之间使用随机路径?
  • 在2个固定点之间生成路径

标签: unity3d path 2d procedural-generation


【解决方案1】:

我能想到的最简单的解决方案是生成航路点节点,这些节点知道它们连接到哪些其他节点,然后随机选择一个要遵循的连接(可能通过一些启发式方法朝着你的目标前进)

例如,

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);
    }
}

此外,“天下没有免费的午餐”也适用于此。建造这样的东西总是要付出代价的。您要么花时间学习 A*,要么花时间手动创建路径...

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-11-19
    • 1970-01-01
    • 1970-01-01
    • 2021-02-06
    • 1970-01-01
    • 1970-01-01
    • 2015-06-19
    • 2015-01-05
    相关资源
    最近更新 更多