31
More Complex Movement AI Flocking and more

More Complex Movement AI

  • Upload
    dolan

  • View
    33

  • Download
    0

Embed Size (px)

DESCRIPTION

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? - PowerPoint PPT Presentation

Citation preview

Page 1: More Complex Movement AI

More Complex Movement AI

Flocking and more

Page 2: More Complex Movement AI

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

Page 3: More Complex Movement AI

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

Movement in Groups

Page 4: More Complex Movement AI

Flocking

Page 5: More Complex Movement AI

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

Boids

Page 6: More Complex Movement AI

Have each unit steer towards the average position of its neighbors

Units are attracted to one another as long as they are within range

Cohesion

Page 7: More Complex Movement AI

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

Alignment

Page 8: More Complex Movement AI

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

Separation

Page 9: More Complex Movement AI

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

Neighborhood

Page 10: More Complex Movement AI

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

Neighborhood

Page 11: More Complex Movement AI

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?

Neighborhood

Page 12: More Complex Movement AI

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

Page 13: More Complex Movement AI

Visibility arc defined by 2 parameters – arc radius r and angle θ

How do these parameters affect flocking motion?

Unit Visibility

Page 14: More Complex Movement AI

Large radius? Small radius? Wide FOV? Narrow FOV?

Unit Visibility

Page 15: More Complex Movement AI

Narrow FOV: Squadron of jets, Sneaking up behavior

Wide FOV: Group of birds, Military army

FOV determines formation

Page 16: More Complex Movement AI

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

Steering for Flocking

Page 17: More Complex Movement AI

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

Implementation

Page 18: More Complex Movement AI

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 -1bool InView; bool DoFlock = WideView||LimitedView||NarrowView; int RadiusFactor; . . .

}

Sample Variable Set

Page 19: More Complex Movement AI

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

Cohesion - Implementation

Page 20: More Complex Movement AI

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

Alignment - Implementation

Page 21: More Complex Movement AI

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

Separation - Implementation

Page 22: More Complex Movement AI

http://www.lalena.com/ai/flock/

Try this niec flocking demo

Page 23: More Complex Movement AI

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

Page 24: More Complex Movement AI

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

Obstacle Avoidance

Page 25: More Complex Movement AI

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

Page 26: More Complex Movement AI

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?

Obstacle Avoidance - Remarks

Page 27: More Complex Movement AI

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?

Follow the Leader

Page 28: More Complex Movement AI

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

Let them sort themselves!

Page 29: More Complex Movement AI

Can you figure out an algorithm to do this?

Leader Check

Page 30: More Complex Movement AI

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?

Leader Check

Page 31: More Complex Movement AI

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

Follow the Leader