;; SUMMARY
;;;;  A model where particles follow other particles, like a tail
;; COPYRIGHT & LICENSE
;;;; Copyright (C) 2003 James P. Steiner
;;;; Some Rights Reserved.
;;;; Creative Commons Attribution-NonCommercial-ShareAlike License v. 2.0.
;;;; Visit http://creativecommons.org/licenses/by-nc-sa/2.0/ for more information.

;; UPDATED to use NetLogo 3.1.1, Nov. 2006

Globals [ leader followers time old-pop]
turtles-own [my-leader order] 

To setup
  clear-all
  ask patches [set pcolor back-color ]
  set old-pop population
  Create-custom-turtles population
  [ set shape select-shape 
    set size 1.01 ;; disable use of pre-calculated shape, prevent clipping of shape!
  ]
  designate-leader
  designate-followers
  color-tail
  repeat 5 [ activate-tail ]
end 

to designate-leader
  Set leader turtle 0
  ask leader
  [ set color yellow
    setxy 0 0
    set heading 5
    set order 0
  ]
end

to designate-followers
  Set followers sort (turtles with [who != who-of leader])
  foreach followers [ ask ?
  [ set my-leader max-one-of turtles with [who < who-of myself] [who]
    set order (order-of my-leader) + 1
  ]]
end

to color-tail
    foreach followers [ ask ?
    [ set color scale-color tail-color order (population * 1.2) (population * -.2)
    ]]
end  

to move-head
    Ask leader ;; make leader loop about the universe
    [ if vary-turn? or vary-spd? [ set time time + frequency ]
      if turn?
      [ if vary-turn? [ set steering (sin time) ]
        rt steering * max-steer
      ]
      if move?
      [ ifelse vary-spd? 
        [ jump speed * abs(cos (time * 1.1)) ]
        [ jump speed ]
      ]
    ]
end

to move-tail
    foreach sort followers
    [ Ask ? ;; follow each other
      [ ;; point to leader
        face my-leader
        ;; step towards leader.
        jump (distance my-leader) * tightness
        ;; turn to an angle somewhere between leader's heading and the heading I took to get here.
        set heading ratio-heading heading (heading-of my-leader) stiffness
        ;; take a step back (e.g. to the "connection point" of the leader)
        jump (- spacing * tightness)
      ]
    ]
end

;; monitors the population slider for changes, and
;; dynamicly changes the tail length when needed
to check-pop
    if population != old-pop
    [ ifelse population < old-pop
      [ foreach n-of (old-pop - population ) followers
        [ ask ? [ die ] ]
      ]
      [ ask last followers [ hatch population - old-pop [ ]]
      ]
      designate-followers
      color-tail
      set old-pop population
    ]
end 

;; monitors tail-color slider
to check-color
   if not (shade-of? (color-of turtle 1) tail-color)
   [ color-tail ]
   if (pcolor-of patch 0 0) != back-color
   [ ask patches
     [ set pcolor back-color ]
   ]
end


to activate-tail
  every delay
  [ check-pop
    check-color
    move-head
    move-tail
  ]
end

to-report average-heading [a b]
; reports the simple average of two headings
    report atan ( .5 * ( (sin a) + (sin b) ) )
                ( .5 * ( (cos a) + (cos b) ) )
end

to-report ratio-heading [a b ratio]
; reports the weighted average of two headings
    let xx ( (sin a) * (1 - ratio) + (sin b) * (ratio) )
    let yy ( (cos a) * (1 - ratio) + (cos b) * (ratio) )
    ifelse xx != 0 or yy != 0
    [ report atan xx yy ]
    [ report 0 ]
end

@#$#@#$#@
GRAPHICS-WINDOW
301
10
668
398
25
25
7.0
1
10
1
1
1
0
1
1
1
-25
25
-25
25

CC-WINDOW
5
534
677
629
Command Center
0

BUTTON
10
45
65
78
NIL
setup
NIL
1
T
OBSERVER
T
NIL

BUTTON
10
97
123
130
NIL
activate-tail
T
1
T
OBSERVER
T
NIL

SWITCH
202
343
292
376
turn?
turn?
0
1
-1000

SLIDER
4
282
156
315
stiffness
stiffness
-0.5
1
0.5
0.1
1
/1

SLIDER
6
208
155
241
spacing
spacing
0.5
4
1.0
0.25
1
patches

SLIDER
166
379
292
412
steering
steering
-1
1
-0.42
0.01
1
NIL

BUTTON
146
487
213
520
turn-1-deg
ask leader [ rt 1 ]
NIL
1
T
OBSERVER
T
NIL

BUTTON
218
487
292
520
turn-45-deg
ask leader [ rt 45 ]
NIL
1
T
OBSERVER
T
NIL

SWITCH
190
451
292
484
vary-turn?
vary-turn?
0
1
-1000

SLIDER
10
380
134
413
speed
speed
0
2
0.2
0.01
1
spaces

SLIDER
46
245
156
278
tightness
tightness
0
1
0.5
0.1
1
/1

SLIDER
165
415
292
448
max-steer
max-steer
0.1
5
1.0
0.1
1
deg.

SLIDER
9
10
181
43
population
population
5
200
50
5
1
NIL

BUTTON
161
208
216
241
chain
set tightness 1\nset stiffness 0\nset spacing 1.0\nask turtles [ set size spacing + .01 ]
NIL
1
T
OBSERVER
T
NIL

BUTTON
219
208
274
241
rubber
set tightness .5\nset stiffness 0.5
NIL
1
T
OBSERVER
T
NIL

TEXTBOX
14
323
191
341
Head Movement Controls

TEXTBOX
12
188
102
206
Tail Properties

TEXTBOX
160
186
250
204
Tail Presets

BUTTON
181
243
255
276
collapsing
set tightness  0.1\nset stiffness -0.1\nask turtles [ set size spacing + .01 ]
NIL
1
T
OBSERVER
T
NIL

SWITCH
10
416
113
449
vary-spd?
vary-spd?
1
1
-1000

BUTTON
128
97
244
130
start/stop head
set move? not move?\nset turn? move?
NIL
1
T
OBSERVER
T
NIL

SWITCH
10
344
100
377
move?
move?
0
1
-1000

SLIDER
9
451
147
484
frequency
frequency
0.1
5
1.6
0.1
1
NIL

SLIDER
8
145
267
178
delay
delay
0
1
0.0
0.01
1
sec.

SLIDER
301
401
404
434
tail-color
tail-color
5
135
85
10
1
NIL

SLIDER
407
401
656
434
back-color
back-color
0.0
139.9
35.0
0.1
1
NIL

BUTTON
443
445
516
478
apply-shape
ask turtles [ set shape select-shape set size 1.01 ]\n\n
NIL
1
T
OBSERVER
T
NIL

CHOOSER
302
445
440
490
select-shape
select-shape
"chain" "link" "default-1" "default-2" "line-1" "line-2" "circle-1" "circle-2"
3

BUTTON
520
445
604
478
size=spacing
ask turtles [ set size spacing + .001 ]
NIL
1
T
OBSERVER
T
NIL

BUTTON
187
278
250
311
alive
set tightness .8\nset stiffness .5\nset spacing 1.25\nask turtles [ set size spacing + 1.01 ]
NIL
1
T
OBSERVER
T
NIL

TEXTBOX
77
47
284
89
Use "activate-tail" to start animation.  Use "start/stop head" to control movement.

@#$#@#$#@
Super-follower model

this simulates various sinuous following behavior: e.g. chain or rubber or spring like.

the head of the "snake" wander around the world. Head controls are explained at the bottom.


spacing: the desired gap between followers and leaders

tightness: how readily the follower closes the gap with it's leader. lower means more stretchy

stiffness: defines how "stiff" the rope is, i.e. how readily a follower turns to the same angle as it's leader: higher is stiffer

The special properties of some settings are not evident in any but very long tails.





@#$#@#$#@
default
true
0
Polygon -7500403 true true 150 5 40 250 150 205 260 250

link
true
6
Polygon -13840069 false true 150 -105 105 -150 150 -195 195 -150 150 -105 150 105 105 150 150 195 195 150 150 105

link direction
true
0
Line -7500403 true 150 150 30 225
Line -7500403 true 150 150 270 225

chain
true
6
Polygon -13840069 true true 0 105 0 -105 90 -195 210 -195 300 -105 300 105 210 195 90 195 30 135 240 135 240 -135 60 -135 60 135

circle-1
false
6
Circle -13840069 true true 0 0 300

circle-2
true
6
Circle -13840069 true true 0 -150 300

default-1
true
6
Polygon -13840069 true true 150 5 40 250 150 205 260 250

default-2
true
6
Polygon -13840069 true true 150 -145 40 100 150 55 260 100

line-1
true
6
Line -13840069 true 150 0 150 300

line-2
true
6
Line -13840069 true 150 -150 150 150

@#$#@#$#@
NetLogo 3.1.1
@#$#@#$#@
@#$#@#$#@
@#$#@#$#@
@#$#@#$#@
