Game Development Stack Exchange is a question and answer site for professional and independent game developers. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

So: top-down game, my enemies chase the player, when they get within a certain distance they stand still and fire. If they're all coming from the same direction they all end up standing in the same spot (i.e. standing "within" each other), as I'm not currently doing collision detection between enemies - they are free to pass over each other.

What's a simple way around this? Either some form of collision detection or some ai?

share|improve this question
Limit yourself to one enemy. :P – Kylotan Aug 11 '10 at 20:20
up vote 19 down vote accepted

You don't need collision detection, but you will need to give your enemies a bit of intelligence, and have them avoid running in to each other. Collision detection without that will just make your enemies look stupid anyway - people avoid each other, they don't generally collide.

Look up "flocking" for some simple behaviours.

The basic idea is that things should head towards the player, but away from a close obstacle (each other, walls, that kind of thing).

share|improve this answer
It's called also called boids. Read about the simple rules surrounding boids here: Should be trivial to implement these restrictions. – Nailer Aug 10 '10 at 12:34
Thanks for that wikipedia link, its good – DFectuoso Aug 11 '10 at 0:56

You could inspire yourself on flocking behaviours, such as Boids.

These tend to consist on 3-4 very simple rules that, applied correctly, result on very satisfying and "complex-looking" behaviours.

In your case, you could try something like this:

  • Separation (minimun radii between enemies)
  • Cohesion (so they travel in groups)
  • Alignment (so they tend to move on the same direction)
  • Targetted (they should tend to move towards the player)
share|improve this answer
This is nearly a verbatim copy of JasonD's answer. I swear I didn't look at it before posting. – egarcia Aug 10 '10 at 16:19

What you're asking about in general is dynamic avoidance between actors. You could take a look at velocity obstacles:

share|improve this answer

You may want to check out collaborative diffusion (great animations here) or some other kind of antiobject pattern for movement. While the diffusion algorithm in and of itself may not stop enemies from crossing over, you can add additional constraints to make it unlikely or impossible.

share|improve this answer
Full disclosure: pretty sure I found the collaborative diffusion link here somewhere on GD, but I can't for the life of me find the post that refers to it now. It was quite a find. =) – leander Aug 10 '10 at 23:30
This is super awesome but I bit more complexity than I needed. Will read the whitepaper anyway but managed to implement simpler solution. – Iain Aug 11 '10 at 16:14

What if you disallow moving through other units? It seems to me you will need it sooner or later. Whenever the current path is blocked by a unit, adjust the path to move around that.

share|improve this answer

There's some reference for steering and flocking behaviours at Craig Reynold's website. They're relatively simple to implement but can produce some lovely emergent behaviour.

share|improve this answer
This is essentially the same answer as the other 2, but +1 for the link - That website is a really good reference. – AShelly Aug 10 '10 at 17:50

All the other answers produce a better result, but the cheapest(?) way to avoid complete grouping is to just add a random vector that is perhaps 25% of the original calculated movement vector so that objects bounce around a bit in a general blob group. It won't keep things completely off of each other, but depending on what you are trying to achieve, I have found this can be adequate. Objects can still group with this method if you run circles around them, but over time they tend to diffuse.

Here is a small game-let I made several years ago that demonstrates this method:

share|improve this answer

People always bring up boids because that gives very good results, but implementing boids may be overkill for your specific purpose. In the past I've gotten very good results by simply randomizing the target position within a small circle.

For example, for a demo I built in Unity I used its command Random.insideUnitSphere to do flocking puffballs

share|improve this answer
+1 I agree, another example of this strategy (with probably a little better guarantee of not overlapping) can be found here: “Unclutter” units in RTS game – Byte56 Nov 11 '13 at 23:20

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.