Question

Are there any examples of flocking where the flock surrounds and engages the target instead of a fish like feeding frenzy where they swoop in then fly away?

I'm working on an overhead shooter and I want the pack of melee enemies to rush the enemy and surround him and keep attacking while more file in behind. I'm trying to avoid them all bunching up on one spot and flocking seems perfect but all the "move to target" examples have them move at the target then move away immediately afterwards.

Ideas?

Était-ce utile?

La solution

As I understand it, generic flocking is generated by a weighted-average of:

  • an alignment vector, which is an average of the alignment vectors of those around you
  • a separation vector, which is a vector pointing away from those around you
  • a cohesion vector, which is a vector pointing towards the local group center

There are different ways to calculating these vectors and different behaviours result when they are given different weights. There are also different ways of combining the combined, weighted-average vector with the current velocity vector.

If I read your question correctly, you would like your flock to circle or flit around a central point. To do this, you've created a fourth vector, which is

  • a target vector, which is a vector pointed towards a particular destination point

Now, you can probably see where this is going... the behaviour of your flock is the result of the weights placed on each vector. If the weight you've placed on the target vector is too small relative the others, your flock will be focusing on flocking instead of attacking.

Therefore, assuming I've outlined flocking correctly, and you've programmed things in a reasonable way, you'll be wanting to try different combinations of weights until you find a behaviour that suits your needs.

Autres conseils

Make a weighted function that takes the distance to prey you want to stay close to an easy to attack from distance, a cost for being close to the x closest allies and a high cost from being too close to the prey.

If you balance things correctly, it should produce a nice circle around the target at optimal distance. Then, when more arrive, the closest ones will close in a little to the target (depending on parameters) and the rest of the flock will form around the initial line of attackers equally. Just add some randomness so it doesn't look like a choreography and it should produce the kind of flocking you are looking for.

PS. You will probably need exponential cost factors instead of linear.

One way to do this is to model "forces" on the attackers and borrow some techniques from particle simulations. If the attackers are drawn to the target, this behavior can be simulated as a "gravity", or an attractive force.

You also want to discourage bunching. This is commonly done in particle simulations (or in active contours/snakes in computer vision) using springs. Simulate a spring connecting two nearby attackers. If the distance between the attackers is greater than the resting length of the spring, then an attractive force is generated. When the attackers are too close to one another (i.e. shorter than the resting length), a repulsive force is generated between the two attackers.

Here is a link for spring simulation (with C code, etc). This powerpoint presentation is a reasonable discussion of physics simulation.

Apply these methods to the mob (occasionally throw in some random noise in the force calculations for realism), and you should see a flocking behavior with "anti-bunching" properties. You can weight the respective attractive and repulsive forces to regulate how strongly the attackers chase the target and how much they avoid one another.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top