Rotating a vector denoting turtle position by an angle Netlogo

点点圈 提交于 2019-12-24 10:41:07

问题


I set/update each turtle's position as follows:

 set xcor xcor + item 0 vector
 set ycor ycor + item 0 vector

Therefore I add a vector to the current agent's coordinates.

PROBLEM: I wish to rotate the added vector by angle x. Thus the vector "vector" should be rotated by angle x. The angle should be taken from a Gaussian distribution with a specified deviation.

I am trying to achieve something similar to Couzin's model. http://www.csim.scu.edu.tw/~chiang/course/ComputerGameAdvance/Collective%20Memory%20and%20Spatial%20Sorting%20in%20Animal%20Groups.pdf

Thanks in advance!


回答1:


You seem to have two questions here; I'll address the one you used for the title. The matrix extension allows matrix multiplication, so you could just create a standard rotation matrix once you have the ange of rotation. But standard advice in NetLogo would be to use a more turtle-centric approach. Then you need to decide whether to use the NetLogo heading conventions (0 degrees for north, 90 degrees for east, etc.) If so you could do something like this:

to move [#dx #dy]
  let %dist 0
  ask patch 0 0 [set %dist distancexy #dx #dy]
  facexy (xcor + #dx) (ycor + dy)
  let %theta random-rotation
  rt %theta
  jump %dist
end

to-report random-rotation
  report (random-float 360) - 180
end

Here the random rotation is not Gaussian distributed because I was not sure what you meant. Perhaps a von Mises distribution? In any case, you should clarify and ask as a separate question.




回答2:


Somewhat simple, convert vector to angle, rotate (randomize), then convert back. For good coding style and such, break into modules.

to-report rotate [ #vector #angle ]
   let $dx first #vector
   let $dy last #vector
   let $magnitude sqrt ($dx * $dx + $dy * $dy)
   set #angle #angle + atan first #vector last #vector
   report (list $magnitude * sin #angle $magnitude * cos #angle)
end

to-report nudge-vector [ #vector #std-dev ]
   report rotate #vector random-normal 0 #std-dev
end

to move-inaccurately [ #vector #std-deviation ]
   set #vector nudge-vector #vector #std-deviation  
   setxy (xcor + first #vector) (ycor + last #vector)
end



回答3:


Just to emphasize Alan's point: Unless you have a good reason to use vectors, it's usually much easier and clearer to avoid them in NetLogo. If all you want to do is turn the turtle by a random amount drawn from a Gaussian distribution, you can just do:

right-turn random-normal 0 <std-dev>

where <std-dev> is your desired standard deviation. Then, you can tell the turtle to go forward by what would have been the magnitude of the vector: forward <distance>.

If you absolutely need to do a vector rotation, you can do so without the matrix extension fairly easily:

to-report rotate-vector [ vec angle ]
  let x first vec
  let y last vec
  let mag sqrt (x * x + y * y)
  let old-angle atan x y
  let new-angle angle + old-angle
  report (list (mag * sin new-angle) (mag * cos new-angle))
end

Remember that angles in NetLogo are flipped around 45º so that 0º is north and 90º is east; thus, sin and cos are flipped when dealing with angles.



来源:https://stackoverflow.com/questions/25304332/rotating-a-vector-denoting-turtle-position-by-an-angle-netlogo

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!