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

Turtle moving from its current location to selected destination without moving straight from one node to another

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

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转发0.5到节点2 ...直到它到达目标节点。

谢谢。

我认为问题是 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 仅在乌龟实际到达时才会更改。不过,这让我觉得“current-location”是错误的名称。此外,使用此代码,乌龟将在最终节点 之前的节点 处停止。所以考虑让 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