• Advertisement
Sign in to follow this  

Pathfollowing Implementation help

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

If you intended to correct an error in the post then please contact us.

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;
    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 this post

Link to post
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_time

pos += 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.


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.


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

Share this post

Link to post
Share on other sites
Sign in to follow this  

  • Advertisement