Presentation is loading. Please wait.

Presentation is loading. Please wait.

Flocking and more.  NPC groups can move in cohesive groups not just independently ◦ Meadow of sheep grazing? ◦ Hunting flock of birds? ◦ Ants? Bees?

Similar presentations

Presentation on theme: "Flocking and more.  NPC groups can move in cohesive groups not just independently ◦ Meadow of sheep grazing? ◦ Hunting flock of birds? ◦ Ants? Bees?"— Presentation transcript:

1 Flocking and more

2  NPC groups can move in cohesive groups not just independently ◦ Meadow of sheep grazing? ◦ Hunting flock of birds? ◦ Ants? Bees? Creatures?  Other types of computer controlled NPCs ◦ Humans, Orcs, Catapults? ◦ Squadrons of aircraft? ◦ Friendly soldier squads? ◦ Simulate crowds of people loitering?

3  Coordinated group movement, the idea: ◦ To have the NPCs move with the illusion of having purpose and coordination  One of the earliest, most successful group behavior  Flocking ◦ “Flocks, Herds, and Schools: A Distributed Behavioral Model”, Craig Reynolds, SIGGRAPH 1987 ◦ Originally intended for birds, fish and other creatures, but it can be modified for other types of NPCs


5  Term used by Craig Reynolds to refer to this simulated flocks  Leaderless flock – able to stick in a group  3 simple rules ◦ Cohesion ◦ Alignment ◦ Separation  Neighborhood: Defines the area where these rules will come to effect

6  Have each unit steer towards the average position of its neighbors  Units are attracted to one another as long as they are within range

7  Have each unit steer so as to align itself to the average heading of its neighbors.  Match direction of units around it that it can detect

8  Have each unit steer to avoid hitting its neighbors.  Units are repelled by non-member units or obstacles. Repel effect is inversely prop. to distance from unit

9  Flocking neighborhood creates a range that units can detect for other same-group units, other-group units

10  Some implementations use two neighborhoods – one for detection of units, one for separation to avoid other units

11  Typically, a visibility arc or field-of-view (FOV) is used to define the neighborhood  Is this practical?  To what extend is each unit aware of its neighbors?

12  Each unit is aware of its local surroundings  Each unit does not necessarily know what the entire group is doing at any given time

13  Visibility arc defined by 2 parameters – arc radius r and angle θ  How do these parameters affect flocking motion?

14  Large radius? Small radius?  Wide FOV? Narrow FOV?

15  Narrow FOV: Squadron of jets, Sneaking up behavior  Wide FOV: Group of birds, Military army

16  Steering forces to be applied on the units  Treat each unit as a rigid body that is able to turn and apply net steering force accumulated from each flocking rule  2 important techniques when implementing flocking ◦ Tuning is required so that no single rule dominates ◦ Modulation of steering forces so that contribution is not constant for all units

17  In each game loop ◦ Cycle thru all units in the flock to acquire data (direction, speed, etc.) from unit’s neighbors ◦ For each unit, update with net steering force from the three rules  Each unit must update its view of the world each game loop (cycle thru all units in the flock)  Refer to textbook for more details on the implementation code snippet

18 void DoUnitAI(int i) { int j; int N; // Number of neighbors Vector Pave; // Average position vector Vector Vave; // Average velocity vector Vector Fs; // Net steering force Vector Pfs; // Point of application of Fs Vector d, u, v, w; double m; // multiplier, +1 or -1 bool InView; bool DoFlock = WideView||LimitedView||NarrowView; int RadiusFactor;... }

19  Calculate average position – vector sum of their respective positions divided by total number of neighbors  Determine direction to turn and angle to steer towards  Steering force effected = Direction multiplier * Max steering force * angle of steering / scale factor

20  Normalize each unit’s velocity vector to get heading unit vectors  Calculate average heading of all units – sum of heading unit vectors divided by total number of neighbors  Effected steering force is calculated same way as cohesion

21  Separation is enforced by steering away from any neighbor that is within view AND within prescribed minimum separation distance  Because this steering force is corrective, direction multiplier goes the opposite way  Effected steering force = Direction multiplier * Max steering force * (Unit length * separation factor) / separation distance

22 

23  Flocking would be much more realistic if units also avoid running into objects in the game world  To detect whether an obstacle is in the unit’s path ahead, imagine that each unit has “feelers” like those on insects!  Well, if one feeler is not enough, maybe you might need a few feelers?  Let’s see how a single “feeler” works…

24  v : “feeler”  Calculate vector a  Project a onto v by dot product to obtain p  Subtract p from a to get vector b  Test conditions: 1.Magnitude (p) < Magnitude (v) 2.Magnitude(b) < Radius (r)  If both tests pass, corrective steering required, otherwise unit can continue on its current heading

25  Corrective force can be calculated as inversely prop. to distance from unit to the center of obstacle or Magnitude (a)  Effected steering force = Direction multiplier * Max steering force * (Collision Visibility Factor * Unit length for Magnitude(v) / Magnitude(a) )

26  This obstacle avoidance algo will not necessarily guarantee zero collisions between units and obstacles. What are some likely problems?  What we have seen so far only applies to circular obstacles. What about block (rectangular) obstacles or other free forms shapes?

27  So far, flocking behaviors are leaderless  By combining classic flocking with leader- based AI, many new possibilities are available!  Flocks may have greater purpose if follow a leader  Question: How to designate leader? Should we “appoint” a unit as leader? Or should we let them sort out themselves who should be a leader?

28  Let’s focus on this particular method  Advantage: Any unit can become a leader at any given time, flock will not be leaderless if leader gets destroyed or separated from flock  Once a leader is established, we can implement any number of rules to have the leader do something meaningful ◦ Execute pattern movement or patrolling ◦ Chase or evade or intercept something

29  Can you figure out an algorithm to do this?

30  A possible solution: ◦ Determine the number of units directly in front of or within view of current unit being processed (velocity directions are available for use) ◦ If no other units are directly in front of the unit, it becomes the leader. The rest follows flocking rules  Any more ideas?

31  Follow the Leader AI adds an interesting dimension into flocking and group coordinated behavior  More than one leader (of different purposes) can also be implemented  You can also implement flocking behavior for player-friendly/assisting NPCs where the “leader” is simply the player

Download ppt "Flocking and more.  NPC groups can move in cohesive groups not just independently ◦ Meadow of sheep grazing? ◦ Hunting flock of birds? ◦ Ants? Bees?"

Similar presentations

Ads by Google