Is this a bug, or what am I doing wrong?
https://jsfiddle.net/Lsw18r3u/9/ Can anyone help me figure what is going on here? Try clicking on "8,5" and then "8,8".
The shortest path should just be a straight line to the right but instead it makes a huge loop and goes through blockers(grey squares).
It paths down from squares that are double wide fine. From 8,4 to 11,5 is fine, for example. But from 9,12 to 11,12 it is unpathable.
It seems there is an index being off by 1 somewhere, but it doesn't seem like the problem is with my code, is it? I'm using 0-start indices.
Just a guess - I'm betting on the grid being transposed. How it is rendered vs how pathfinding understands it are transposed. If this suggestion doesn't fix the issue, reduce the size of the grid so that I can take a look.
imor, if you look at the grid in the JS the html/css display of it matches it correctly.
https://jsfiddle.net/Lsw18r3u/15/
There seems to be worth problems with an oblong grid, giving a " this.nodes[e] is undefined" error if you click to the right side. On the left side you can see the same incorrect pathing and going through walls.
I thought I might be supplying x/y wrong but I flipped my pos to cords function, which matched the cords text on the squares, to be the other way around but that just makes things worse so I don't think that's it.
I'll say again: simplify your problem, maybe a 3x3 grid or something and the problem will most likely pop out, and it will be far easier to debug. I'll try to take a look if I find some time, no promises though.
https://jsfiddle.net/Lsw18r3u/16/ Alright. Well you can edit the fiddle to try different grids. It's only 60 lines of code, which is as little as I could make it to display the problem and translate index to cords and vice versa. It's still is messed up on this 4x3 one
And to add, it's basically just what's on the github readme. I just added visuals and otherwise used the same code. This is for the latest 0.4.18 version
The readme example grid also doesn't work and often throws a this.nodes[e] error https://jsfiddle.net/Lsw18r3u/18/
By contrast, https://github.com/bgrins/javascript-astar works fine with the same code wrapped around it. https://jsfiddle.net/jzqkmqvb/3/
They appear to have roughly the same API except that the other flips it around so that 0 is blocking and 1(or higher) is pathable. It really seems like a bug with PathFinding.js to me when I basically copy the docs and it doesn't work.
There is a bug with my code with starting at 0,0 but the rest is a problem with Pathfinding.js it would seem unless my copying the docs is using it wrong.
Or can you show a working example? Because simply copying the example in the readme doesn't work with 0.4.18 and the latest NPM version. It gives wrong paths, no path, or that this.nodes[e] is undefined error.
You have switched the x and y coordinates. x increments in right direction and y increments towards the bottom, while in your code it's the other way around. Take a look at this corrected fiddle: https://jsfiddle.net/Lsw18r3u/23/ and here's your original big map with correct code: https://jsfiddle.net/Lsw18r3u/24/ For you reference the code that I've changed is the following:
costMatrix.forEach(function(line, x){
line.forEach(function(cost, y){
var $square = $('<div class="square">'+y+','+x+'</div>').appendTo( $matrix );
if( cost === 1 ){
$square.addClass('blocker');
}
});
});
posToCords = function(pos){
return [
pos % matrixWidth,
~~(pos / matrixWidth),
];
}
cordsToPos = function(cords){ //array
return (cords[1] * matrixWidth) + cords[0] ;
}
Ah okay, thanks!
I did try flipping them and now I realize I flipped the input but not the output when I did so. I still like to stick to x,y not y,x internally.