24
\$\begingroup\$

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?

\$\endgroup\$
  • 1
    \$\begingroup\$ Limit yourself to one enemy. :P \$\endgroup\$ – Kylotan Aug 11 '10 at 20:20
18
\$\begingroup\$

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).

\$\endgroup\$
  • 6
    \$\begingroup\$ It's called also called boids. Read about the simple rules surrounding boids here: en.wikipedia.org/wiki/Boids Should be trivial to implement these restrictions. \$\endgroup\$ – Nailer Aug 10 '10 at 12:34
12
\$\begingroup\$

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)
\$\endgroup\$
  • \$\begingroup\$ This is nearly a verbatim copy of JasonD's answer. I swear I didn't look at it before posting. \$\endgroup\$ – egarcia Aug 10 '10 at 16:19
4
\$\begingroup\$

What you're asking about in general is dynamic avoidance between actors. You could take a look at velocity obstacles: http://gamma.cs.unc.edu/RVO/

\$\endgroup\$
3
\$\begingroup\$

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.

\$\endgroup\$
  • \$\begingroup\$ 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. =) \$\endgroup\$ – leander Aug 10 '10 at 23:30
  • \$\begingroup\$ This is super awesome but I bit more complexity than I needed. Will read the whitepaper anyway but managed to implement simpler solution. \$\endgroup\$ – Iain Aug 11 '10 at 16:14
2
\$\begingroup\$

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.

\$\endgroup\$
2
\$\begingroup\$

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.

\$\endgroup\$
  • \$\begingroup\$ This is essentially the same answer as the other 2, but +1 for the link - That website is a really good reference. \$\endgroup\$ – AShelly Aug 10 '10 at 17:50
1
\$\begingroup\$

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: http://deleter.phatcode.net/index.php?page=projects&p=4

\$\endgroup\$
1
\$\begingroup\$

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

\$\endgroup\$
0
\$\begingroup\$

I was facing the same probelm when I using NevMeshAgent as enemy, enemeis are penetrating in eachother so i increase the

NavMeshAgent's radius

Thanks to gameDevAnswer

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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