Hi, I am trying to make a pathfinding system, and the pathfinding part is done however I don’t know how to change the start or end point. Down below is some of my code. Would be nice if i could get some inspiration how to move the end og start point.
int rows=99;
int cols=99;
int startx=0;
int starty=0;
float bred=10;
float hoj=10;
boolean run=false;
Cell start;
Cell end;
Cell[][] grid=new Cell[cols][rows];
void setup(){
frameRate(200);
size(1000,1000,P2D);
//fullScreen(P2D);
for(int i=0;i<cols;i++){
for(int j=0;j<rows;j++){
grid[i][j]=new Cell(startx+i*bred,starty+j*hoj,bred,hoj);
}
}
start=grid[0][0]; //start
end=grid[rows-1][cols-1]; //end
start.wall=false;
end.wall=false;
}
void draw(){
for (int i=0; i<cols;i++){
for(int j=0; j<rows;j++){
grid[i][j].display(255,255,255);
}
}
start.display(0,255,0);
end.display(225,125,0);
}
void mouseDragged(){
// if inside preset start or end move it
}
class Cell{
// til pathfinding
float f=0;
float g=0;
float h=0;
// til kontruktion
float i;
float j;
float bredde;
float hojde;
boolean wall=false;
//farve
int rod=255;
int gron=255;
int bla=255;
//previous
Cell previous = null;
//neighbors
Cell[] neighbors = new Cell[0];
//ArrayList<Cell> neighbors = new ArrayList<Cell>();
Cell(float x, float y,float b, float h){
i=x;
j=y;
bredde=b;
hojde=h;
//wall or not
if (random(0,1)<0.40){
wall=true;
}
}
void display(int x, int y, int z){
if (wall){
fill(0);
rect(i,j,bredde,hojde);
}else{
fill(x,y,z);
rect(i,j,bredde,hojde);
}
}
}
Sorry for some of the variable-names. I am danish but i hope you don’t mind. The code shouldn’t be that complicated
Hi @jensemand
Here is the solution I came up with
On the Cell class added a method to know if you are on top of a cell
When the mouse is Pressed on top of a cell + key pressing ‘s’, it defines the new cell as a start.
When the mouse is Pressed on top of a cell + key pressing ‘e’, it defines the new cell as the end cell.
Hope it helps!
There are other ways to do this as well. You can also save the start and end cells to a file so when you restart it knows the last start and end cell locations
int rows=99;
int cols=99;
int startx=0;
int starty=0;
float bred=10;
float hoj=10;
boolean run=false;
Cell start;
Cell end;
Cell[][] grid=new Cell[cols][rows];
int startPosX = 0;
int endPosX = rows-1;
int startPosY = 0;
int endPosY = cols-1;
void setup() {
frameRate(200);
size(1000, 1000, P2D);
//fullScreen(P2D);
for (int i=0; i<cols; i++) {
for (int j=0; j<rows; j++) {
grid[i][j]=new Cell(startx+i*bred, starty+j*hoj, bred, hoj);
}
}
start=grid[0][0]; //start
end=grid[rows-1][cols-1]; //end
start.wall=false;
end.wall=false;
}
void draw() {
for (int i=0; i<cols; i++) {
for (int j=0; j<rows; j++) {
grid[i][j].display(255, 255, 255);
}
}
start.display(0, 255, 0);
end.display(225, 125, 0);
}
void mousePressed() {
for (int i=0; i<cols; i++) {
for (int j=0; j<rows; j++) {
if(grid[i][j].onTop() && key =='s') start = grid[i][j] ;
if(grid[i][j].onTop() && key =='e') end = grid[i][j] ;
}
}
}
class Cell {
// til pathfinding
float f=0;
float g=0;
float h=0;
// til kontruktion
float i;
float j;
float bredde;
float hojde;
boolean wall=false;
//farve
int rod=255;
int gron=255;
int bla=255;
//previous
Cell previous = null;
//neighbors
Cell[] neighbors = new Cell[0];
//ArrayList<Cell> neighbors = new ArrayList<Cell>();
Cell(float x, float y, float b, float h) {
i=x;
j=y;
bredde=b;
hojde=h;
//wall or not
if (random(0, 1)<0.40) {
wall=true;
}
}
void display(int x, int y, int z) {
if (wall) {
fill(0);
} else {
fill(x, y, z);
}
if (onTop()) fill(0, 0, 255);
rect(i, j, bredde, hojde);
}
boolean onTop() {
return (mouseX > i && mouseX < i+bredde &&
mouseY > j && mouseY < j+hojde);
}
}
1 Like