310 likes | 413 Views
More Complex Movement AI. Flocking and more. Movement in Groups. 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?
E N D
More Complex Movement AI Flocking and more
Movement in Groups • 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?
Movement in Groups • 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
Boids • 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
Cohesion • Have each unit steer towards the average position of its neighbors • Units are attracted to one another as long as they are within range
Alignment • 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
Separation • 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
Neighborhood • Flocking neighborhood creates a range that units can detect for other same-group units, other-group units
Neighborhood • Some implementations use two neighborhoods – one for detection of units, one for separation to avoid other units
Neighborhood • 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?
Unit Visibility • Each unit is aware of its local surroundings • Each unit does not necessarily know what the entire group is doing at any given time
Unit Visibility • Visibility arc defined by 2 parameters – arc radius r and angle θ • How do these parameters affect flocking motion?
Unit Visibility • Large radius? Small radius? • Wide FOV? Narrow FOV?
FOV determines formation • Narrow FOV: Squadron of jets, Sneaking up behavior • Wide FOV: Group of birds, Military army
Steering for Flocking • 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
Implementation • 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
Sample Variable Set void DoUnitAI(inti) { 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 boolInView; boolDoFlock = WideView||LimitedView||NarrowView; intRadiusFactor; . . . }
Cohesion - Implementation • 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
Alignment - Implementation • 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
Separation - Implementation • 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
Try this niec flocking demo • http://www.lalena.com/ai/flock/
Obstacle Avoidance • 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…
Obstacle Avoidance • 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: • Magnitude (p) < Magnitude (v) • Magnitude(b) < Radius (r) • If both tests pass, corrective steering required, otherwise unit can continue on its current heading
Obstacle Avoidance • 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) )
Obstacle Avoidance - Remarks • 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?
Follow the Leader • 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?
Let them sort themselves! • 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
Leader Check • Can you figure out an algorithm to do this?
Leader Check • 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?
Follow the Leader • 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