乌龟从其当前位置移动到选定目的地,而不是直接从一个节点移动到另一个节点

时间:2017-05-21 02:50:31

标签: netlogo

我试图让乌龟从当前节点位置移动到节点目的地而不必从一个节点跳到另一个节点,而是逐渐从一个节点移动到另一个节点。我看了一下Move Towards Target Example和Link-Walking Turtles示例模型,并尝试将这些组合在下面的代码中,这似乎使乌龟逐渐从一个节点移动到另一个节点,但只是以随机的方式。

to walk
  let distance-from-current-location distance current-location
  ifelse 0.5 < distance from-current-location [
    fd 0.5 ]
  [
    let new-location one-of [ link-neighbors ] of current-location
    face new-location
    set current-location new-location
  ]
end

我想要的是乌龟在节点之间逐渐行走直到它到达目的地。例如,我尝试了下面的代码,但是乌龟最终走开了链接。

to walk
  if current-location != destination [
    let next-node item 1 [ nw:turtles-on-path-to [ destination ] of myself ] of current-location
    set current-location next-node
    ifelse distance current-location < 0.5 [
      move-to current-location ]
    [
      face current-location
      fd 0.5
    ]
end

如何让乌龟在所选路径的节点之间从当前位置移动到目的地,而不是直接从一个节点移动到另一个节点?例如,我不想从节点1跳转到节点2到节点3 ...再到节点n,我希望乌龟从节点1转发到节点2 ...直到它到达目标节点。

谢谢。

1 个答案:

答案 0 :(得分:1)

我认为问题是在{/>>龟真正到达它之前,current-location正在更新到下一个节点。试试这个:

to walk
  if current-location != destination [
    ifelse distance current-location < 0.5 [
      move-to current-location
      let next-node item 1 [ nw:turtles-on-path-to [ destination ] of myself ] of current-location
      set current-location next-node
    ] [
      face current-location
      fd 0.5
    ]
end

因此,current-location仅在乌龟实际到达时才会更改。但是,这让我觉得&#34; current-location&#34;是错的名字。此外,使用此代码,乌龟将在最终节点之前的节点处停止。所以考虑将next-node变为乌龟变量。然后尝试以下代码:

to walk
  if current-location != destination [
    ifelse distance next-node < 0.5 [
      ;; Close enough to the next node; make it my current location
      ;; and target the next node on the list.
      set current-location next-node
      move-to current-location
      set next-node item 1 [ nw:turtles-on-path-to [ destination ] of myself ] of current-location
    ] [
      ;; Not there yet; keep walking towards the next node.
      face next-node
      fd 0.5
    ]
end