NetLogo turtles leaving a trail that fades with time

寵の児 提交于 2019-12-04 02:54:45

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.)

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
StephenGuerin

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
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!