No Backtracking

Pathing with no backtracking

This simple "pathfinding" method, which I call No Backtracking, is a method I previously developed for use in a game where an unintelligent NPC chases the player. As such, this method is not really pathfinding, but can appear to act in this way in many circumstances.

View example

This method works as follows when the NPC is given a target coordinate:

  • Each of the neighbouring tiles surrounding the NPC is valued based on:
    1. Can the tile be moved to? (ie; not a wall, outside map bounds, or otherwise impassable)
    2. Distance of the tile from the destination
    3. If the tile is in our movement history, and if so how far back in the list? (older tiles are scored less than newer tiles)
  • The list of available tiles is then sorted, and the lowest scored tile is set as the current destination of the NPC
  • The destination is put on the end of the history list
  • If the history is longer than the maxHistory for the NPC, an entry is shifted (removed) from the beginning of the list
  • The method repeats (indefinitely) until the target is reached

This also means the NPC may run madly backwards and forwards, unable to pass a confusing area of terrain. In some circumstances, this style of behaviour can be desireable.

To begin with, our Character needs some specific properties:

var character = {
	from		: [0, 0],
	to			: [0, 0],
	target		: [0, 0],
	history		: [],
	maxHistory	: 20,

	update		: function(timeElapsed) { ... }

When the Character has moved to a new map cell, it performs the following check to see if it needs to keep moving to reach its destination:

if(this.from[0]![0] || this.from[1]![1])

We then create a temporary array, n, to which we add each of the neighbouring tiles if they fall within map bounds, and are a tile type we can move on:

	var n = new Array();

	if(this.from[0]>0 && map[((this.from[1]*mapW)+this.from[0]-1)]==1)
		{ n.push([this.from[0]-1, this.from[1], 0]); }
	if(this.from[0]<(mapW-1) && map[((this.from[1]*mapW)+this.from[0]+1)]==1)
		{ n.push([this.from[0]+1, this.from[1], 0]); }
	if(this.from[1]>0 && map[(((this.from[1]-1)*mapW)+this.from[0])]==1)
		{ n.push([this.from[0], this.from[1]-1, 0]); }
	if(this.from[1]<(mapH-1) && map[(((this.from[1]+1)*mapW)+this.from[0])]==1)
		{ n.push([this.from[0], this.from[1]+1, 0]); }

Along with the neighbouring tiles x, y coordinates, we add a third entry, which is currently just 0 (zero).

We then loop through all of the tiles in the n array, and find the distance of that tile from the target, which we assign to the third index of the entry:

	for(i in n)
		n[i][2] = getDistance([0],[1], n[i][0], n[i][1]);

We the loop through the history of traversed tiles, and if this tile is in the Characters history, we increase the distance of this tile by 10 plus 10 x [array index].

		for(h in this.history)
			if(n[i][0]==this.history[h][0] && n[i][1]==this.history[h][1])
				n[i][2] = n[i][2] + 10 + (h*10);

If the n is not empty, we first sort it with the lowest score first:

		n.sort(function(v1, v2) { return v1[2]-v2[2]; });

We then set our characters destination tile (to) to the first tile (the tile with the lowest score) in the neighbours list:[0] = n[0][0];[1] = n[0][1];

...and we add this new destination tile to the end of the history array...

		this.history.push([n[0][0], n[0][1]]);

If the history array is longer than maxHistory entries, we remove the first entry, and we're ready to go:

		if(this.history.length > this.maxHistory) { this.history.shift(); }

Additionally, when we give the character a new target, we must remember to clear the history!

setTarget	: function(px, py)
	this.history.length	= 0;			= [px, py];

Page loaded in 0.008 second(s).