I am simulating a mouse trapped in a circular pond with a cat roaming the outside of the pond. The mouse always moves 180 degrees away from the cat, and the cat is SUPPOSED to get as close to the mouse as it can from the outside.
However, right now the cat just moves around clockwise until the mouse is out of the pond and has reached the wall. THEN it changes direction going counter-clockwise.
I want the code to basically compare the distance before and after the cat moves; if the distance to the mouse is more, than it needs to turn around, if the distance to the mouse is less, that means it's getting closer and should keep going that way.
My code is below:
to go
mouse-movement
cat-movement
end
to cat-movement
ask turtle 0
[
pen-down
set cprevious-patch-distance distance turtle 1
move-along-circle radius
set ccurrent-patch-distance distance turtle 1
]
end
to move-along-circle [r]
ifelse
ccurrent-patch-distance < cprevious-patch-distance
[
fd (pi * r / 180) * (speed / 50)
rt speed / 50
]
[
fd (pi * r / 180) * (speed / 50) * -1
rt speed / 50 * -1
]
end
Related
I am simulating a random walk within bounded angels, and I want to calculate the mean squared displacement of the turtles in 2D in my model. The turtles all start from the center. I am not sure how to update the xy coordinates for the turtles after they hit the boundaries. I am asking the turtles to bounce back when they hit the wall and save their new xcor and ycor. Here's my code:
to go
ask turtles
[; head in a random direction in range (-theta, +theta)
ifelse theta = 0
[set heading heading + 0 ]
; choose a normally distributed random angel in range (-theta, +theta)
[set heading heading + random-normal 0 (theta)]
fd step-size
set xc xc + (step-size * dx)
set yc yc + (step-size * dy)
; if your next patch is blocked:
ifelse not can-move? 1
[ set heading heading + 180
fd 1 ]
;otherwise:
[rt random-float random-normal 0 (theta)]
set xcor xc
set ycor yc
set dist sqrt (xc * xc + yc * yc)]
]
I am getting the error which says:
"Cannot move turtle beyond the worlds edge.
error while turtle 423 running SET
called by procedure GO
called by Button 'go'". Any ideas why?
You have the grid-wrap disabled and your xcor/ycor of your agents are going past the max-pxcor/min-pxcor and max-pycor/min-pycor
I have an ellipse shape. (created having vertical direction)
I want to place several such shapes on the circle contour
They should be placed having same distance one from another
;x,y - circle center coordinates
;r - radius
to draw-ellipses [x y r elNum ]
if elNum > 0 [
let theta 0
let delta 360 / elNum
loop[
if elNum = 0 [ stop ]
crt 1 [
setxy (x + r * cos theta) (y + r * sin theta)
set shape "ellipse"
set heading 90 + theta
set size 7
]
set theta (theta + delta)
set elNum (elNum - 1)
]
]
end
The ellipse shapes ate placed correctly but their direction is wrong. I want the ellipses to have radial direction. I.e to be the continues of the radius to the point, where shape is placed. but directions are not good.
Only for 0 and 180 degrees the directions are ok.
Some advices what am I missing?
Do I understand correctly that you want your turtles to face away from the radius? If that's true, try replacing your heading line with:
set heading atan xcor ycor
to do it in a way that is consistent with your angle math. Alternatively, you can use facexy, and replace the heading line instead with something like:
facexy x y
rt 180
I want move to a specific diagonal direction in netlogo.
I can move the turtle with the fd command but I don't know what condition (if-else) put in the code
to go
ask turtle 2
[ move ]
tick
end
to move
if( (pxcor = -15) and (pycor = -15 ))
[fd 5 ]
end
Your question is still unclear. You say you "want turtle with id 2 ,first move to right and then move to up". But how far in each direction? I'll assume 5, based on your question. Here is what seems to be the best overall match to your question.
to go
ask turtle 2 [move1]
end
to move1
set heading 45
fd 5
end
But if you really want to move right and then move up
to move2
set heading 90
fd (5 / sqrt 2)
set heading 0
fd (5 / sqrt 2)
end
I wish to write to where I have a cone-of-vision cone angle for the turtle.
And I rotate the turtle through the cone-of-vision.
Therefore, first it's heading the start of cone-of-vision and with a increment of 0.05 degree it changes till it reaches the end of cone-of-vision
let max-head heading + (cone-angle / 2)
set heading subtract-headings heading (zero-dash / 2)
while[(subtract-headings heading max-head ) < 0]
[
set heading heading + .05
;wait 0.1
]
The above code I wrote isnt correct. But I can't find the mistake.
I think part of your difficulty is using subtract-headings. This finds the size of the shortest angle between two headings and that's not what you want for a cone of vision (which could potentially be wide). Since your code rotates clockwise, I have assumed you want to start at the most anti-clockwise point and rotate. The only difficulty then is if the heading goes through 360, so you need to separate the cone sweeping counter and the actual heading, and use mod arithmetic to convert from the counter.
globals
[ cone-angle]
to setup
clear-all
create-turtles 1
set cone-angle 25
end
to go
ask turtles
[ let max-head heading + (cone-angle / 2)
let fake-head heading - (cone-angle / 2)
while [fake-head < max-head]
[ print fake-head
set fake-head fake-head + 1
set heading fake-head mod 360
wait 0.1
]
]
end
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!
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.
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.
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 $dx $dy
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