I have a small AI framework for a shooting based game. Although this is rarely needed, as when agents are close to each other they are usually fighting, I would none the less like some way of implementing avoidance behaviour.

For example, if in the future I wanted to take away their weapons and have many of them wonder around in a crowd, how would I make them not hit / pass through each other, but instead avoid each other? while still following their calculated paths that is.

two ideas I had would be to add steering behaviour and allow that to deviate from their path, or to use a dynamic pathfinding technique. Are these valid solutions? If yes how, in theory, would I implement them? Are there better ways? What is the more respected practice?


1 Answer 1


Make use of flocking.

Flocking combines three steering behaviours, separation, cohesion and alignment.

The idea is that through the combination of these behaviours, more complex, fairly realistic behaviour is created (this is called emergent behaviour).

Each agent has a neighbourhood data structure. The neighbourhood can be a simple bounding sphere test to check for the nearest neighbouring agents. Using this data, the 3 behaviours perform certain actions:

Separation (Ensure that agents don't get too close together)
Steer the agent away from the neighbouring agents

This behaviour iterates through the neighbouring agents, normalizes the vector to the neighbour and divides by the distance to the neighbour. This is then added to the steering force you apply to the agent.

Alignment (Used to make sure agents are facing the right direction)
Steer the agent towards the average heading of neighbouring agents

This behaviour iterates through the neighbouring agents, calculating the average heading of the neighbours and adds the vector to this average heading to the steering force of the agent.

Cohesion (Used to group agents together)
Steer the agent towards the centre of mass of neighbouring agents

This behaviour is similar to alignment, but instead of the calculating the average heading, we calculate the average position of the neighbours. Then we add the vector to the centre of mass to the agent's steering force.

Or... just make use of a library like OpenSteer.

I'd highly recommend you get Mat Buckland's Programming Game AI by Example. It will answer a lot of questions you have about steering behaviours and provides easy to understand source code.


If you look at OpenSteer and Buckland's book, you will notice that steering behaviours are atomic behaviours. Each behaviour is given some sort of weighting/priority value and the effects of all active behaviours are summed together.

So, to work with pathfinding, a path-follow behaviour is also a type of steering behaviour. This means that path-follow and flocking can be weighted, thus combining the effects of both.

Honestly, though, break it down into very small chunks, before you get caught up in integrating components with other components.

  • \$\begingroup\$ I'm still a little unsure of how this would fit into a third person shooter. How does the flocking and the A* pathfinding work together? \$\endgroup\$ Commented Nov 21, 2011 at 19:30
  • \$\begingroup\$ Edited to add some clarification. OpenSteer is open source btw and it's fairly readable C++ code. If your design is something you're worrying about, then you can take a look at their implementation to see if you can glean some ideas from that. \$\endgroup\$
    – Ray Dey
    Commented Nov 21, 2011 at 19:58

You must log in to answer this question.

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