# Pathfollowing Implementation help

This topic is 3544 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

Hi I am currently create a PacMan clone for the fun off it, and I've encountered a problem on how to make the Ghosts follow the path returned by the A* Path Finder. Now the Path returned consists of a whole lot of - std::pair< Node, Action> where Node is the next Node the Ghost should go to and Action is the Action it can use to get there. I'm using Box2D as a physics engine (I know it would be a lot easier without it, just humour me ..) The First Method that I tried was that there was a class GhostAction which was basically this - (I love version controll :-) )
class GhostAction
{
public :
float fSpeed;

GhostAction(float dist=1.0)
{
//As speed = dist/time
fSpeed = 5.00;
time = 1/fSpeed;
vel.SetZero();
}
virtual ~GhostAction() {}

float time;
b2Vec2 vel;

void left() { vel.Set( -fSpeed, 0.0f ); }
void right(){ vel.Set( +fSpeed, 0.0f ); }
void up(){vel.Set( 0.0f, -fSpeed); }
void down(){ vel.Set( 0.0f, +fSpeed); }
};


Then when the path was returned I would simple transverse the list and apply the required velocity for the required amount of time. But that did not work as planned as 1. The GhostNode class was implemented with int and not floats (The world is continuous ) so certain rounding off occured and the Ghost often got stuck. 2. Same as above ^^ The second approach was that I used a Dir enum as the GhostAction and the Ghost would move in that direction until it reached the next point or went ahead. This approach doesn't work for high speeds though. Does any one have any idea as to how I could implement some way to transverse the Path ?

##### Share on other sites
First of all, I recommend you take the time to use floats in your ghosts(if you have not already).Box2D works on floats so you will find it a lot easier to use the data it gives you.

When I did a pac-man clone (no physics), I took the vector from start to destination and scaled it by the speed multiplied by delta time (milliseconds).

Something like:
Vec Pos;Vec Dest;Vec Speed;Vec move = Dest - Pos;move *= speed * delta_timepos += move;if( abs(dest.x,y - pos.x,y) <= float_range)choose_next_node();

This would make the ghost move from point A to B as long as delta time was below 1. The next node would be targeted when the sprite was within distance from the target location. This would easily handle any small skips the sprite took, if it missed the exact location. Otherwise if delta time was >1 it would skip the target and go hit a wall. It would of course comeback, since a vector is being calculated before every move.

For me the whole thing was just an exercise in A* with only ghosts running around to random targets so I didn't really care about how accurate the locations of the ghosts were. There were no walls, just an 2D array of ints which represented open/close area.

You could use a similar method to push your ghosts by adding an impulse/force to the body and stopping it when it reaches within certain range to a node target. The you could set the position to the exact point on the node, before moving it to the next.

You can use tweening move an object from one location to another.

http://www.actionscript.org/resources/articles/94/1/Actionscript-Powered-Movement/Page1.html

Adding a impulse or force to an object is somewhat like shooting a bullet and hope it reaches it's destination. But it you are happy to what it mostly follow the path then it should work out fine. Also looking at the programming faq for box2d will give you a few pointers.

http://www.box2d.org/wiki/index.php?title=Programming_FAQ

[Edited by - FireNet on January 6, 2009 12:28:19 PM]

1. 1
2. 2
Rutin
21
3. 3
4. 4
frob
17
5. 5

• 9
• 33
• 13
• 13
• 10
• ### Forum Statistics

• Total Topics
632582
• Total Posts
3007207

×