-
Notifications
You must be signed in to change notification settings - Fork 2
Improved Flocking Algorithm
This flocking algorithm has roots in the Reynold's flocking realm, however, has been updated and changed with several new rules.
Movement is performed using a quasi-kinetic method. A Rigidbody is used for the main movement of the agent, however, the velocity is directly updated by the flocking algorithm every FixedTimestep. The flocking algorithm is attached to each agent and will be calculated individually for each agent.
Currently, the rules in the flocking algorithm are:
- Migration
- Separation
- Lane Formation
- Selective Alignment
- Orthogonal Separation
Additionally, it is possible to apply global potential field rules to create intersections, fixed lanes, no entry zones, etc.
The rules are calculated individually and multiplied by their respective gains. A gain of 0 means the rule is not used. A sum of all rules is made and then normalized. This means that the resultant velocity of each rule can be greater than the speed and acts as a "weighting". The final unit vector is then multiplied by the agent's desired speed and set in the RigidBody. The useful part about the "weighting" is that you are able to set the preference to a particular rule, for example, setting Lane Formation to 20, which would largely override the migration rule in many instances. It also means that some rules can be based on the number of agents in its sphere of influence (SOI). If there are lots of agents close to the agent, the separation vector can be very large and again override any other rule.
This is a simple rule which aims to get the agent to its destination. The normalization for this rule is to use the unit vector to the migration point and multiply this by the gain and the desired speed of the agent.
This rule requires Vortices, Sinks, and/or Sources, all of which can be found in the Flocking source folder. These are global infrastructure and are to be added to non-agent GameObjects. Agents search for all intersection-based scripts and use them to apply forces on themselves in each flocking loop.
This rule finds all colliders in the SOI and calculates a repulsive velocity from all. The tuning of the rule can be applied through the gain, but also the standard deviation. The rule is weighted by a normal distribution and hence, the standard deviation sets the distance in which an agent will experience 61% of the separation force. This force is applied to all agents and all other colliders in SOI.
For all agents in the SOI, the dot product between each agent's migration vector is found. If the dot product is positive, meaning the angle between the two migration vectors is less than 90 degrees, then there will be an attractive force. The direction of the resultant velocity is orthogonal to the migration vector of the agent who is calculating the velocity. This is shown in the below figure as the velocity component of lane formation being orthogonal to its migration vector. The force is also in the same plane as the vector between the two calculating agents. This is shown by the velocity component being in the plane of both agents and the i th agent's migration vector.
This rule and orthogonal separation both rely on the fact that agents are closing on each other. This can be calculated by taking the dot product of the relative velocity and the vector between the agents. If the result is positive, the agents are getting closer, if it's negative they are moving apart. The magnitude of the dot product is the speed at which they are closing. Selective alignment is a rule which imposes another agent's velocity onto an agent with a weight that depends on distance and closing dot product. Note: The closing dot product is clamped between 0 and 1 (no force for agents diverging).
This rule is weighted the same as selective alignment with distance and the closing dot product however the direction is found using the cross product between the two agents' velocity. This generates velocities in opposite directions for each agent out of the plane of travel.