Follow The Leader [Solved]

I discovered a very versatile method of doing it that is based heavily on the boids example. I just need to clean up my code and finish the rest of the script, then I will post it. Hopefully this will only take a couple of days (got a lot of work ahead of me… I only discovered the method and I have the basic framework for it).

Thanks to everyone who helped me out and definitely a lot of thanks to the person who wrote the boids code.


I’ll be bothering you guys again. :slight_smile:

My new problem is having something follow the player. I been looking around to see how other people do this, but I kind of just want something simple that I can build on myself. I thought I could use intervals but then I read that those don’t go well with collisions.

I know about a few that show different ways of doing it but I wanted to do something myself that was just a bit more bare-bones.

If you think something relatively simple has been done already, feel free to just redirect me there.

So… any suggestions?[/i]

The most obvious way is to simply parent the thing to the player. That would always keep a fixed distance and orientation, but that also means collisions wouldn’t do much.

If it’s something simple enough that you would consider using intervals, it would be easy to write a task to do something similar.


If (it's not close enough to player)
  face the player
  increment current position towards player based on dt

Yeah, I am trying something along those lines. In the function that is called during the movement task (this task handles the players movements) I call this:

self.shadowNode.posInterval((self.movementSpeed), self.c_node.getPos())

At this point all I want for the follower to do is constantly follow the leader. The above code doesn’t work at all. To be honest, I only considered intervals because it’s the only thing I know that moves an actor over time. I’ve never used them except for in the tutorial because the need for them has never arisen. I don’t even need the layout like you just posted because I already have that all set up. What’s really troubling me is how to get the sucker to literally start moving gradually. All I’m able to do is get him to teleport (so I know my code is working so far). :smiley:

Thank you for your help.


That’s all you can do. The trick is to teleport in small enough increments so that it all runs together.

Directly changing a position is pretty easy:

See the Asteroids sample for some examples of using tasks to move things.

For a more relavent example, this is an excerpt of code I wrote for Tiptoe’s Point & Click Player Movement Over Uneven Terrain script.

    def update(self,dt):
        """Call this method in your main loop task.""" update position based on velocity
   )#let Floor worry about Z
        #recalc velocity to point to destination
        self.vel =
        if self.vel.lengthSquared()<.1:#Then consider yourself arrived.
            self.request('Stand')#change FSM state
            self.vel.normalize()#set magnitude to 1
            self.vel*=self.speed#the magnitude of velocity is speed. 
    def setDestination(self,point):
        """Go to the point."""
        self.point = point
        pr =, preserve pitch and roll affects all three (HPR)
        #keep heading but revert pitch and roll.,*pr)
        #subtracting points yields a vector pointing from the 1st to 2nd
        self.vel = from position
        self.vel.normalize()#set magnitude to 1
        self.vel*=self.speed#the magnitude of velocity is speed.
        self.request('Run')#change FSM state 

The [color=darkred]update function gets called every frame by the main loop task. The [color=darkred]dt is the time elapsed since the last frame. You can ignore the FSM state changes for now. The destination point in your case would be the position of the player, instead of where the user clicked on the terrain.

Thanks for clearing that up. I’m actually very used to that idea, but I saw it done with acceleration, friction, and even applying a force in a direction, so I thought that physics was the way to go. I’ll leave that for later. Either way, it makes sense now. I’ll see if I can get this to work properly.

(I was thinking something besides intervals, since it’s just moving in increments, which is a concept I understand. I suppose it doesn’t hurt to just use it since it can work.)

Edit: Lol, I got it working but watching it follow the character is hilarious. I’m almost hesitant to fix it now.

:wink: You could always save an old version for later…

For some advanced behaviour like follow the leader, advance, flee , etc… you may try to look on Boids and their several implementations…

Thank you for the suggestion, I actually downloaded Boids last night and was planning to take a look today. (I downloaded it before but it magically disappeared off my computer. When I originally looked at it it didn’t make enough sense for me to use. I only understood the python code, but was confused as to how Panda worked at the time. Now that I have a better understand thanks to the people at the forums, I can actually understand it and will implement the ideas.)

And Cyan, it’s not really something I would save… :laughing: It’s funny in a different way (embarrassing is a better word I guess).