i am working on pathfinding script for my future project with square grid based nodes. I am using the typical “A star” method. so far my script seems to be working nicely. In some situations there are unnecessary curves along the output path. Is this typical for astar? Any easy way around this without troubling the CPU with even more loops of calculations?
the grey is my closed list and the darker blue is my open list. The FINAL path is shown in light blue!!!
I cant fit the entire code. the g and h values are stored in an array of arrays to represent two dimentions. in my code it is land which is an array.
if(gopath==1){
land[point.x][point.z].path=5;
int nogo;
i=8;
//==============================search around current position==============================
while(i>0){i--;
if(land[point.x+cross_.x][point.z+cross*.z].type>-1){*_
* nogo = 0;*
* b=false;*
* if(i>3){i2=14;}else{i2=10;}*
if(land[point.x+cross.x][point.z+cross.z].path==0){land[point.x+cross_.x][point.z+cross*.z].path=1;nogo=1;b=true;}
if(land[point.x+cross.x][point.z+cross.z].g>land[point.x][point.z].g+i2){nogo=1;}*_
* if(nogo==1){*
land[point.x+cross_.x][point.z+cross*.z].g=land[point.x][point.z].g+i2;
land[point.x+cross.x][point.z+cross.z].parent=new two(point.x,point.z);
if(b){olist.Add(new two (point.x+cross.x,point.z+cross.z));
Physics.Raycast(new Vector3((float)point.x+cross.x,-10f,(float)point.z+cross.z),new Vector3(0,1,0f), out hit);
hit.transform.renderer.material.color=Color.blue;}
}}}*_
* //===========================check open list for next move=====================================*
* i=olist.Count;*
* f=9999f;i3=0;*
* while(i>0){i–;*
if(land[olist_.x][olist*.z].path!=5){
if(land[olist.x][olist.z].h+land[olist.x][olist.z].f<f){
i3=i;f=land[olist.x][olist.z].h+land[olist.x][olist.z].f;
}}}
point.x=olist[i3].x;
point.z=olist[i3].z;*_
* if (Physics.Raycast(new Vector3((float)olist[i3].x,-10f,(float)olist[i3].z),new Vector3(0,1,0f), out hit)) {*
* hit.transform.renderer.material.color=Color.grey;}*
* land[olist[i3].x][olist[i3].z].path=5;*
olist.RemoveAt(i3);
if(point.x==goal.x){
* if(point.z==goal.z){*
* gopath=2;*
olist.Clear();
* }}}*
//=============trace back the path=====================
* if (gopath==2) {*
* Physics.Raycast(new Vector3((float)point.x,-10f,(float)point.z),new Vector3(0,1,0f), out hit);*
* hit.transform.renderer.material.color=Color.cyan;*
* olist.Add(new two(point.x,point.z));*
* i = land [point.x][point.z].parent.x;*
* i2 = land [point.x][point.z].parent.z;*
* point.x = i;*
* point.z = i2;*
* if (point.x == started.x) {*
* if (point.z == started.z) {*
* gopath=3;*
* olist=shortlist(olist);*
* moving=olist.Count-1;*
* }}*
* }}*