Octavian Bordeanu
Octavian Bordeanu

Reputation: 45

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

I am trying to make a turtle to move from its current node location to a node destination without having it to jump from one node to another, but instead move progressively from one node to another. I looked at Move Towards Target Example and Link-Walking Turtles Example models and tried to combine these in the code below which seems to make the turtle move from one node to another progressively, but only in a random manner.

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

What I would like is the turtle to walk progressively between nodes until it reaches its destination. For example, I tried the code below but the turtle ends up walking off the links.

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

How could I make the turtle move between the nodes of its selected path from current location to destination without moving straight from one node to another? For example, instead of jumping from node 1 to node 2 to node 3 ... to node n, I would like the turtle to forward 0.5 from node 1 to node 2 ... until it reaches the destination node.

Thank you.

Upvotes: 0

Views: 436

Answers (1)

Bryan Head
Bryan Head

Reputation: 12580

I believe the problem is that current-location is being updated to the next node before the turtle actually reaches it. Try this:

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

Thus, current-location is only changed when the turtle actually reaches it. Though, that makes me think that "current-location" is the wrong name for it. Also, with this code, the turtle will stop at the node before the final node. So consider making next-node a turtle variable. Then try this code:

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

Upvotes: 1

Related Questions