Can turtles move the lateral direction (left or right) in NetLogo? - netlogo

My simulated turtles can move forward and backward.
In addition to it, I want to move turtles towards left or right directions with their heading direction keeping.
However, I don't know how codes acheves it.
Please tell me any codes to program it in NetLogo.

To make a turtle move left one square, have it do:
lt 90
fd 1
rt 90

Related

Net Logo Turtle disappears inside of Patch

In Net Logo is there a value for a turtles Z coordinate, or its height in the 3D View? I have white patches in my simulation, when my turtles go into those patches they are not visible until they emerge from the other side of the patch.
In the screen shot, you can see edges of turtles inside of my white patches, I want to display them on top of the patch.
How can I do this?
I found the issue, simply use
set zcor 2
will place the turtles on top of the patches as I had hoped.

what does `forward 1` mean in netlogo? how to specify the nearest patch of a turtle in netlogo?

when we ask a turtle to forward 2, does it mean:
on its heading direction, move to the second nearest patch
on its heading direction, move to the second nearest patch's center point
on its heading direction, move to the second nearest patch's center point which is (pxcor, pycor)
which one is correct? or are they the same answer?
How do we define the nearest patch this turtle is pointing to? Is the following understanding correct?
top patch: if heading is between -45(315) and 45 degree
right patch: if heading is between 45 and 135 degree
bottom patch: if heading is between 135 and 225 degree
left patch: if heading is between 225 and 315 degree
It means to move forward a distance of 1. The easiest way to see this is with the following code:
to setup
clear-all
create-turtles 1
[ setxy 0 0
set heading 45
forward 1
print xcor
print ycor
]
end
Try changing the heading and see what happens. Note that patches are exactly 1 unit wide and the centre of the patch is at integer values (so patch 0 0 extends from xcor -0.5 to xcor +0.5)
forward totally ignores patch boundaries and patch centers, so 1–3 are all incorrect. Patches don't enter into it; it's just trigonometry on the turtle's x and y coordinates. Specifically, forward 1 just means setxy (xcor + sin heading) (ycor + cos heading). The destination patch is simply whatever patch the new x and y coordinates happen to lie within. The destination may or may not be a patch center.
forward 1 might leave the turtle in the same patch (e.g. if the turtle is in the southwest corner of a patch facing northeast; the length of the diagonal is 1.414..., so forward 1 isn't enough to reach a new patch). Or, forward 1 might take the turtle to the nearest patch ahead, or to the second nearest patch ahead. (The latter can occur if the turtle just grazes the corner of a patch.)
A good model to play with to explore and understand all of these possibilities is Next Patch Example, in the Code Examples section of NetLogo's Models Library
As for the second part of your question, your definition is only correct if the turtle is initially standing on a patch center. For the general case, Next Patch Example answers that too. The next-patch procedure in that model is like your definition, but handles the general case.

How to control the positioning of a turtle on a patch?

In order for two way traffic to flow along a road that is the thickness of a single patch it is necessary for the position of each turtle to be to either the left or the right of the patch on which they are travelling. How can I control where a turtle resides on a patch while travelling along it so that there is enough space for two opposing flows of traffic on the same road?
You can use set xcor ... and set ycor ..., or setxy ... ..., or any other turtle motion command such as fd or bk, to position the turtle however you like. If the turtle's coordinates are both integers, it's on a patch center. If either or both aren't integers, then the turtle is in some other location in the patch.

Direct to move turtle to a specific spot

I have enclosed turtles in between walls and obstacles and want them to move to a specific goal spot. Each tick I forward turtle only a specific amount. How do I implement this in terms to variating the turtle heading?
Explaining more
In the above figure:
1. Consider all turtles inside the box at the start
2. You want turtles to reach goal spot above,(where turtles present currently in figure)
3. You have obstacles that is, walls in between which 1 opening that is the rectangle in the figure with the gap. Turtles have to pass this gap only.
How I tried
I make turtles face the goal spot using facexy and bounce back after colliding with wall but in doing so the turtles on extreme left and right of box keep colliding with wall since after each collision they again have the direction from facexy
Please help. Thanks in advance.
An easy fix (according to what I read about making the turtles bounce).
goal-xcor is the x-coordinate of goal.
goal-ycor is the y-coordinate
of goal.
random-number is a random number between 1 and 10(?).
`If COLLISION: `
if they're in the left make them facexy goal-xcor + random-number goal-ycor
if they're in the right make them facexy goal-xcor - random-number goal-ycor
If there is not any collision it means they're in the right way and they should keep going the goal direction.
It's important that you facexy goal-xcor goal-ycor every time you make them turn around. To be more clear: Pseudo-code:
1 setup-turtles-inside-box
loop:
2 ask all turtles to face goal.
3 ask all turtles to go forward.
4 if collision
5 if turtle-in-left
6 ask turtle to face goal-xcor + random-number goal-ycor
else
ask turtle to face goal-xcor - random-number goal-ycor
7 loop until all turtles are in goal.
Let us know of any doubt!
Here is another approach:
http://rur-ple.sourceforge.net/en/random.htm

Learn the direction of a moving agent

I created a turtle in NetLogo which is moving randomly and there are some obstacles. Is it possible to get its current direction? I want to get the turtle to walk back to the center when it sees an obstacle. I can calculate distance to the center, but since I don't know its direction I can't say forward or backwards, for example.
The turtle's current direction is given by the heading variable. You can both read and write to this variable in order to change the turtle's heading. You can also change it using facexy as N. Payette mentioned.
The facexy primitive will allow you to set your turtle's heading toward the origin:
http://ccl.northwestern.edu/netlogo/docs/dictionary.html#facexy
ask turtle <who>
[If (patch-ahead = obstacle)
[
facexy origin
fd distance origin
]
]
Here obstacle and origin are the respective patches.
Building on what Jose M Vidal and N. Payette have already said.