yufeih wrote:
There is a GetEdges method in PathGraph, you can modify that to support wrapping. Also update the GeHeuristicsValue method to return a correct distance when positions are wrapped.
I dont understand what i can modify in these methods. It looks very hard for me :((
Please explain me - I dont find any other source about this stuff.
/// <summary>
/// Gets all the adjacent edges of the specified node.
/// </summary>
public int GetEdges(int node, GraphEdge[] edges, int startIndex)
{
int count = 0;
int x = node % SegmentCountX;
int y = node / SegmentCountX;
GraphEdge edge;
edge.From = node;
if (x > bounds.Left && data[x - 1 + y * SegmentCountX] == 0)
{
edge.To = y * SegmentCountX + x - 1;
edge.Cost = 1.0f;
edges[startIndex++] = edge;
count++;
}
if (x < bounds.Right - 1 && data[x + 1 + y * SegmentCountX] == 0)
{
edge.To = y * SegmentCountX + x + 1;
edge.Cost = 1.0f;
edges[startIndex++] = edge;
count++;
}
if (y > bounds.Top && data[x + (y - 1) * SegmentCountX] == 0)
{
edge.To = (y - 1) * SegmentCountX + x;
edge.Cost = 1.0f;
edges[startIndex++] = edge;
count++;
}
if (y < bounds.Bottom - 1 && data[x + (y + 1) * SegmentCountX] == 0)
{
edge.To = (y + 1) * SegmentCountX + x;
edge.Cost = 1.0f;
edges[startIndex++] = edge;
count++;
}
if (x > bounds.Left && y > bounds.Top && data[x - 1 + (y - 1) * SegmentCountX] == 0)
{
edge.To = (y - 1) * SegmentCountX + x - 1;
edge.Cost = 1.4142135f;
edges[startIndex++] = edge;
count++;
}
if (x > bounds.Left && y < bounds.Bottom - 1 && data[x - 1 + (y + 1) * SegmentCountX] == 0)
{
edge.To = (y + 1) * SegmentCountX + x - 1;
edge.Cost = 1.4142135f;
edges[startIndex++] = edge;
count++;
}
if (x < bounds.Right - 1 && y > bounds.Top && data[x + 1 + (y - 1) * SegmentCountX] == 0)
{
edge.To = (y - 1) * SegmentCountX + x + 1;
edge.Cost = 1.4142135f;
edges[startIndex++] = edge;
count++;
}
if (x < bounds.Right - 1 && y < bounds.Bottom - 1 && data[x + 1 + (y + 1) * SegmentCountX] == 0)
{
edge.To = (y + 1) * SegmentCountX + x + 1;
edge.Cost = 1.4142135f;
edges[startIndex++] = edge;
count++;
}
return count;
}
/// <summary>
/// Gets the heuristic value used by A star search.
/// </summary>
public float GetHeuristicValue(int current, int end)
{
int xx = current % SegmentCountX - end % SegmentCountX;
int yy = current / SegmentCountX - end / SegmentCountX;
return (float)Math.Sqrt(xx * xx + yy * yy);
}
|