# Moving unit along a computed path

I am generating a path using pathfinding.js. This returns a path of node coordinates from a matrix. Example:

var matrix = [[0,0,0,0], //0 are passable, 1 are obstacles
[0,1,1,0],
[0,1,1,0],
[0,0,0,0]]

var path = [[0,0],[0,1],[0,2],[0,3],[1,3]] etc.


I now wish to convert this to path to something like, takeoff(), turnLeft(), forward(4) etc.

What this process called, I have had a look at many programming books and guides but they seem to ignore this method or miss it out. I also had a go at it myself but can't figure out the direction of unit that is being moved along the path.

You need to “parse” the string of positions using a state, the orientation of your plane/car/bomberman/boat/... Let say you represent direction by unitary vector in the direction; e.g. north is [0,1], east is [1,0], south is [0,-1], west is [-1,0]

Now you have the direction of the car as a state during parsing, which is initialize to the current direction of the car. You can now simply feed the path nodes one by one, compute where to go for next step and remove nodes of the list when you move onto them.

A simple pseudo code would be :

orientation_state = plane.orientation;
position_state = path.pop();
action_sequence = [];

while( path != [] ) {
next_position = path.pop();
shift = next_position - position_state;
if( shift != orientation_state ){
orientation_state = shift;
action_sequence.append("turn " + shift + " !");
}
action_sequence.append("forward !");
position_state = next_position;
}