Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

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.

share|improve this question
add comment

1 Answer

up vote 5 down vote accepted

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;
}
share|improve this answer
    
Thanks, I will try that. –  Casla Jan 30 '12 at 21:45
    
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 –  Casla Jan 31 '12 at 1:28
    
@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 :) –  David Gouveia Jan 31 '12 at 7:59
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.