1 decade ago by nuktas
Hi,
I am trying to get some very basic pathfinding to work in a 2d topdown game.
I want the enemies to walk along the x or y axis towards the player, whichever is the shortest. That is, if the enemy is 200 units away horizontally and 120 units away vertically, it should first move along the x axis until 120 units away, and from then on zig-zag towards the player.
Now, this works some of the time, but the monsters move very strangely - they sort of "overshoot" the player and move further along a diagonal axis than they should and then move like a pendulum behind my player while he moves away. I have
attached the relevant code below. Anybody who has an idea of why this occurs?
I would share my game for you too see, but I don't know how I can do that, really. Any help with regards to that would be appreciated.
Pathfinding logic
Movement logic
I am trying to get some very basic pathfinding to work in a 2d topdown game.
I want the enemies to walk along the x or y axis towards the player, whichever is the shortest. That is, if the enemy is 200 units away horizontally and 120 units away vertically, it should first move along the x axis until 120 units away, and from then on zig-zag towards the player.
Now, this works some of the time, but the monsters move very strangely - they sort of "overshoot" the player and move further along a diagonal axis than they should and then move like a pendulum behind my player while he moves away. I have
attached the relevant code below. Anybody who has an idea of why this occurs?
I would share my game for you too see, but I don't know how I can do that, really. Any help with regards to that would be appreciated.
Pathfinding logic
getAction: function(entity) {
this.entity = entity;
var player = ig.game.getEntitiesByType('EntityPlayer')[0];
var distance = this.entity.distanceTo(player);
var x_dist = Math.abs(player.pos.x - this.entity.pos.x);
var y_dist = Math.abs(player.pos.y - this.entity.pos.y);
if (distance < 15) {
return this.doAction(ig.ai.ACTION.Explode);
}
if (distance < 150) {
if (x_dist > y_dist) {
if (this.entity.pos.x > player.pos.x) {
return this.doAction(ig.ai.ACTION.MoveLeft);
} else {
return this.doAction(ig.ai.ACTION.MoveRight);
}
} else {
if (this.entity.pos.y > player.pos.y) {
return this.doAction(ig.ai.ACTION.MoveUp);
} else {
return this.doAction(ig.ai.ACTION.MoveDown);
}
}
}
return this.doAction(ig.ai.ACTION.Rest);
},
doAction: function(action){
this.lastAction = action;
return action;
}
Movement logic
update: function(){
var action = ai.getAction(this);
switch (action) {
case ig.ai.ACTION.Rest:
this.vel.x = 0;
this.vel.y = 0;
break;
case ig.ai.ACTION.MoveLeft:
this.vel.x = -this.maxVel.x;
break;
case ig.ai.ACTION.MoveRight:
this.vel.x = this.maxVel.x;
break;
case ig.ai.ACTION.MoveUp:
this.vel.y = -this.maxVel.y;
break;
case ig.ai.ACTION.MoveDown:
this.vel.y = this.maxVel.y;
break;
case ig.ai.ACTION.Explode:
this.explode();
break;
default:
this.vel.x = 0;
this.vel.y = 0;
break;
}
this.parent();
}
