i used algorithm a start to find path with array nods. I have image map and nodes like this:
Red nodes is obstacle, black used to find path. I dont know why this path is to curve. I used this library https://code.google.com/p/a-star-java/source/browse/AStar/src/aStar/?r=7 and I was changed function registerEdges.:
private void registerEdges(ArrayList<Node> nodes)
{
float currentDistX = 0;
float currentDistY = 0;
float distance = 0;
for(int l = 0 ; l < nodes.size(); l++)
{
MINDISTN = Integer.MIN_VALUE;
MINDISTS = Integer.MAX_VALUE;
MINDISTE = Integer.MIN_VALUE;
MINDISTW = Integer.MAX_VALUE;
MINDISTNE = Integer.MAX_VALUE;
MINDISTNW = Integer.MAX_VALUE;
MINDISTSE = Integer.MAX_VALUE;
MINDISTSW = Integer.MAX_VALUE;
Node node = null;
currentDistX = 0;
currentDistY = 0;
//System.out.println("current " + node.x + " " + node.y);
for(int j = 0 ; j < map.size() ; j++)
{
if(l != j)
{
node = map.get(l);
currentDistX = map.get(j).x - node.x;
currentDistY = map.get(j).y - node.y;
if(currentDistX == 0)
{
if(currentDistY < 0)
{
if(currentDistY > MINDISTN)
{
MINDISTN = currentDistY;
node.setNorth(map.get(j));
//System.out.println(currentDist + " n " + map.get(j).x + " " + map.get(j).y);
}
}
else if(currentDistY > 0)
{
if(currentDistY < MINDISTS)
{
//System.out.println(currentDist + " south " + map.get(j).x + " " + map.get(j).y);
MINDISTS = currentDistY;
node.setSouth(map.get(j));
}
}
}
if(currentDistY == 0)
{
if(currentDistX < 0)
{
if(currentDistX > MINDISTE)
{
MINDISTE = currentDistX;
node.setEast(map.get(j));
//System.out.println(currentDist + " e " + map.get(j).x + " " + map.get(j).y);
}
}
else if(currentDistX > 0)
{
//System.out.print("m " + MINDISTRIGHT);
if(currentDistX < MINDISTW)
{
MINDISTW = currentDistX;
node.setWest(map.get(j));
//System.out.println(currentDist + " w " + map.get(j).x + " " + map.get(j).y);
}
}
}
if(currentDistY != 0 && currentDistX != 0)
{
if(currentDistX > 0 && currentDistY > 0)
{
distance = node.calculateDistanceBetweenNods(map.get(j));
if(distance < MINDISTNE)
{
MINDISTNE = distance;
node.setNorthEast(map.get(j));
//System.out.println(currentDist + " e " + map.get(j).x + " " + map.get(j).y);
}
}
else if(currentDistX < 0 && currentDistY > 0)
{
distance = node.calculateDistanceBetweenNods(map.get(j));
if(distance < MINDISTNW)
{
MINDISTNW = distance;
node.setNorthWest(map.get(j));
//System.out.println(currentDist + " e " + map.get(j).x + " " + map.get(j).y);
}
}
else if(currentDistX <= 0 && currentDistY <= 0)
{
distance = node.calculateDistanceBetweenNods(map.get(j));
if(distance < MINDISTSW)
{
MINDISTSW = distance;
node.setSouthWest(map.get(j));
//System.out.println(currentDist + " e " + map.get(j).x + " " + map.get(j).y);
}
}
else if(currentDistX > 0 && currentDistY < 0)
{
distance = node.calculateDistanceBetweenNods(map.get(j));
if(distance < MINDISTSE)
{
MINDISTSE = distance;
node.setSouthEast(map.get(j));
//System.out.println(currentDist + " e " + map.get(j).x + " " + map.get(j).y);
}
}
}
}
}
}
This function looks for North,South,West,East,N-East... neighbor node.
Estimate Path:
public float getEstimatedDistanceToGoal(float startX, float startY, float goalX, float goalY) {
float dx = goalX - startX;
float dy = goalY - startY;
//float result = (float) (Math.sqrt((dx*dx)+(dy*dy)));
//Optimization! Changed to distance^2 distance: (but looks more "ugly")
float result = (float) (dx*dx)+(dy*dy);
return (float) Math.sqrt(result);
}
Bad connection current nodes with neighbor nodes.
Example:
Some nodes have one-way connection (image up).
Node 2 have neighbor node 1, node 1 don't have neighbor node 2.