netlogo

Netlogo 'towards' behaviour


For a pursuit-evasion assignment I need to use the NetLogo command towards, but it doesn't seem to work, or I don't understand it.
What I think it should do: give the angle between the heading of the turtle and the line connecting the turtle with the target.
Here's the code for a simple model I made just to show the problem.

to set-up
  clear-all
  create-turtles 2
  ask turtle 0 [
    set xcor 0
    set ycor 0
    set shape "circle"
  ]
  ask turtle 1 [
    set xcor min-pxcor
    set ycor max-pycor
    set heading 135
  ]
end

to go
  ask turtle 0 [ fd 0.1 ]
  ask turtle 1 [ show towards turtle 0 ]
end

And here's a video of the behaviour. https://youtu.be/MUBiAypppc4 (I couldn't find a way to remove the audio without just replacing it using YouTube's current editing system, I'm sorry; you'll have to mute the audio yourself)
Examples of expected behaviour:
from 0:14 to 0:19, I would expect the number to gradually decrease, not increase
at about 0:38, I would expect the number to be 0, not somewhere around 300
between 0:38 and 0:42, I would expect the number to decrease or increase consistently, without those two sudden jumps

Is there a problem somewhere, or does towards mean something different than I thought?


Solution

  • So turtle 0 is moving and turtle 1 is reporting the direction to turtle 0. I think towards is working fine but you have forgotten about the world settings. For example, in the 14-19s part, the shortest path from 0 to 1 is down and left (about 220 heading), but that shortest path is with the world wrapped. Your turtles can move off one side and come in on the other (as you can see turtle 1 doing).

    NetLogo measures distances and directions taking into account the wrapping configuration. It knows that the shortest path to get from turtle 0 to turtle 1 goes off the side and comes in the other, and reports the direction that the turtle would have to move to follow that path.

    Create a link and you can see this. Revised code:

    to set-up
      clear-all
      create-turtles 2
      ask turtle 0 [
        set xcor 0
        set ycor 0
        set shape "circle"
      ]
      ask turtle 1 [
        set xcor min-pxcor
        set ycor max-pycor
        set heading 135
        create-link-with turtle 0
      ]
    end
    
    to go
      ask turtle 0 [ fd 0.1 ]
      ask turtle 1 [ show towards turtle 0 ]
    end