15
1 Animation Software Development Development of Extendable Flocking System Volha Kolchyna MSc Computer Animation NCCA, Bournemouth University

Flocking System Report

Embed Size (px)

Citation preview

Page 1: Flocking System Report

1

Animation Software Development

Development of Extendable Flocking System

Volha Kolchyna

MSc Computer Animation NCCA, Bournemouth University

Page 2: Flocking System Report

2

Contents 1.The Goal ............................................................................................................................................... 3

2. Flocking System Research ............................................................................................................... 3

3. System Design ..................................................................................................................................... 6

3.1 Planning......................................................................................................................................... 6

3.2 Main classes design ....................................................................................................................... 6

3.3 Polices-based design ..................................................................................................................... 7

3.4 Classes Diagram ............................................................................................................................ 9

3.5 Advantages of Extendable Flocking System design ....................................................................... 10

4. Algorithms ..................................................................................................................................... 10

4.1 Spherical Rotation ....................................................................................................................... 10

4.2 Collision Avoidance ..................................................................................................................... 11

4.3 Reducing computational complexity ........................................................................................ 133

5.Screenshots........................................................................................................................................14

References............................................................................................................................................15

Page 3: Flocking System Report

3

1. The Goal

The goal of this project is to create Extendable flocking system.

Minimum requirements are:

- Possibility to dynamically create entities;

- Implementation of few types of behaviour (seek, flee, followLeader, etc.)

- Obstacle avoidance;

The idea is to design flexible and reusable system, which can be easily expanded to satisfy

additional requirements.

2. Flocking System Research

The most common framework to simulate flocking behaviour was developed by

Reynolds (1987). Reynolds’ paper aimed to represent the flocking behaviour of animals.

Reynolds’ framework is applicable to two dimensions and three dimensions. In 3D it

involved bird-like flocking agents (referred by Reynolds as boids); in 2D the movement of

sheep.

Reynolds framework expresses the direction of entities (“agents”). Each agent has some

perception of other agents as neighbours. Each agent is expressed by a simple point mass

model. For this simple point mass, the direction is the result of calculating the impact of the

different steering behaviours.

There are three main types of steering behaviours described by Reynolds, which make

possible the simulation of the movement of a flock. These steering behaviours are:

Collision avoidance, Velocity matching and Flock centring. Behaviours can be represented as

a force in Newtonian physics and can be computed in an acceleration of the agent.

In later paper Reynolds(1999) renamed these behaviours respectively: separation,

alignment and cohesion. Reynolds defines them to create a flocking movement as follows:

Separation: steering of the agent avoiding the collision with other agents (not with

obstacles), see Figure 2.1.

Mathematical description, a force result of the calculation of summing the difference

between the neighbours and the agent. Since each agent is described with a radius to

the point of mass, the sum of the difference is scaled by the inverse of the distance to

the neighbours. As a result the force increases as the agents come closer.

Page 4: Flocking System Report

4

Figure 2.1 Separation. (Reynolds, 1999)

Alignment: Is a behaviour that describes the process of steering to match the velocity of

an agent with its neighbours, see Figure 2.2. The mathematical description is the

average velocity of the neighbours.

Figure 2.2 Alignment. (Reynolds, 1999)

Cohesion: Refers to steering towards the average relative position of all the other

agents in the flock), see Figure 2.3. This steering is responsible of causing a herd

movement, by generating a tendency that all agents follow, thus herding together.

Mathematical description -sum of the position of neighbours, divided by number of

neighbours.

Figure 2.3 Cohesion. (Reynolds, 1999)

Page 5: Flocking System Report

5

For Reynolds an agent has incomplete information regarding the all the entities within a

flock. The averaged movement compensates for the restricted amount of information on

each one the agents while allowing the movement of the herd as a unity. Reynolds extends

his analysis to consider that an agent only requires information regarding the agents within

its radius of perception. Originally Reynolds describes a sphere of perception and later adds

a “field of view”, see Figure 2.4.

Figure 2.4 Neighbourhood. (Reynolds, 1999)

The scripted behaviour of the three basic steering forces is combined with obstacle

avoidance to emulate the flock/herd movement, see Figure 2.5. Obstacles generate a

steering force that directs the boids to the edge of the obstacle. It is mentioned that

obstacle could be also bigger agent that move with their own script of steering forces.

Figure 2.5 Obstacle avoidance. (Reynolds, 1999)

Reynolds criticizes in his own work the difficulty of combining the forces adequately to

generate the desired behaviour. Precisely because of this the adjustment of the behaviour

to the desired level can be a time consuming. Definitely this is an area that has enough

room for improvement.

Page 6: Flocking System Report

6

Reynolds mentions that it is possible to extend his approach to include other

behaviours: Flee, Seek, Pursuit, Evasion, Wander, Path Following, etc. This additional

behaviour enriches the ethological model simulations proposed by the Reynolds papers.

Taking the Reynlods framework as a starting point several authors have been working on

different methods of either optimizing the herd interaction or reducing the calculation

complexity. In this sense Le Bajec proposes weighting the steering behaviours to optimize

the flock movements and the boids interaction during navigation. Treuille(2006) introduces

the concept of individual potential fields instead of individual agent perception. By these

means Treuille is looking to create “global paths” that combine local avoidance in to a

combined calculation.

This section briefly summarizes the key aspects of the Reynolds framework for flock

systems and mentions areas of development by other authors or that can be developed.

3. System Design

3.1 Planning

The initial idea of the project is to develop design, which can be easily expanded to

create simulations of different types of groups of autonomous characters. One example of

application is simulation of ecosystem. In every ecosystem different types of organisms live

together and interact. Every type of organisms can be presented in a system as a separate

flock. This means, that to simulate real life environment, the system should support

possibility to create multiple flocks in one simulation. The interaction between the members

of one flock or members of different flocks can be implemented as various types of

behaviour. The addition of new type of behaviour to the system should not require

modification of the main classes.

3.2 Main classes design

The main classes are MovingObject, Boid, Flock, Environment, BasicBehaviour.

MovingObject is a class represents any moving object, which does not think and analyse

its behaviour. Typical example of this class is obstacle. When obstacle moves it does not

follow any particular behaviour and does not try to avoid collisions. In case when obstacle is

static, its velocity will be null.

Page 7: Flocking System Report

7

Boid is a child class of MovingObject. All “thinking” characters are instances of this class.

Objects of this class analyse the environment and calculate steeringForce to define the

direction of next movement. Essential for flock behaviours such as Alignment, Cohesion and

Separation are calculated within the class. Implementation of other types of behaviour is

delegated to the policy classes. When all forces affecting the boid are defined, the final

force is calculated taking in account the weight of each force. Then the properties of the

boid are updated and it is redrawn.

Flock class represents group of autonomous characters with common scripts. It has a

list of all boids which are part of this flock, leader of the flock, list of behaviours which this

flock perform, as well as list of weight for each type of behaviour. When the user changes

the behaviour of the flock, create or delete boids, define a leader the modifications to the

flock will be made according to the user choice.

Environment is the class with information about all the entities in ecosystem/space. It

contains list of all flocks and obstacles. There is possibility for the user to add/delete

obstacles and flocks, change the size of environment.

3.3 Polices-based design

It was decided to implement different types of behaviour as separate policies. Policy is a

class, which put emphasis only on behaviour. “Policies and policy classes help in

implementing safe, efficient, and highly customizable design elements” says Andrei

Alexandrescu in his book "Modern C++ design", see (Alexandrescu, 2001). Using this

approach the following behavioural classes were designed:

Figure 3.3 Behaviour classes diagram

Page 8: Flocking System Report

8

BasicBehaviour class is a parent class for all possible types of behaviour. It has virtual

method CalculateNavigationForce(* Boid), which will be overridden in child classes to

provide various unique behaviours.

Below is the code of how implementation may look like:

class BasicBehaviour

{

public:

virtual void CalculateNavigationForce (); // with parent implementation

};

class SpecificBehaviour : public BasicBehaviour

{

public:

virtual void CalculateNavigationForce (); // with child implementation

};

...

class Flock

{

private:

std::vector<BasicBehaviour*> m_behaviours;

}

//...

m_behaviours.push_back(new SpecificBehaviour());

m_behaviours.push_back(new SpecificBehaviour2());

//...

//After that any call to:

m_behaviours[i]-> CalculateNavigationForce ()

//will call the actual child implementation

There is already existing project MetaAgent (Halleux, 2003), which simulates steering

behaviour using policies. The author applies templates in his project and also creates totally

different characters/agents by merging different policies:

// agent will go round

agent< point_mass_model, circle_move_behavior > circle_mover;

// this agent will track a target

agent< point_mass_model, seek_behavior > seeker;

Page 9: Flocking System Report

9

MetaAgent implementation is limiting the agent to one type of behaviour at a time.

In present project the inheritance from BasicBehaviour class is used, what allowed to create

listOfNavigationBehaviours of parent type and perform numerous behaviours from this list

by each agent.

3.4 Classes Diagram

Figure 3.4. Classes Diagram

Page 10: Flocking System Report

10

3.5 Advantages of Extendable Flocking System design

- Easy to add new types of behaviour or combine existing ones to achieve desirable

motion;

- Various types of characters can be created by combining one base class Boid with

different behaviours. For example, Prey and Predator will be instances of the same

class Boid, but have different behaviours AvoidPredator and Hunt accordingly;

- All classes are created with the right properties and methods which match real life.

There are no properties or methods which are not supposed to be part of that class.

For example a flock consists of boids and has a leader, but it does not include

predators and obsticles, since they are not part of the flock;

- It is possible to simulate real ecosystem by creating many flocks of different

characters in one simulation;

- The leader of the flock can be a member of the other Flock. Example of application

of such behaviour: There is a group of tourists which consists of families. Guide of

the group is the leader for the “flock” of tourists. Father of the family is the leader

for the “flock” of his children. Children follow father, father follows guide. Possibility

of such behaviour is included in the design of the system, though further

implementation is required to check the correctness;

4. Algorithms

4.1 Spherical Rotatio

When the boid is at the origin, it is required to rotate its position towards the

point U.

Figure 4.1 Sphere defined in spherical coordinates. (Macey, 2003, p.41)

Page 11: Flocking System Report

11

R is the radial distance from the origin to U, φ is the angle between U and the xz-plane (latitude of

point U) and lies in the interval –π/2 ≤ φ<π/2

Ɵ is the azimuth of U and lies in the interval 0≤ Ɵ<2π

The relationships between Cartesian coordinates and spherical can be described as following:

Ux = Rcos(Ɵ)cos(φ), Uy = Rsin(φ), Uz = Rcos(φ)sin(Ɵ)

R = , Ɵ = arctan(Uz, Ux, ), φ = (Uy/R)

Arctan(y,x) =

4.2 Collision Avoidance

Different methods can be used to avoid collisions. It is important to balance the

computation complexity and the effectiveness of technique.

One of the simple ways is to create repelling force field around each object[Parent]. If the

boid is positioned on the safe distance from the object, there is no force applied to it. As soon as

distance goes under the certain value, the repelling force starts pushing the boid away from the

object. Closer the member of flock is to the object, greater the effect of distance-based force is, see

Figure 3.2.

Figure 4.2.1 Force field collision avoidance. (Parent, 2002, p.403)

Page 12: Flocking System Report

12

This method has few drawbacks. One of them is that it does not allow to the boid to travel

parallel or close to the surface. Another problem appears, when collision avoidance forces results in

a force vector which points directly to the boid. In this situation no direction is defined for flock

member to move.

The collision avoidance method which is planned to be used in this project and provides

better results is using bounding spheres to divert boid’s path around the obsticles. When flock

member gets inside this bounding sphere of the object, its direction vector will be tested, see

Figure 3.2.2. If the is a possibility of collision, method to redirect the boid should be executed.

Figure 4.2.2 Testing for potential collision with a bounding sphere. (Parent, 2002, p.403)

In order to avoid the collision the member of flock will be sent to the boundary point of the

bounding sphere. To calculate that point the math from Figure 3.2.3 can be used.

Figure 4.2.3 Calculation of point B on the boundary of sphere. (Parent, 2002, p.403)

Page 13: Flocking System Report

13

4.3 Reducing computational complexity

If each boid had to determine the positions of each one of the other boids to

determine its closest neighbours, the efficiency of the program would fall dramatically. The

computation complexity would achieve O(n2). For the purpose of reducing the complexity

Reynolds suggested to store the boids in some kind of spacial data structure. The idea is to

pre-sort the members of a flock based on their location in space. This is will allow to easily

find the neighbourhood of a character withought need to examine the whole flock.

In this project bin-lattice spatial subdivision will be used. The whole environment will

be divided into boxes “bins”, see Figure 3.3. Each member of flock is located inside of one

of the boxes, based on his position. If the box of the boid is changed while his movement, he

has to update the membership. Each boid also has a sphere of influence. During analysis

bins which overlap with the sphere will be found. Boids in each of those bins will later be

tested to see if they are located within the sphere of interest. If so, they will become a part

of neighbourhood.

Figure 4.3 A query sphere inside the bin-lattice spatial data structure. (Reynolds, 2000)

This method allows to reduce the computation expense to O(n). As an alternative

Reynolds considered also the use of Binary space partioning trees(BSP).

Page 14: Flocking System Report

14

3. Screenshots

Figure 5.1 Flocking System. Obstacles and 2 flocks

Figure 5.2 Flocking System. Obstacles and 1 flock

Figure 5.3 Flocking System. 3 flocks

Page 15: Flocking System Report

15

References

[1] Alexandrescu, A. (2001), Modern C++ Design: Generic Programming And Design Patterns Applied

[2] Bajec, I., L., Zimic, N. and M. Mraz. The computational beauty of ocking: boids revisited.

Mathematical and Computer Modelling of Dynamical Systems.

[3] Bourg, D . 2002. Physics for Game Developers. O'Reilly and Associates

[4] Halleux, J.2003. MetaAgent, a Steering Behavior Template Library, Retrived on

17/10/2010 from http://www.codeproject.com/KB/library/metaagent.aspx

[5] Macey J, 2003, E.B.P.L. USER GUIDE AND LANGUAGE REFERENCE, Retrved on 09/11/2010

from

http://nccastaff.bournemouth.ac.uk/jmacey/MastersProjects/MyMSc/ebpluserguide.pdf

[6] Parent R, 2002.Computer Animation Algorithms and Techniques, Morgan Kaufmann

Publishers, San Francisco, USA

[7] Reynolds, C. W. (1987), Flocks, herds and schools: A distributed behavioral model, ACM

SIGGRAPH Computer Graphics, v.21 n.4, p.25-34

[8] Reynolds, C. W. (1999) Steering Behaviors For Autonomous Characters, in the

proceedings of Game Developers Conference 1999 held in San Jose, California. Miller

Freeman Game Group, San Francisco, California. Pages 763-782.

[9] Reynolds, C. W. (2000) Interaction with Groups of Autonomous Characters, in the

proceedings of Game Developers Conference 2000, CMP Game Media Group (formerly:

Miller Freeman Game Group), San Francisco, California, pages 449-460.

[10] Treuille, A., S. Cooper, and Z. Popovic.2006. Continuum crowds. In ACM SIGGRAPH 2006

Papers, page 1168. ACM.