Erik
Erik

Reputation: 331

NetLogo - Getting turtle position from mathematically generated turtle id

I'm trying to make groups of four turtles rotate randomly around the center of the group, however I'm having trouble calculating said center. Group ids are sequential (eg turtles with [who] 0-3 are group 1, 4-7 are group 2 etc). Currently, my attempt at calculating the group center is as follows:

let i 0
while [i < group_num] [ ;;iterates over each group

    ;;setup some information about the group
    let j 0
    let cmx 0
    let cmy 0
    let cmz 0
    while [j < 4] [
      set cmx (cmx + (turtles ((i * 4) + j) xcor)) ;this doesn't work
      ;set cmy (cmx + (turtles with ((who = ((i * 4) + j)) ycor ))) ;nor does this
      ;set cmz (cmx + (turtles with ((who = ((i * 4) + j)) zcor )))
    ]
    set cmx (cmx / 4)
    set cmy (cmy / 4)
    set cmz (cmz / 4) 

    ;; rest of the program
]

Both the cmx and cmy line tell me that there's a missing closing parenthesis, but all parenthesis have a partner and the program highlights them as such. Any advice on how to call the position of a particular turtle?

Thanks in advance!

Upvotes: 0

Views: 89

Answers (1)

Luke C
Luke C

Reputation: 10301

This is for NetLogo3D, correct? Maybe this base can get you on the right track. Some details in comments. With this setup:

turtles-own [ group-id static-center my-radius]

to setup
  ca
  let i 1
  crt 16 [ 
    set group-id i
    if count turtles with [ group-id = i ] > 3 [
      set i i + 1
    ]
    set color 25 + ( 20 * group-id )
    setxyz random-xcor / 2 random-ycor / 2 random-zcor / 2
    pd
  ]
  ask turtles [ 
    ; Identify the starting center of the group, as well
    ; as each turtles distance to that point
    set static-center group-center 
    set my-radius distancexyz item 0 group-center item 1 group-center item 2 group-center

    ; Face the center point, then tilt up to be tangential
    ; to the circle the turtle should transcribe
    facexyz item 0 static-center item 1 static-center item 2 static-center
    tilt-up 90
    ask patch item 0 group-center item 1 group-center item 2 group-center [ 
      set pcolor [color] of myself 
    ]
  ]
  reset-ticks
end

That makes use of a group-center reporter that returns a list of the mean xyz coordinates of the group:

to-report group-center
  let my-group turtles with [ group-id = [ group-id ] of myself ]
  let group-x mean [xcor] of my-group
  let group-y mean [ycor] of my-group  
  let group-z mean [zcor] of my-group
  report ( list group-x group-y group-z )
end

And this is just a simple go for the turtles to tilt-down according to their radius.

to go
  ask turtles [
    tilt-down 180 / ( pi * my-radius )
    fd 1
  ]
  tick
end

Upvotes: 1

Related Questions