Jump to content

Easystar.js Pathfinding Help


27thColt
 Share

Recommended Posts

So I kinda need a bit of help with pathfinding. I have been using the Easystar.js library as well as this pathfinding plugin for Phaser to do pathfinding for my topdown game.

I ended up with this in my update function:

testenemy.forEach(function(item) {

	//pathfinding portion
	let tilemap = teststageMap;
   	 pathfinder.setCallbackFunction(function(path) {
        item.path = path || [];

      	for(i = 0; i < item.path.length; i++) {
      		let goingX = item.path[i].x;
      		let goingY = item.path[i].y;

      		this.game.physics.arcade.moveToXY(item, goingX * 32, goingY * 32, 75);
        }


    });

	pathfinder.preparePathCalculation([Math.round(item.x / 32), Math.round(item.y / 32)], [Math.round(testplayer.x / 32), Math.round(testplayer.y / 32)]);
    pathfinder.calculatePath();

}, this);

However instead the enemy following a path around walls to get to the target player, the enemy just blindly takes a straight path towards the player so it wouldn't be able to get to me if there were any walls in it's path.

 

Please help! I have been stuck at this problem for days and i don't know what to do!

Link to comment
Share on other sites

  • 2 weeks later...
On 7/10/2016 at 1:41 PM, thefootballguy said:

Look at this guy implementation of easystar. By all means, I am no expert, however, he has source code, examples, and if you use google translate a pretty good explanation.

http://apprendre-le-js.com/mmo-phaser-express-socket-par-1/

 

Thank you! It helped quite a bit! But the problem itself was different from what I was thinking, which was the for loop itself, so I changed it. 

However a new problem arises where enemies get stuck at corners. Diagonals are already set to false in the plugin so I don't know what is wrong.

New Code:

//enemy actions
testenemy.forEach(function(item) {

	//pathfinding portion
	let tilemap = teststageMap;
   	 pathfinder.setCallbackFunction(function(path) {
        item.path = path || [];

		goingX = item.path[1].x;
      	goingY = item.path[1].y;

      	finalX = item.path[item.path.length - 1].x;
      	finalY = item.path[item.path.length - 1].y;

      	console.log("Going to: " + (goingX * 32) + ", " + (goingY * 32) + "| Final: " + (finalX * 32) + ", " + (finalY * 32));
      				
      	console.log(item.x + " " + item.y);

		this.game.physics.arcade.moveToXY(item, goingX * 32, goingY * 32, 75);

      	if ( (item.x >= goingX * 32 - 1 && item.x <= goingX * 32 + 1) && (item.y >= goingY * 32 - 1 && item.y <= goingY * 32 + 1) ) {
      		item.path.splice(0, 0);
      	}




    });

	pathfinder.preparePathCalculation([Math.round(item.x / 32), Math.round(item.y / 32)], [Math.round(testplayer.x / 32), Math.round(testplayer.y / 32)]);
    pathfinder.calculatePath();

}, this);

Any help?

Link to comment
Share on other sites

 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...