# Flocking 3D

Do you have questions or comments about this model? Ask them here! (You'll first need to log in.)

### WHAT IS IT?

This is a 3D version of the Flocking model in the NetLogo Models Library. This model is an attempt to mimic the flocking of birds. (The resulting motion also resembles schools of fish.) The flocks that appear in this model are not created or led in any way by special leader birds. Rather, each bird is following exactly the same set of rules, from which flocks emerge.

The birds follow three rules: "alignment", "separation", and "cohesion". "Alignment" means that a bird tends to turn so that it is moving in the same direction that nearby birds are moving. "Separation" means that a bird will turn to avoid another bird which gets too close. "Cohesion" means that a bird will move towards other nearby birds (unless another bird is too close). When two birds are too close, the "separation" rule overrides the other two, which are deactivated until the minimum separation is achieved.

The three rules affect only the bird's heading. Each bird always moves forward at the same constant speed.

### HOW TO USE IT

First, determine the number of birds you want in the simulation and set the POPULATION slider to that value. Press SETUP to create the birds, and press GO to have them start flying around.

The default settings for the sliders will produce reasonably good flocking behavior. However, you can play with them to get variations:

The three turn sliders (MAX-COHERE-TURN, MAX-ALIGN-TURN, and MAX-SEPARATE-TURN) control the maximum angle a bird can turn as a result of each rule.

VISION is the distance that each bird can see 360 degrees around it.

FOLLOW BIG FLOCK and WATCH BIG FLOCK - select a random bird with the maximum number of FLOCKMATES and follows or watches appropriately.

### THINGS TO NOTICE

Central to the model is the observation that flocks form without a leader.

There are no random numbers used in this model, except to position the birds initially. The fluid, lifelike behavior of the birds is produced entirely by deterministic rules.

Also, notice that each flock is dynamic. A flock, once together, is not guaranteed to keep all of its members. Why do you think this is?

After running the model for a while, all of the birds have approximately the same heading. Why?

Sometimes a bird breaks away from its flock. How does this happen? You may need to slow down the model or run it step by step in order to observe this phenomenon.

### THINGS TO TRY

Play with the sliders to see if you can get tighter flocks, looser flocks, fewer flocks, more flocks, more or less splitting and joining of flocks, more or less rearranging of birds within flocks, etc.

You can turn off a rule entirely by setting that rule's angle slider to zero. Is one rule by itself enough to produce at least some flocking? What about two rules? What's missing from the resulting behavior when you leave out each rule?

Will running the model for a long time produce a static flock? Or will the birds never settle down to an unchanging formation? Remember, there are no random numbers used in this model.

### EXTENDING THE MODEL

Currently the birds can "see" all around them. What happens if birds can only see in front of them? The IN-CONE primitive can be used for this.

Is there some way to get V-shaped flocks, like migrating geese?

What happens if you put walls around the edges of the world that the birds can't fly into?

Can you get the birds to fly around obstacles in the middle of the world?

In this model, the birds turn left or right (change in yaw) without rolling. Real birds bank when they turn. Can you change the basic rules of the model so that the flocking behavior results from banking?

What would happen if you gave the birds different velocities? For example, you could make birds that are not near other birds fly faster to catch up to the flock. Or, you could simulate the diminished air resistance that birds experience when flying together by making them fly faster when in a group.

Are there other interesting ways you can make the birds different from each other? There could be random variation in the population, or you could have distinct "species" of bird.

### NETLOGO FEATURES

Compare this model to the 2D version in the NetLogo Models Library. Notice how similar

the procedures tab is to the other model but also notice the use of pitch-related primitives as well.

Notice the need for the SUBTRACT-HEADINGS primitive and special procedure for averaging groups of headings. Just subtracting the numbers, or averaging the numbers, doesn't give you the results you'd expect, because of the discontinuity where headings wrap back to 0 once they reach 360.

### CREDITS AND REFERENCES

This model is inspired by the Boids simulation invented by Craig Reynolds. The algorithm we use here is roughly similar to the original Boids algorithm, but it is not the same. The exact details of the algorithm tend not to matter very much -- as long as you have alignment, separation, and cohesion, you will usually get flocking behavior resembling that produced by Reynolds' original model. Information on Boids is available at http://www.red3d.com/cwr/boids/.

### HOW TO CITE

If you mention this model in an academic publication, we ask that you include these citations for the model itself and for the NetLogo software:

- Wilensky, U. (1998). NetLogo Flocking 3D model. http://ccl.northwestern.edu/netlogo/models/Flocking3D. Center for Connected Learning and Computer-Based Modeling, Northwestern University, Evanston, IL.

- Wilensky, U. (1999). NetLogo. http://ccl.northwestern.edu/netlogo/. Center for Connected Learning and Computer-Based Modeling, Northwestern University, Evanston, IL.

In other publications, please use:

- Copyright 1998 Uri Wilensky. All rights reserved. See http://ccl.northwestern.edu/netlogo/models/Flocking3D for terms of use.

### COPYRIGHT NOTICE

Copyright 1998 Uri Wilensky. All rights reserved.

Permission to use, modify or redistribute this model is hereby granted, provided that both of the following requirements are followed:

a) this copyright notice is included.

b) this model will not be redistributed for profit without permission from Uri Wilensky. Contact Uri Wilensky for appropriate licenses for redistribution for profit.

This is a 3D version of the 2D model Flocking.

This model was created as part of the projects: PARTICIPATORY SIMULATIONS: NETWORK-BASED DESIGN FOR SYSTEMS LEARNING IN CLASSROOMS and/or INTEGRATED SIMULATION AND MODELING ENVIRONMENT. The project gratefully acknowledges the support of the National Science Foundation (REPP & ROLE programs) -- grant numbers REC #9814682 and REC-0126227.

## Comments and Questions

turtles-own [ flockmates ;; agentset of nearby turtles nearest-neighbor ;; closest one of our flockmates ] globals [ scale ] to setup ca set scale 0.25 ;; scale let's us have a bigger world without too many patches crt population [ set color yellow - 2 + random 7 ;; random shades look nice setxyz random-xcor random-ycor random-zcor right random-float 360 tilt-up asin (1.0 - random-float 2.0) roll-right random-float 360 ] ask turtle 0 [ set color red ] end to go ask turtles [ flock ] repeat 5 [ ask turtles [ fd scale / 2 ] display ] tick end to flock ;; turtle procedure find-flockmates if any? flockmates [ find-nearest-neighbor ifelse distance nearest-neighbor < ( minimum-separation * scale ) [ separate ] [ align cohere ] ] end to find-flockmates ;; turtle procedure set flockmates other turtles in-radius (vision * scale) end to find-nearest-neighbor ;; turtle procedure set nearest-neighbor min-one-of flockmates [distance myself] end ;;; SEPARATE to separate ;; turtle procedure turn-away ([heading] of nearest-neighbor) max-separate-turn pitch-away ([pitch] of nearest-neighbor) max-separate-turn end ;;; ALIGN to align ;; turtle procedure turn-towards average-flockmate-heading max-align-turn pitch-towards average-flockmate-pitch max-align-turn end to-report average-flockmate-heading ;; turtle procedure ;; We can't just average the heading variables here. ;; For example, the average of 1 and 359 should be 0, ;; not 180. So we have to use trigonometry. report atan sum [sin heading] of flockmates sum [cos heading] of flockmates end to-report average-flockmate-pitch ;; turtle procedure ;; We can't just average the heading variables here. ;; For example, the average of 1 and 359 should be 0, ;; not 180. So we have to use trigonometry. report atan sum [sin pitch] of flockmates sum [cos pitch] of flockmates end ;;; COHERE to cohere ;; turtle procedure turn-towards average-heading-towards-flockmates max-cohere-turn pitch-towards average-pitch-towards-flockmates max-cohere-turn end to-report average-heading-towards-flockmates ;; turtle procedure ;; "towards myself" gives us the heading from the other turtle ;; to me, but we want the heading from me to the other turtle, ;; so we add 180 report atan mean [sin (towards myself + 180)] of flockmates mean [cos (towards myself + 180)] of flockmates end to-report average-pitch-towards-flockmates ;; turtle procedure report mean [0 - (towards-pitch myself)] of flockmates end ;;; HELPER PROCEDURES to turn-towards [new-heading max-turn] ;; turtle procedure turn-at-most (subtract-headings new-heading heading) ( max-turn * scale ) end to turn-away [new-heading max-turn] ;; turtle procedure turn-at-most (subtract-headings heading new-heading) ( max-turn * scale ) end to pitch-towards [new-pitch max-turn] ;; turtle procedure pitch-at-most (subtract-headings new-pitch pitch) ( max-turn * scale ) end to pitch-away [new-pitch max-turn] ;; turtle procedure pitch-at-most (subtract-headings pitch new-pitch) ( max-turn * scale ) end ;; turn right by "turn" degrees (or left if "turn" is negative), ;; but never turn more than "max-turn" degrees to turn-at-most [turn max-turn] ;; turtle procedure ifelse abs turn > max-turn [ ifelse turn > 0 [ rt max-turn ] [ lt max-turn ] ] [ rt turn ] end to pitch-at-most [turn max-turn] ;; turtle procedure ifelse abs turn > max-turn [ ifelse turn > 0 [ tilt-up max-turn ] [ tilt-down max-turn ] ] [ tilt-up turn ] end ; Copyright 1998 Uri Wilensky. All rights reserved. ; The full copyright notice is in the Information tab.

There are 3 versions of this model.

## Attached files

File | Type | Description | Last updated | |
---|---|---|---|---|

Flocking 3D.png | preview | Preview for 'Flocking 3D' | over 7 years ago, by Uri Wilensky | Download |

This model does not have any ancestors.

This model does not have any descendants.