Flocking is an emergent behavior and is the resultant of the following forces:
cohesion -- finds average position of neighbors and tries to move to that position
separation -- object keeps a certain distance between itself and its neighbor
alignment -- finds average direction in which all neighbors are moving and tries to move in that direction
Each NPC has a "visibility cone" and this is used to compute it's neighbors. The neighbors contribute towards the forces mentioned above.
Tuners:
1. The angle and length of each NPC's "visibility cone".
2. Weight of cohesion, separation, and alignment (how much each sub-behavior of flock affects the overall flocking behavior).
Note: Flocking behavior is NOT a standalone behavior. It needs to be combined with other steering behaviors such as seek, pursue, flee, evade etc. to function.
Using PandAI's flocking system:
// To create the flock flockObject = Flock(unsigned int flock_id, double vcone_angle, double vcone_radius, unsigned int cohesion_wt, unsigned int separation_wt, unsigned int alignment_wt)
"flock_id" is a value identifying the flock.
"vcone_angle" is the visibility angle of the character (represented by a cone around it)
"vcone_radius" is the length of the visibility cone.
"cohesion_wt", "separation_wt" and "alignment_wt" is the amount of separation force that contributes to the overall flocking behavior.
Some standard values to start you off with:
Type vcone_angle vcone_radius separation_wt cohesion_wt alignment_wt
Normal Pack 270 10 2 4 1
Loose Pack 180 10 2 4 5
Tight Pack 45 5 2 4 5
You could try experimenting with your own values to customize your flock.
To add your AI Character to the above created flock
flockObject.addAiChar(aiChar) // aiChar is an AICharacter object.
After all the AI Characters are added to the flock, add the flock to the world.
aiWorld.addFlock(flockObject) // aiWorld is an AIWorld object.
Specify the flock behavior priority. As mentioned earlier, flock behavior works with other steering behaviors.
aiBehaviors.flock(float priority) // aiBehaviors is an AIBehaviors object. <code>
<b>Other useful flock functions:</b> <code python> aiWorld.flockOff(unsigned int flock_id); // Turns the flock behavior off.
aiWorld.flockOn(unsigned int flock_id); // Turns the flock behavior on.
aiWorld.removeFlock(unsigned int flock_id); // Removes the flock behavior. Note: This does NOT remove the AI characters of the flock.
aiWorld.getFlock(unsigned int flock_id); // Returns a handle to the flock object.
The full working code in Panda3D :
#for directx window and functions import direct.directbase.DirectStart #for most bus3d stuff from pandac.PandaModulesimport* #for directx object support from direct.showbase.DirectObjectimport DirectObject #for tasks from direct.taskimport Task #for Actors from direct.actor.Actorimport Actor #for Pandai from panda3d.aiimport* #for Onscreen GUI from direct.gui.OnscreenTextimport OnscreenText
# Globals speed = 0.75
# Function to put instructions on the screen. font = loader.loadFont("cmss12") def addInstructions(pos, msg): return OnscreenText(text=msg, style=1, fg=(1,1,1,1), font = font, pos=(-1.3, pos), align=TextNode.ALeft, scale = .05)