1
\$\begingroup\$

Is there a common way to implement a character following path that is issued and replaced in real time?

For example: the character follows a path issued, and while it's on that path, if a new path is issued, the character abandons the existing one and follows the new path from where it is currently located.


Here's a more detailed explanation:

I am trying to make a real-time action game, like Diablo, but in 2D and tile-based. The player can issue new destinations on the map and the in-game character should walk towards that destination in real time.

I got the path finding part all worked out, but I am having issues with getting the character to smoothly abandon an existing path, and follow a new path (when a new destination is issued).

The character sometimes flickers when a new path is issued while it's following an existing path. If the character sprite is not at the centre of a tile, it will "teleport" to the centre of the tile it's on and follow the path from there on smoothly.

I tried to get around this problem by first checking if the player is on at the centre of a tile, and if not, I amend a node in the front of the new path to get the player to move to that tile. But this still doesn't get me the desired effect, because if the same destination is issued over and over, the character will just keep walking to the same tile instead of walking towards that destination.

\$\endgroup\$

1 Answer 1

5
\$\begingroup\$

I don't think there's anything special about switching from one path to another. You just abort the movement that was taking place, generate a new path from the current position to the destination, and start following the new path from your current position.

Your problem seems to be that you're teleporting your character to the center point of the first tile in the path (i.e. the tile your character is already in), but there's no need to do so.

Although this problem depends mostly on how you're processing the movement and how you're representing the path (either as world coordinates or as tiles), I think you can probably solve it simply by dropping the first node of the path and start moving directly towards the second node instead. See the picture below for clarification:

enter image description here

This basically means, make your current position replace the first node in the path, instead of appending it like you were doing. Example:

List<Vector2D> path = FindPath(player.Position, destination);
if(path.Count > 0)
{
    path[0] = player.Position;
}
\$\endgroup\$
2
  • \$\begingroup\$ Cool, this worked perfectly, and it has made my code so much simpler :) I just replaced the first step in every path instead of appending it. Before I was doing all sorts of checks to see if the sprite is in the centre of the tile etc. Thanks \$\endgroup\$
    – Casla
    Jan 31, 2012 at 1:28
  • \$\begingroup\$ @Casla Glad it worked. You should use the accept answer button on the left side of my post to mark this question as answered then :) \$\endgroup\$ Jan 31, 2012 at 7:59

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .