PathFinding.js
PathFinding.js copied to clipboard
Random points *not walkable* - question.
how to generate random points in this grid that cannot be passed through? Currently, if we draw them with a mouse, they are blocked. In view.js, I use a switch and a case 'opened' by throwing in a for loop that generates these elements (blocked grey points) but they are ignored, the user goes through them even though they are on the map. Could somebody help me?
The node already has a flag called "walkable" so after you instantiated the grid you could get some random cells from there and set walkable to false:
var width = 5;
var height = 3;
var grid = new PF.Grid(width, height);
var not_walkable = 3; // lets make 3 fields not walkable, do not make it bigger then width*height or you will run in an endless loop.
var i = 0;
do {
var x = Math.floor(Math.random() * width);
var y = Math.floor(Math.random() * height);
if (!grid.isWalkableAt(x, y)) {
// we catched a field that's already not walkable - retry!
continue;
}
i++;
grid.setWalkableAt(x, y, false);
} while (i < not_walkable);
I don't know if I implemented the code well. I've rewritten the code and added it to the switch, in which case it works... but the elements are still not blocked? Could you help me because I don't know if I'm doing it right? MfG https://github.com/pkkkkkkk123/schoolproject
case 'opened': this.colorizeNode(this.rects[gridY][gridX], nodeStyle.opened.fill); this.setCoordDirty(gridX, gridY, true); var node; var width = 64; var height = 36; var grid = new PF.Grid(width, height); var not_walkable = 100; // lets make 3 fields not walkable, do not make it bigger then width*height or you will run in an endless loop. var i = 0; for (i = 0; i < 2; i++) { gridX = Math.floor(Math.random() * width); gridY = Math.floor(Math.random() * height); node = this.rects[gridY][gridX].clone(); this.zoomNode(node); this.colorizeNode(node, this.nodeStyle.blocked.fill); var x = Math.floor(Math.random() * width); var y = Math.floor(Math.random() * height); grid.setWalkableAt(x, y, false);
}
break;
You are doing this at the wrong position. You create a new grid while you already have a grid in the controller.js, I suggest you do that there. Also you use a for loop which might not work for you because Math.random might pick the same tile again (not very lightly but might happen).
For example this would create 30 random walls whenever you press the "Usun Wszystko" button you can overwrite your buildNewGrid-function with this (after you undo your changes to the switch):
buildNewGrid: function() {
var width = this.gridSize[0];
var height = this.gridSize[1];
this.grid = new PF.Grid(width, height);
var node;
var i = 0;
var not_walkable = 30; // lets make 30 fields not walkable, do not make it bigger then width*height or you will run in an endless loop.
var i = 0;
do {
var x = Math.floor(Math.random() * width);
var y = Math.floor(Math.random() * height);
if (!this.grid.isWalkableAt(x, y)) {
// we catched a field that's already not walkable - retry!
continue;
}
i++;
View.setWalkableAt(x, y, false);
this.grid.setWalkableAt(x, y, false);
console.log(x, y);
} while (i < not_walkable);
},