23
1 CO3301 - Games Development 2 Week 22 Flocking Gareth Bellaby

1 CO3301 - Games Development 2 Week 22 Flocking Gareth Bellaby

Embed Size (px)

Citation preview

Page 1: 1 CO3301 - Games Development 2 Week 22 Flocking Gareth Bellaby

1

CO3301 - Games Development 2Week 22

Flocking

CO3301 - Games Development 2Week 22

Flocking

Gareth BellabyGareth Bellaby

Page 2: 1 CO3301 - Games Development 2 Week 22 Flocking Gareth Bellaby

2

IntroductionIntroduction

Flocking is a way of generating realistic movement for animals in a group: a flock. For example a flock of birds.

Can be applied to non-human animals such as birds and fish.

Can be extended and applied to aircraft, units and squads.

Flocking was developed by Craig Reynolds.

Page 3: 1 CO3301 - Games Development 2 Week 22 Flocking Gareth Bellaby

3

Application to GamesApplication to Games

Flocking is of interest to games for two main reasons:

• Realistic flocks of birds or other animals which can be placed within a game world and just left to their own devices, e.g. a flock of birds that fly around, perch and react to the characters presence but without the need for scripting.

• Application to group movement.

Page 4: 1 CO3301 - Games Development 2 Week 22 Flocking Gareth Bellaby

4

CharacteristicsCharacteristics

Flocking is an example of emergent behaviour.

The individual members of the flock do not know anything about the behaviour of the flock.

The flock will turn without direction. In the original formulation of flocking there is no leader. The movement propagates though the flock.

An example of complexity out of simplicity - uses three simple rules.

Page 5: 1 CO3301 - Games Development 2 Week 22 Flocking Gareth Bellaby

5

Original FormulationOriginal Formulation

Three rules:

• Separation: steer to avoid crowding local members of the flock.

• Alignment: steer towards the average heading of local members of the flock.

• Cohesion: steer to move toward the average position of members of the flock.

Page 6: 1 CO3301 - Games Development 2 Week 22 Flocking Gareth Bellaby

6

DiscussionDiscussionOnly takes local members of the flock into account.

The system is unpredictable (over moderate time periods.

The algorithm uses feedback mechanisms. Feedback is when part (or all) of the output of a system is used as input into the system, e.g. a thermostat. Positive feedback leads to an uncontrolled system. Negative feedback leads to a controlled system.

The system is chaotic. However, the use of feedback leads to group behaviour and, in most circumstances, a stable system.

Page 7: 1 CO3301 - Games Development 2 Week 22 Flocking Gareth Bellaby

7

Efficiency considerationsEfficiency considerations

An individual knows the average location and heading of other members of the flock, plus the distance between itself and the other members.

The algorithm can be computationally expensive since each member needs to make a comparison against every other members.

Can reduce this cost if the members are kept sorted by location.

Page 8: 1 CO3301 - Games Development 2 Week 22 Flocking Gareth Bellaby

8

DistancesDistances

In Craig's initial experiments attraction and repulsion were weighted linearly by distance.

This resulted in an unrealistic "bouncy" flocking action. The net effect is like a spring.

He revised the model to use an inverse square of the distance. This resulted in more realistic flocking behaviour.

The inverse square law dampens the effect.

Page 9: 1 CO3301 - Games Development 2 Week 22 Flocking Gareth Bellaby

9

Inverse Square LawInverse Square Law

The inverse square law is the law that an effect is inversely proportion to the square of the distance. For example, let the repulsive effect of the flock members at a distance of 1 be r :

distance calculation effect

1 1/12 = 1 r/1

2 1/22 = 1/4 r/4

3 1/32 = 1/9 r/9

Page 10: 1 CO3301 - Games Development 2 Week 22 Flocking Gareth Bellaby

10

CollisionsCollisions

Collisions will occur within the flock.

There will come points where the net effect of the repulsors and attractors will force a collision between flock members.

• It is necessary to maintain a collision detection system in place.

• Could abrupt turns in emergency situations.

• It can be argued that this is realistic, e.g. birds do occasionally collide.

Page 11: 1 CO3301 - Games Development 2 Week 22 Flocking Gareth Bellaby

11

Forward perceptionForward perception

Can introduce forward perception.

The individual can only perceive those members of the flock who are in their field of view.

A wide field of view leads to a broad flock.

A narrow field of view leads to a narrow flock (ultimately follow-my-leader)

Page 12: 1 CO3301 - Games Development 2 Week 22 Flocking Gareth Bellaby

12

BehaviourBehaviour

As you change the values used with separation, alignment and cohesion the behaviour of the flock will change.

Could give each member of the flock slightly different values, i.e. a flock made out of non-uniform members.

Page 13: 1 CO3301 - Games Development 2 Week 22 Flocking Gareth Bellaby

13

Graceful obstacle avoidanceGraceful obstacle avoidance

The flocking behaviour as described ignores obstacles.

A graceful form of obstacle avoidance can be created by providing the individual with sensors. This has similarities with the sensors used in robotics.

Page 14: 1 CO3301 - Games Development 2 Week 22 Flocking Gareth Bellaby

14

Graceful obstacle avoidanceGraceful obstacle avoidance

Use a vector drawn from the individual to the outside of the radius.

May be able to get away with just a single sensor sticking out of the front of the individual.

Page 15: 1 CO3301 - Games Development 2 Week 22 Flocking Gareth Bellaby

15

LeadersLeaders

The original formulation of flocking did not include targets or leaders.

The leader could be permamently designated.

The designation of leader could be temporary. For example, let's introduce the idea of the flock aiming towards the most recent mouse click on screen. The leader could be the member of the flock nearest the target and where the target is in front of the individual.

The rest of the flock could use a follow-the-leader rule.

Page 16: 1 CO3301 - Games Development 2 Week 22 Flocking Gareth Bellaby

16

Attractors and RepulsorsAttractors and Repulsors

These 3 rules describe 3 attractors/repulsors.

Attractors attract the individual.

Repulsors repulse the individual.

The repulsors and attractors are summed together to generate the action.

Page 17: 1 CO3301 - Games Development 2 Week 22 Flocking Gareth Bellaby

17

Attractors and RepulsorsAttractors and Repulsors

The use of the inverse square law helps to dampen oscillation. However, it is inefficient to let the influence exerted by each member of the flock just degrade over distance since they will still exert an influence. It is necessary to introduce a cut-off as well.

Page 18: 1 CO3301 - Games Development 2 Week 22 Flocking Gareth Bellaby

18

Attractors and RepulsorsAttractors and Repulsors

The flock is in constant motion.

If you want to allow the flock to come to rest then it may prove to use further dampening or allow more complex combination of forces, e.g. so that a flock of birds can come to rest when they perch. The aim is allow still points to arise automatically.

Could tie the repulsion between flock members in with the speed of the members - although this would have to be a non-linear relationship (similar sort of reasoning to the inverse square used more generally in flocking).

Page 19: 1 CO3301 - Games Development 2 Week 22 Flocking Gareth Bellaby

19

More behaviourMore behaviour

Predator - prey relationships can be created.

Predators are to be avoided. A predator is repulsive. The individual will attempt to move away from a predator.

Prey is to be hunted. The prey is attractive. A predator will attempt to close with prey.

Note that real predators may utilise the natural response of their prey in order to trap them, e.g. wolves.

Page 20: 1 CO3301 - Games Development 2 Week 22 Flocking Gareth Bellaby

20

FormationsFormations

Previously talked about the use of relative positioning in a formation.

This can be used with flocking as well.

Page 21: 1 CO3301 - Games Development 2 Week 22 Flocking Gareth Bellaby

21

Attractors and RepulsorsAttractors and Repulsors

Lots more to explore.

Olsen, "Attractors and Repulsors", Game Programming Gems 4, discusses the summation of attractors and repulsors.

He also introduces a "mass" into the equation. The net effect of the "mass" would be to dampen the acceleration.

Olsen also discusses some alternative non-linear attraction curves.

Page 22: 1 CO3301 - Games Development 2 Week 22 Flocking Gareth Bellaby

22

Some referencesSome references

Craig Reynolds (1987), "Flocks, Herds, and Schools: A Distributed Behavioral Model", SIGGRAPH

http://www.red3d.com/cwr/papers/1987/boids.html

Bourg & Seemann, AI for Game Developers. Sample chapter:

http://www.gamecareerguide.com/features/282/book_excerpt_ai_for_game_developers.php

Steve Rabin, "Common Game AI Techniques", AI Game Programming Wisdom 2, 2003.

Page 23: 1 CO3301 - Games Development 2 Week 22 Flocking Gareth Bellaby

23

Some referencesSome references

Steven Woodcock, "Flocking with Teeth: Predators and Prey", Game Programming Gems 2, 2001.

John M. Olsen , "Attractors and Repulsors", Game Programming Gems 4, 2004.