I am implementing some simple steering behaviors like: follow the leader, separation, cohesion and flee(from the leader when he walks in our direction - aka spread the group for him).

I am having issues with design of this system. I know how each of this behavior works separately but I simply can't do it with "if ladder" (as I did up to know). Can someone lead me on how to concatenate/normalize/multiply the forces and how to join them in one resultant force ?


What I am exactly talking about is: for now I have something like so :

else if(followTheLeader)


if(velocity > maxSpeed)
    clamp(velocity, maxSpeed)

Which is EXTREMELY bad in my opinion but I have no idea how to improve it.

  • \$\begingroup\$ I know what you're talking about but I have no idea specifically what you are asking. Try being more specific with your question. Are you asking how to do vector math? Your forces are 2D (or possibly 3D) vectors and normalizing/multiplying/etc are simple mathematic functions that are usually built into any decent api/engine. These functions are also easy to google. perhaps you are asking something else? \$\endgroup\$
    – Amplify91
    Jan 10, 2013 at 10:15
  • \$\begingroup\$ @Amplify91 Please check my edit. \$\endgroup\$
    – Patryk
    Jan 10, 2013 at 10:31

2 Answers 2


Add all the relevant forces together. Clamp them at the end to a maximum acceleration rate for that agent. Then you're done.

  • \$\begingroup\$ +1 It really is that simple. You can extend that by weighting your preferred behaviors when adding them together, but the simplest approach is here. \$\endgroup\$
    – House
    Jan 10, 2013 at 16:10

This comes up in AI fairly often. I think what you are looking for is a way to keep your State Machine organized.

Assuming you are using an object-oriented language, the general idea is to represent each of those actions that your agent can do as a state Object, with a defined entry and exit methods that leads into other states. You're most likely going to have a "parent" AI controller that they can ask to transition over into what state they want to be in next.

You can then update your vectors accordingly. Instead of doing those state-based if statements, it might be a better approach to store all of the vectors that your states can update, and then apply those during a regular update cycle.

With respect to Steering Behavior, any number of recent books will have them showcased as an example of using an object-oriented state machine, Mat Buckland's Programming Game AI by Example and Ian Millington's Artificial Intelligence for Games come to mind.


You must log in to answer this question.

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