[source lang="cpp"]
speed_per_tick = 1; //constant speed you want the object to move at
delta_x = x_goal - x_current;
delta_y = y_goal - y_current;
goal_dist = sqrt((delta_x * delta_x) + (delta_y * delta_y));
if (goal_dist > speed_per_tick)
{
Ratio = speed_per_tick / goal_dist;
x_move = Ratio * delta_x;
y_move = Ratio * delta_y;
new_x_pos = x_move + x_current;
new_y_pos = y_move + y_current;
}
Else
{
new_x_pos = x_goal;
new_y_pos = y_goal;
}
[/source]
It works great when the player isn't moving, but when he does move, it seems ok for a bit but sometimes once in awhile I'll notice some jitteryness with the monster as its following the path when the A* Path gets cleared or changed. Which leaves some questions I can't seem to find on google.
First of all, when the player moves and passes the next node, I obviously need to clear the A* Path and recalculate, but do I need to also clear and recalculate it when the monster reaches the next node?
Secondly It seems the path following code shown above doesn't work too well with diagnal cause the monster gets locked in between the two nodes and bounces back and forth as the player moves, but stops when the player stands still and the monster continues following the path as nortmal. And believe me, Its not the A* pathfinding code I have thats causing this cause the last following code I used didnt lock the sprite between nodes. Yet I had to change it cause I had too many problems with the last code I used.
Third, I found that using the top left of every tile as a node brought about too many problems. Moving right and down is normal. But when moving left and up, it tends to skip tiles, or drags against the wall when about to turn. I basically had to rethink this, draw some tiles on a sheet of paper, make little thick squares, and solve this on my own. My solution was to use the center of the tiles as nodes. As a result it moved normal in all 8 directions. In other words, any side of the player passing the center of a tile will be the next node. My question is that would this be the correct way of doing this? I'm sure many of you had similar problems. And I'm looking for solutions. Thanks in advance.
![smile.png](http://public.gamedev.net//public/style_emoticons/default/smile.png)