OK, so I’ve got where I the path goes from point a to b but idk how to make it avoid objects. I want to make a tower defense game where different maps appear & the enemy gotta find its way through or to the item it wants. Right now my code just makes the shortest line no matter what’s in its way. Here the path finding code:
import pathfinder.*;
// PathFinding_01
Graph graph;
// These next 2 are only needed to display
// the nodes and edges.
GraphEdge[] edges;
GraphNode[] nodes;
GraphNode[] route;
// Pathfinder algorithm
IGraphSearch pathFinder;
// Used to indicate the start and end nodes as selected by the user.
GraphNode startNode, endNode;
void setup() {
size(600, 600);
textSize(8);
// Create graph
createGraph();
// Get nodes and edges
nodes = graph.getNodeArray();
edges = graph.getAllEdgeArray();
// Now get a path finder object
pathFinder = new GraphSearch_Astar(graph);
// Now get a route between 2 nodes
// You can change the parameter values but they must be valid IDs
pathFinder.search(0, 575);
route = pathFinder.getRoute();
}
void draw() {
background(0);
drawGraph();
drawPath();
noLoop();
}
void drawGraph() {
// Edges first
strokeWeight(2);
stroke(180, 180, 200);
for (int i = 0; i < edges.length; i++) {
GraphNode from = edges[i].from();
GraphNode to = edges[i].to();
line(from.xf(), from.yf(), to.xf(), to.yf());
}
// Nodes next
noStroke();
fill(240, 255, 240);
for (int i = 0; i < nodes.length; i++) {
GraphNode node = nodes[i];
ellipse(node.xf(), node.yf(), 6, 6);
text(node.id(), node.xf() + 4, node.yf() - 2);
}
}
void drawPath() {
strokeWeight(10);
stroke(200, 255, 200, 160);
for (int i = 1; i < route.length; i++) {
GraphNode from = route[i-1];
GraphNode to = route[i];
line(from.xf(), from.yf(), to.xf(), to.yf());
}
}
public void createGraph() {
graph = new Graph();
// Create and add node
GraphNode node;
int node_id = 0;
for (int v = 0; v < 24; v++) {
for (int h = 0; h < 24; h++) {
node = new GraphNode(node_id, 12 + h * 25, 12 + v * 24);
graph.addNode(node);
node_id++;
}
}
// Create horizontal edges
for (int v = 0; v < 24; v++) {
for (int h = 1; h < 24; h++) {
graph.addEdge(v * 24 + h-1, v * 24 + h, 0, 0);
}
}
// Create vertical edges
for (int h = 0; h < 24; h++) {
for (int v = 1; v < 24; v++) {
graph.addEdge(v * 24 - 24 + h, v * 24 + h, 0, 0);
}
}
}
So can anyone tell me how to make it avoid walls.