veer-from-edges


Information

Created with NetLogo version NetLogo 4.0.4
Running with NetLogoLite.jar version 404.


You could do this:
Choose a distance that is "too close" to the edge. When an agent is inside that zone, calculate the angle of reflection to the boundary (as if the agent was a beam of light). Make the agent turn toward that heading, until it escapes from the too-close zone.
;; ** assumes that inside-edge-boundary is a reporter that
;; reports true when the agent is too close to an edge
;; e.g. you could color the edge boundary a slightly
;; different color from the main field.
;; ** assumes that reflection-angle is a reporter that calculates the
;; the reflection angle
;; ** assumes that turn-speed is global or turtles-own'd number
;; between 0 (can't turn at all) and 1.0 (turns instantly)

Procedures

NetLogo Version: NetLogo 4.0.4

globals
[ bumper-color
   
  move-speed 
  boundary-width
]
turtles-own
[ veering? ]

to setup
   ca
   set bumper-color red - 3
   
   set move-speed .1
   set boundary-width 10
      
   ask patches with [    pxcor + boundary-width > max-pxcor
                      or pxcor - boundary-width < min-pxcor
                      or pycor + boundary-width > max-pycor
                      or pycor - boundary-width < min-pycor
                    ]
   [ set pcolor bumper-color ]
   
   ;;   ask n-of 100 patches with [ pcolor = black ]
   ;;   [ sprout 1
   ;;     [ rt random-float 360
   ;;       set shape "default"
   ;;       set color blue
   ;;       set veering? false
   ;;     ]
   ;;   ]
   let pop 100
   create-turtles 100
   [ home
     set heading who / (pop) * 360 
     jump 5
     set color blue
     set veering? false
   ]
end

to go
   ask turtles
   [ ifelse veering? = false
     [ ifelse inside-edge-boundary
       [ set color red
         set veering? true
       ]
       [ rt (turn-speed * (1 - random-float 2.0))
       ]
     ]
     [ ifelse pcolor = bumper-color
       [ lt turn-speed * subtract-headings heading towardsxy-nowrap 0 0
       ]
       [ set veering? false
         set color blue
       ]
     ]
     jump move-speed
   ]
   tick
end

to-report inside-edge-boundary
   report ( ( pcolor = bumper-color ) ) 
end

to-report reflection-angle
   report towardsxy-nowrap 0 0
end
      

                    


Download Link

View or download the complete model file (to download: right-click, save-link-as):
-- Download veer-from-edges --