Path finder java pathfinder library processing

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.

1 Like

If I understand your question right, you can simply remove the nodes where you want a wall (or another object).
So you can do something like this:

void addWall(int x, int y) {
  graph.removeNode(x + y * 24);
}

Consider replacing “24” with a variable, so if you want to change the size of your game, you can do that by just changing the variable.
Anyway, I hope that helped :slight_smile:

1 Like

OK, I tried remove node but it didn’t work. The path still goes through the node.

Could you please post the code you used to remove the node?
I guess your problem is that you remove the node after you initialize the edges and nodes arrays, so it works, but it doesn’t get displayed.
Try removing the node right after createGraph() in your setup function.

I moved the remove around & finally got it to work & now I’m hoping you know a way to make my enemy follow the path as well. I had it in draw then moved it right before the edges are made. I tried googling pathfinder but the tutorials won’t come up on my computer from their site.

Please provide your code and show your attempt. Brek your problem into smaller steps.

Kf

I don’t even know where to start to make the enemy follow the path that the pathfinder made.

In that case, add your updated code so that people can give advice based on your latest. You should also say something about what an enemy is. Do you have an enemy class? Before making the enemy follow a particular generated path, maybe you need an enemy that can follow any path – or go anywhere, for that matter, like travel to point A.


In order to better understand what you are trying to do, I also read your previous thread:

Atm I’m using a sx, sy, x, y system to make them go where I wanna go. Basically all they can do is go in straight lines not avoid walls.