C# 定制的*路径查找不是';工作不太好
我正在用坐标在地图上做一个寻路算法,用于一个有河流的项目。我看了a*是如何工作的,我开始理解它,所以我用自己的代码编写了它。问题是这根本不起作用。探路者只是在河的前两个方格里,然后就放弃了。我不知道这个问题 这是我的密码:C# 定制的*路径查找不是';工作不太好,c#,unity3d,path-finding,C#,Unity3d,Path Finding,我正在用坐标在地图上做一个寻路算法,用于一个有河流的项目。我看了a*是如何工作的,我开始理解它,所以我用自己的代码编写了它。问题是这根本不起作用。探路者只是在河的前两个方格里,然后就放弃了。我不知道这个问题 这是我的密码: using System; using System.Collections; using System.Collections.Generic; using UnityEngine; using System.Linq; public class APath {
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System.Linq;
public class APath
{
public bool[,] open;
private bool[,] closed;
private float width;
private float height;
public int currentX, currentY;
public void FindPath(int startX, int startY, int endX, int endY)
{
open = new bool[75,75];// stores path
open[startX, startY] = true;
bool[,] opens = open;
closed = new bool[75,75] ;
currentY = startY;
currentX = startX;
bool end = false;
for(int l = 0; l< 1000; l++) //while loop went on forever. Had to use for loop
{
float[][] adjacent = Adjacent(currentX,currentY,endX,endY,startX,startY);//gets adjacent squares
float min = adjacent[0][2];//creates temp min
int per = 0;// storage of refrence for later
for(int i = 0; i < 8; i++)// checks adjacent squares
{
if (adjacent[i][2] < min)//if there is a new min
{
min = adjacent[i][2];// new min
per = i; // for later use
}
}
if(currentX == endX && currentY == endY)// makes sure that adjacent isn't at end place
{
open[endX, endY] = true;
}
currentX = Mathf.RoundToInt((int)adjacent[(int)per][0]);//new current
currentY = Mathf.RoundToInt(adjacent[(int)per][1]);
opens[currentX, currentY] = true;// tracks data
closed[currentX, currentY] = false;//tracks data
}
open = opens;
}
float[][] Adjacent (int currentX, int currentY, float endX, float endY, float startX, float startY)
{
//creating adjacent squares areound current square
float[][] adjacent = new float[4][];
adjacent[0] = new float[3] { currentX + 1, currentY +0, HandG( currentX + 1, currentY,endX, endY, startX, startY) };
adjacent[1] = new float[3] { currentX + 1, currentY + -1, HandG(currentX + 1, currentY + -1, endX, endY, startX, startY) };
adjacent[2] = new float[3] { currentX + 0, currentY + -1, HandG(currentX + 0, currentY + -1, endX, endY, startX, startY) };
adjacent[3] = new float[3] { currentX + -1, currentY +- 01, HandG(currentX + -1, currentY +-1, endX, endY, startX, startY) };
adjacent[4] = new float[3] { currentX + -1, currentY + 0, HandG(currentX + -1, currentY, endX, endY, startX, startY) };
adjacent[5] = new float[3] { currentX + -1, currentY + 01, HandG(currentX + -1, currentY + 1, endX, endY, startX, startY) };
adjacent[6] = new float[3] { currentX, currentY + 1, HandG(currentX , currentY + 1, endX, endY, startX, startY) };
adjacent[7] = new float[3] { currentX + 1, currentY + 1, HandG(currentX + 1, currentY + 1, endX, endY, startX, startY) };
return adjacent;
}
float HandG (float currentX, float currentY,float endX, float endY, float startX, float startY )
{
//creates g of a*
float g = Mathf.Sqrt(Mathf.Pow(currentX + -startX, 2) + Mathf.Pow(currentY + -startY, 2));
//creates h of a*
float h = Mathf.Sqrt(Mathf.Pow(currentX + -endX, 2) + Mathf.Pow(currentY + -endY, 2));
//creates f of a*
float f =g + h;
return f;
}
}
使用系统;
使用系统集合;
使用System.Collections.Generic;
使用UnityEngine;
使用System.Linq;
公共类公寓
{
公共图书馆开放;
私人住宅[,]关闭;
私有浮动宽度;
私人浮动高度;
公共int currentX,currentY;
公共void FindPath(int startX、int startY、int endX、int endY)
{
open=new bool[75,75];//存储路径
打开[startX,startY]=真;
bool[,]打开=打开;
关闭=新布尔值[75,75];
电流y=星形;
currentX=startX;
bool end=false;
for(int l=0;l<1000;l++)//while循环永远继续。必须使用for循环
{
float[][]相邻=相邻(currentX,currentY,endX,endY,startX,startY);//获取相邻的正方形
浮点最小值=相邻的[0][2];//创建临时最小值
int per=0;//为以后存储引用
for(int i=0;i<8;i++)//检查相邻的正方形
{
if(相邻[i][2]
使用此
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Rivers : MonoBehaviour
{
// Start is called before the first frame update
public Color[] MakeRiver (Color[] color, int mapHeight, int mapWidth)
{
Color[] colorss = color;// makes color[]
APath path = new APath();
path.FindPath(20,20,9,19);// makes path. Set certain values for testing
bool[,] open = path.open;// gets path from that class
for(int y = 0;y < mapHeight; y++)
{
for(int x = 0; x< mapWidth; x++)
{
if(open[x,y] == true)
{
colorss[y * mapWidth + x] = Color.red;// shows the river.
}
}
}
return colorss;
}
}
使用系统集合;
使用System.Collections.Generic;
使用UnityEngine;
公共级河流:单一行为
{
//在第一帧更新之前调用Start
公共颜色[]MakeRiver(颜色[]颜色,整数贴图高度,整数贴图宽度)
{
Color[]colorss=Color;//生成颜色[]
磷灰石路径=新磷灰石();
FindPath(20,20,9,19);//生成路径。为测试设置某些值
bool[,]open=path.open;//从该类获取路径
对于(int y=0;y
如果有人能看到这个问题,我将非常感激。感谢您阅读我的问题,祝您度过愉快、精彩的一天。老实说,这里有太多的问题,很难帮助您。我真的觉得如果你阅读更多关于a*的文档并遵循指南,那将是最好的。无论如何,你的问题的完整答案必须是一个完整的A*指南。我看到一些与a*相关的内容,但所有内容都被错误地实现,抱歉:-(甚至在你自己的评论中也有一些关键指标表明出了问题,事实上你因为无休止的循环而把1000个for循环放进了循环中。)a*上有很多教程,而且,这不在好的列表中,在youtube上搜索一个*codemonkey做得很好,但它是一个简单的算法,可能会让人头脑麻木你有什么特别的理由自己实现一个*吗?实际上有几十个免费的优秀的Unity实现。这是我的最爱。我自己实现的,因为我没有使用网格。还有,@Knoop所说的有很多问题是什么意思?