The applet requires Java 5 or higher. Java must be enabled in your browser settings. Mac users must have Mac OS X 10.4 or higher. Windows and Linux users may obtain the latest Java from Sun's Java site.

powered by NetLogo

view/download model file: avoid_obstacle_4_0_bergey.nlogo


This model adds obstacle avoidance functionality onto the flocking model of Reynolds'
"boids" as implemented by Uri Wilensky.


The birds' first prioroity is to avoid obstacles. Once that is achieved, they go about
their flocking business.


Obstacle avoidance functionality added by Christina Bergey,,
as part of a primate behavior modelling class taught by Tony Di Fiore.

Original Model 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


Original Model Citation Info:
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 model. Center for Connected Learning and Computer-Based Modeling, Northwestern University, Evanston, IL.
- Wilensky, U. (1999). 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 for terms of use.


Original Model 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 model was created as part of the project: CONNECTED MATHEMATICS: MAKING SENSE OF COMPLEX PHENOMENA THROUGH BUILDING OBJECT-BASED PARALLEL MODELS (OBPML). The project gratefully acknowledges the support of the National Science Foundation (Applications of Advanced Technologies Program) -- grant numbers RED #9552950 and REC #9632612.

This model was converted to NetLogo 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. Converted from StarLogoT to NetLogo, 2002.


breed [ boids boid ]
breed [ targets target ]  ;; Targets added by C. Bergey

boids-own [
  flockmates         ;; agentset of nearby turtles
  nearest-neighbor   ;; closest one of our flockmates

to setup
  ;; Target part added by C. Bergey
  create-targets num-targets [
   setxy random-xcor random-ycor
   ;setxy 0 0 
   set size 3
   set shape "target"
   set color magenta 
  create-boids population
    [ set color yellow - 2 + random 7  ;; random shades look nice
      set size 1.5  ;; easier to see
      setxy random-xcor random-ycor
      set shape "line"]

to go
  ask boids [
    ;; This avoid-target bit added by C. Bergey
    ifelse count targets in-cone target-dist target-sight-cone > 0 [
    ] [
  ;; the following line is used to make the turtles
  ;; animate more smoothly.
  repeat 5 [ ask boids [ fd 0.2 ] display ]
  ;; for greater efficiency, at the expense of smooth
  ;; animation, substitute the following line instead:
  ;;   ask turtles [ fd 1 ]

to flock  ;; turtle procedure
  if any? flockmates
    [ find-nearest-neighbor
      ifelse distance nearest-neighbor < minimum-separation
        [ separate ]
        [ align
          cohere ] ]
  if any? targets-on patch-here [ set color magenta ]

to find-flockmates  ;; turtle procedure
  set flockmates other boids in-radius vision

to find-nearest-neighbor ;; turtle procedure
  set nearest-neighbor min-one-of flockmates [distance myself]


to separate  ;; turtle procedure
  turn-away ([heading] of nearest-neighbor) max-separate-turn


to align  ;; turtle procedure
  turn-towards average-flockmate-heading max-align-turn

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.
  let x-component sum [sin heading] of flockmates
  let y-component sum [cos heading] of flockmates
  ifelse x-component = 0 and y-component = 0
    [ report heading ]
    [ report atan x-component y-component ]


to cohere  ;; turtle procedure
  turn-towards average-heading-towards-flockmates max-cohere-turn

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
  let x-component mean [sin (towards myself + 180)] of flockmates
  let y-component mean [cos (towards myself + 180)] of flockmates
  ifelse x-component = 0 and y-component = 0
    [ report heading ]
    [ report atan x-component y-component ]


to turn-towards [new-heading max-turn]  ;; turtle procedure
  turn-at-most (subtract-headings new-heading heading) max-turn

to turn-away [new-heading max-turn]  ;; turtle procedure
  turn-at-most (subtract-headings heading new-heading) max-turn

;; 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 ]


to avoid-target
  let near-targets sort-by [[distance self] of ?1 < [distance self] of ?2] targets in-cone target-dist target-sight-cone

  foreach near-targets [
    ;let nearest-target min-one-of targets [distance self] 

    let target-heading towards ?
    let angle 0
    ifelse heading > towards ? [
      set angle (towards ? - 90)
    ] [
      set angle (towards ? + 90)
    turn-away angle max-target-turn * (1 - (distance ? / (target-dist * (position ? near-targets + 1))))

; Christina Bergey added the avoid-target functionality

; Original model Copyright 1998 Uri Wilensky. All rights reserved.
; The full copyright notice is in the Information tab.