项目实训(七)——车辆寻路算法的实现

本文介绍了在Unity项目中实现车辆寻路的代码和算法学习,特别是A*算法的应用。通过像素化地图和曼哈顿距离的概念,详细阐述了A*算法的估价函数和寻路过程,包括Open列表和Close列表的使用,以及如何处理分叉路径。
摘要由CSDN通过智能技术生成

一、前言

在我们小组的项目的城市游戏场景中,我进行了车辆寻路的代码实现,该博客将简单记录一下工作内容。

二、代码实现

PathFinding:

using System.Collections;
using System.Collections.Generic;
using System;
using UnityEngine;
#if UNITY_EDITOR
using UnityEditor;
using UnityEditorInternal;
#endif

namespace PolyPerfect.City
{
   

    public class PathFinding : MonoBehaviour
    {
   
        private BinaryHeap Open =new BinaryHeap(2048);
        private Dictionary<Guid, int> OpenIDs = new Dictionary<Guid, int>();
        private Dictionary<Guid, PathNode> Closed = new Dictionary<Guid, PathNode>();
        [HideInInspector]
        public List<Path> PathList = new List<Path>();
        [HideInInspector]
        public List<Path> wholePath;

        private Vector3 startPoint;
        private Vector3 endPoint;

        private Tile endTile;
        private Tile startTile;
        private Guid endId;

         public List<Path> GetPath(Vector3 start, Vector3 end, PathType pathType)
         {
   
            Open.Clear();
            PathList = new List<Path>();
            Closed.Clear();
            OpenIDs.Clear();

            startPoint = start;
            endPoint = end;

            startTile = FindClosestTile(startPoint, pathType);
            endTile = FindClosestTile(endPoint, pathType);
            List<Path> startPaths;
            if(pathType == PathType.Sidewalk)
            {
   
                startPaths = startTile.sidewalkPaths;
            }
            else
                startPaths = startTile.paths;
            foreach (Path path in startPaths)
            {
   
                float h = CalculateHeuristic(path.pathPositions[path.pathPositions.Count - 1].position);
                float g = Vector3.Distance(transform.position, path.pathPositions[0].position) + Vector3.Distance(transform.position, path.pathPositions[path.pathPositions.Count-1].position);
                PathNode node = new PathNode() {
   path = path, lastNode = null, currentScore = g, score = h +g};
                Open.Insert(node);
                OpenIDs.Add(node.path.Id, 0);
            }
            if(DoBfsSteps())
            {
   
                Closed[endId].path = FindClosestPath(endPoint, Closed[endId].lastNode.path.nextPaths);
                GetPathList(Closed[endId]);
                PathList.Reverse();
                //PathList[0] = FindClosestPath(startPoint, startPaths);
                return PathList;
            }
            else
            {
   
                return null;
            }
         }
        public List<Path> GetPathWithCheckpoints(List<Vector3> checkpoints, PathType pathType)
        {
   
            wholePath = new List<Path>();
            
            for (int i = 1; i < checkpoints.Count ;i++)
            {
   
                startPoint = checkpoints[i-1];
                endPoint 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

LiuFangdi145

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值