Shows a simple graph type generating a polar graph.
using UnityEngine;
using Pathfinding;
using Pathfinding.Serialization.JsonFx;
[JsonOptIn]
public class PolarGraph : NavGraph {
[JsonMember]
public int circles = 10;
[JsonMember]
public int steps = 20;
[JsonMember]
public Vector3 center = Vector3.zero;
[JsonMember]
public float scale = 2;
PointNode[] nodes;
PointNode[] CreateNodes (int count) {
var created = new PointNode[count];
for (int i = 0; i < created.Length; i++) {
created[i] = new PointNode(active);
}
return created;
}
static Vector3 CalculateNodePosition (int circle, float angle, Matrix4x4 matrix) {
var pos = new Vector3(Mathf.Sin(angle), 0, Mathf.Cos(angle));
pos *= circle;
pos = matrix.MultiplyPoint(pos);
return pos;
}
public override void ScanInternal (
OnScanStatus statusCallback) {
nodes = CreateNodes(circles*steps);
SetMatrix(Matrix4x4.TRS(center, Quaternion.identity, Vector3.one*scale));
nodes[0].position = (Int3)matrix.MultiplyPoint(Vector3.zero);
float anglesPerStep = (2*Mathf.PI)/steps;
for (int circle = 1; circle < circles; circle++) {
for (int step = 0; step < steps; step++) {
float angle = step * anglesPerStep;
Vector3 pos = CalculateNodePosition(circle, angle, matrix);
GraphNode node = nodes[(circle-1)*steps + step + 1];
node.position = (Int3)pos;
}
}
for (int circle = 1; circle < circles; circle++) {
for (int step = 0; step < steps; step++) {
PointNode node = nodes[(circle-1)*steps + step + 1];
int numConnections = (circle < circles-1) ? 4 : 3;
var connections = new GraphNode[numConnections];
var connectionCosts = new uint[numConnections];
int connId = (circle-1)*steps + (step < steps-1 ? step+1 : 0) + 1;
connections[0] = nodes[connId];
connId = (circle-1)*steps + (step > 0 ? step-1 : steps-1) + 1;
connections[1] = nodes[connId];
if (circle > 1) {
connId = (circle-2)*steps + step + 1;
} else {
connId = 0;
}
connections[2] = nodes[connId];
if (numConnections == 4) {
connId = circle*steps + step + 1;
connections[3] = nodes[connId];
}
for (int q = 0; q < connections.Length; q++) {
connectionCosts[q] = (uint)(node.position-connections[q].position).costMagnitude;
}
node.connections = connections;
node.connectionCosts = connectionCosts;
}
}
PointNode centerNode = nodes[0];
centerNode.connections = new GraphNode[steps];
centerNode.connectionCosts = new uint[steps];
for (int step = 0; step < steps; step++) {
centerNode.connections[step] = nodes[1+step];
centerNode.connectionCosts[step] = (uint)(centerNode.position-centerNode.connections[step].position).costMagnitude;
}
for (int i = 0; i < nodes.Length; i++) {
nodes[i].Walkable = true;
}
}
if (nodes == null) return;
for (int i = 0; i < nodes.Length; i++) {
if (!del(nodes[i])) {
return;
}
}
}
}