# Flocking 3D Alternate

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 vector-based 3D flocking model, based on Jon Klein's implementation of Craig Reynolds' Boids algorithm. Each bird is influenced by a series of urges. By assigning different weights to each urge, the birds exhibit different flocking behaviors.

### HOW IT WORKS

Each bird has vectors for velocity and acceleration, that is, a velocity and acceleration in the x, y, and z directions. Each of these vectors is influenced by six urges, to be near the center of the flock, to have the same velocity as the rest of the flock, to be keep spacing correct, to avoid colliding with obstacles, to be near to the center of the world and to wander throughout the world. Each of these factors affects the resulting velocity and acceleration; however, the sliders in the interface weight the amount that each has an effect.

### HOW TO USE IT

Choose the number of birds with the POPULATION slider. Press SETUP to create the birds. Press GO to start the simulation.

BUILD-CUBES randomly places floating-ball obstacles throughout the world. BUILD-WALL creates a randomly placed wall.

VISION is the distance that a bird can see around it. MAX-VELOCITY is the fastest a bird can go and MAX-ACCELERATION is the fastest a bird can accelerate. CRUISE-DISTANCE is the minimum distance from a nearby bird for a bird to feel comfortable.

A bird's movement is determined by the set of urges acting on that bird:

CENTER-CONSTANT - urge to move towards the center of its flock

VELOCITY-CONSTANT - urge to align its velocity with the velocity of her flockmates

SPACING-CONSTANT - urge to be no closer than CRUISE-DISTANCE from other birds

AVOIDANCE-CONSTANT - urge to avoid colliding with obstacles

WORLD-CENTER-CONSTANT - urge to avoid the edges of the world

WANDER-CONSTANT - urge to move in a random way

The urge-constant sliders control the weight of each urge in determining the birds' behavior.

If the VERTICAL-SPACING? switch is off the spacing urge only operates horizontally.

If a bird hits an obstacle, the bird dies. The COUNT DEAD-BIRDS monitor shows the number of dead birds.

### THINGS TO NOTICE

Compare how long it takes the birds to form flocks in this model to how long it takes in the regular 3D Flocking model.

Given the default settings the flocks travel in a circular pattern.

### THINGS TO TRY

Turn VERTICAL-SPACING? off and notice how it changes the shape of the flocks.

Try changing the different constants so that the birds become better or worse at avoiding obstacles.

### EXTENDING THE MODEL

Try adding different urges to the model; also try making different types of obstacles for birds to avoid.

Create different breeds of birds that behave differently from each other and see how they interact.

### NETLOGO FEATURES

This model uses lists and map fairly extensively to represent and manipulate vectors.

### CREDITS AND REFERENCES

This model is based on Jon Klein's "Swarm" demo for breve, (see http://www.spiderland.org/breve/) which was inspired by Craig Reynolds classic Boids flocking algorithm.

### 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. (2005). NetLogo Flocking 3D Alternate model. http://ccl.northwestern.edu/netlogo/models/Flocking3DAlternate. 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 2005 Uri Wilensky. All rights reserved. See http://ccl.northwestern.edu/netlogo/models/Flocking3DAlternate for terms of use.

### COPYRIGHT NOTICE

Copyright 2005 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.

## Comments and Questions

breed [ birds bird ] breed [ dead-birds dead-bird ] birds-own [ flockmates ;; agentset of nearby turtles nearest-neighbor ;; closest one of our flockmates velocity ;; vector with x y and z components it is determined by the previous velocity ;; and the current acceleration at each step acceleration ;; vector with x y and z components, determined by the six urges ] ;; ;; Setup Procedures ;; to setup ca setxyz 0 -30 0 create-birds population [ place-bird set size 0.75 set velocity [ 0 0 0 ] set acceleration [ 0 0 0 ] set color yellow + 2 - random-float 6 ] ask turtle 0 [ set color red ] ;; checkered floor ask patches with [ pzcor = min-pzcor ] [ set pcolor 3 + green + ( pxcor + pycor ) mod 2 ] end to place-bird ;; turtle procedure setxyz random-xcor random-ycor random-zcor ;; make sure that the birds are not on the floor or inside the ;; any obstacles that have been created. while [ pcolor != black ] [ setxyz random-xcor random-ycor random-zcor ] end ;; ;; Runtime Procedures ;; to go ask birds [ flock ;; if we've hit something ;; we're a goner if pcolor != black [ hatch-dead-birds 1 [ bk 1 ] die ] ] ;; ask the dead ones to continue falling ask dead-birds [ fall ] tick end to flock ;; bird procedure ;; look for birds in my vicinity find-flockmates ;; acceleration at each step is determined ;; entirely by the urges set acceleration (list 0 0 0) add-urge world-center-urge world-center-constant add-urge wander-urge wander-constant add-urge avoid-obstacle-urge avoidance-constant ;; if I'm not in a flock ignore the flock related ;; urges if count flockmates > 0 [ add-urge spacing-urge spacing-constant add-urge center-urge center-constant add-urge velocity-urge velocity-constant ] ;; keep the acceleration within the accepted range if magnitude acceleration > max-acceleration [ set acceleration (scale max-acceleration normalize acceleration) ] ;; the new velocity of the bird is sum of the acceleration ;; and the old velocity. set velocity (add velocity acceleration) ;; keep the velocity within the accepted range if magnitude velocity > max-velocity [ set velocity (scale max-velocity normalize velocity) ] let nxcor xcor + ( item 0 velocity ) let nycor ycor + ( item 1 velocity ) let nzcor zcor + ( item 2 velocity ) facexyz nxcor nycor nzcor fd 0.1 * magnitude velocity end to add-urge [urge factor] ;; bird procedure set acceleration add acceleration scale factor normalize urge end to-report world-center-urge ;; bird reporter ifelse distancexyz 0 0 0 > 10 [ report (list (- xcor) (- ycor) (- zcor) ) ] [ report (list 0 0 0) ] end to-report center-urge ;; bird reporter ;; report the average distance from my flockmates ;; in each direction if count flockmates = 0 or center-constant = 0 [ report (list 0 0 0) ] report (map [ ?2 - ?1 ] (list xcor ycor zcor) (list mean [ xcor ] of flockmates mean [ ycor ] of flockmates mean [ zcor ] of flockmates ) ) end to-report velocity-urge ;; bird reporter ;; report the average difference in velocity ;; from my flock mates if count flockmates = 0 or velocity-constant = 0 [ report (list 0 0 0) ] report ( map [ ?1 - ?2 ] (list mean [ item 0 velocity ] of flockmates mean [ item 1 velocity ] of flockmates mean [ item 2 velocity ] of flockmates ) velocity ) end to-report wander-urge ;; turtle reporter ;; report 3 random numbers between -1 and 1 report n-values 3 [ (random-float 2) - 1 ] end to-report spacing-urge ;; turtle reporter let urge [ 0 0 0 ] ;; report the sum of the distances to birds ;; in my flock that are closer to me than ;; cruise-distance ask flockmates with [ distance myself < cruise-distance ] [ set urge add urge (subtract (list [xcor] of myself [ycor] of myself [zcor] of myself) (list xcor ycor zcor)) ] ;; when vertical spacing is not on ignore the ;; z component of the spacing urge. if not vertical-spacing? [ set urge lput 0 but-last urge ] report urge end to-report avoid-obstacle-urge ;; turtle reporter let urge (list 0 0 0) if avoidance-constant = 0 [ report urge ] ;; report the sum of the distances from ;; any patches that are obstacles ;; in each direction ask patches in-radius vision with [ pcolor != black ] [ set urge add urge subtract (list [xcor] of myself [ycor] of myself [zcor] of myself) (list pxcor pycor pzcor) ] report urge end to find-flockmates ;; turtle reporter ;; latch on to the nearby birds set flockmates other birds in-radius vision end to fall ;; dead-bird procedure if zcor > min-pzcor + 1 [ set zcor zcor - 0.5 ] end ;; ;; world building ;; to build-cubes ask n-of 20 patches [ set pcolor blue + random 3 ask neighbors [ set pcolor blue + random 3 ] ] end to build-wall let x max-pxcor - random world-width let ydir 1 - random 2 if ydir = 0 [ set ydir -1 ] let height random max-pxcor ask patches with [ pxcor = x and ydir * pycor < 0 and pzcor < height ] [ set pcolor gray + random 2 ] end ;; ;; vector operations ;; to-report add [ v1 v2 ] report (map [ ?1 + ?2 ] v1 v2) end to-report subtract [ v1 v2 ] report (map [ ?1 - ?2 ] v1 v2) end to-report scale [ scalar vector ] report map [ scalar * ? ] vector end to-report magnitude [ vector ] report sqrt sum map [ ? * ? ] vector end to-report normalize [ vector ] let m magnitude vector if m = 0 [ report vector ] report map [ ? / m ] vector end ; Copyright 2005 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 Alternate.png | preview | Preview for 'Flocking 3D Alternate' | over 9 years ago, by Uri Wilensky | Download |

This model does not have any ancestors.

This model does not have any descendants.