11

I have turtles moving across the view, and I'd like to be able to follow where they go by making them leave a trail behind them, as though they were emitting smoke as they went. Of course, I could use the turtle pen (pen-down), but since there are many turtles, the view rapidly gets filled with old trails. The solution could be trails that last only for a few ticks before they dissipate. But I don't know how to achieve that.

To be more specific: 1) Is there a technique for making the line drawn following a pen-down command gradually fade away over the period of some ticks? 2) If not, is there a way of removing the line drawn using the pen a few ticks after it was drawn? 3) If not, is there some other technique that would have a similar visual effect?

Seth Tisue
  • 29,985
  • 11
  • 82
  • 149
Nigel
  • 585
  • 1
  • 5
  • 20

3 Answers3

8

There is no way to fade the trails in the drawing layer over time. If you want trails that fade, you'll need to represent the trails using turtles instead.

Here's sample code for having "head" turtles that trail ten-turtle "tails" behind them:

breed [heads head]
breed [tails tail]
tails-own [age]

to setup
  clear-all
  set-default-shape tails "line"
  create-heads 5
  reset-ticks
end

to go
  ask tails [
    set age age + 1
    if age = 10 [ die ]
  ]
  ask heads [
    hatch-tails 1
    fd 1
    rt random 10
    lt random 10
  ]
  tick
end

I'm just killing off the old trails outright, but you could also add code that fades their color over time. (An example of a model that does that is the Fire model, in the Earth Science section of the NetLogo Models Library.)

Seth Tisue
  • 29,985
  • 11
  • 82
  • 149
  • I think you also can use this inside asking heads : ask patch-here [ Sprout-tails 1 ] – Marzy Jan 12 '14 at 23:14
  • for color if you set turtle 's initial color white, in each movement you can set color color - 1 which shows a fading effect – Marzy Jan 12 '14 at 23:19
  • 1
    Asking the patch to `sprout` the tail would lose the heading and the precise xcor and ycor, which `hatch` preserve. – Seth Tisue Jan 12 '14 at 23:26
  • Being new in the NetLogo domain, let me ask a novice question about your code: is there any particular ABM reasoning to prefer to precede ( set age age + 1 ) and only next test ( if age = 10 [ die ] )? My gut feeling tells me a performance-wise better ordering ought be to first reduce the amount of the agentset ( if age = 9 [ die ] ), so that next we carry any further operation(s) ( potentially expensive ) only on the still active remainder. Is there any reason ( blocking, gc or other performance related reasoning ) not to do so? Thank you. – user3666197 Feb 19 '22 at 12:37
4

Here's a version based on the same principle as the one by @SethTisue, but the tails fade away:

globals [ tail-fade-rate ]
breed [heads head]    ; turtles that move at random
breed [tails tail]    ; segments of tail that follow the path of the head

to setup
  clear-all              ;; assume that the patches are black
  set-default-shape tails "line"
  set tail-fade-rate 0.3 ;; this would be better set by a slider on the interface
  create-heads 5
  reset-ticks
end

to go
  ask tails [
    set color color - tail-fade-rate ;; make tail color darker
    if color mod 10 < 1  [ die ]     ;; die if we are almost at black
  ]
  ask heads [
    hatch-tails 1        
    fd 1
    rt random 10
    lt random 10
  ]
  tick
end
Nigel
  • 585
  • 1
  • 5
  • 20
2

Here's another approach but without using additional turtles. I include it for variety sake - I would recommend going with Seth's approach first.

In this approach, each turtle keeps a fixed length list of previous locations and headings and stamps out the last position. There's some unwanted artifacts with this approach and is not as flexible as using additional turtles, but I think it uses less memory which may help on larger models.

turtles-own [tail]

to setup
  ca
  crt 5 [set tail n-values 10 [(list xcor ycor heading)] ] 
end

to go
  ask turtles [
    rt random 90 - 45 fd 1
    stamp

    ; put current position and heading on head of tail
    set tail fput (list xcor ycor heading) but-last tail

    ; move to end of tail and stamp the pcolor there
    let temp-color color
    setxy (item 0 last tail) (item 1 last tail)
    set heading (item 2 last tail)
    set color pcolor set size 1.5   stamp

    ; move back to head of tail and restore color, size and heading
    setxy (item 0 first tail) (item 1 first tail) 
    set heading item 2 first tail
    set size 1  set color temp-color
    ]
end
Seth Tisue
  • 29,985
  • 11
  • 82
  • 149
StephenGuerin
  • 871
  • 4
  • 4