Beginners Interactive NetLogo Dictionary (BIND)
Farsi / Persian
NetLogo Models Library:
This code example shows how to have turtles look ahead before they move. By looking ahead, a turtle can determine what is in front of it and take a particular action. Looking ahead is most appropriate in situations where the turtle is not supposed to go "on top of" certain agents. This can be extremely useful for something like barriers or walls, which the turtle shouldn't move through.
In this example, four turtles are placed in a world with a checkerboard-like pattern of blue barriers on which they are not allowed to step. Before the turtles step forward they always check ahead to see if they are about to move into a wall using the patch-ahead primitives.
patch-ahead n reports the patch that the turtle would be on if it were to move forward
n steps (e.g., by executing
fd n). In this example, we want turtles to always move one step at a time, so we always use
In addition to simulating barriers, this technique can also be used to test whether a turtle is near something important, such as food or another turtle.
Note that the code in this model only prevents the turtle from landing on a blue patch. It doesn't prevent the turtle from:
The turtles may cross a blue patch because in NetLogo the
fd 1 command is equivalent to
jump 1 -- in other words, the turtle simply disappears from its old location and reappears in its new location, rather than moving continuously. You could add more code to the model to ensure that the turtle's path never crossed a blue patch, but the math for that gets a bit complicated.
The turtles may visually touch a blue patch because to NetLogo, the turtles are really just their center points. We represent the turtle visually with a certain size and shape, but to NetLogo, the turtle is a point with no extent, and that point is always on one and only one patch. Again, you could add "collision detection" code to the model to ensure that the center point never came within a certain distance of any patch, but the math for that would get complicated. This kind of "collision detection" is not built in to NetLogo.
To see this in use in real models see Ants, Slime, Gas Chromatography, Simple Kinetics, GasLab and Connected Chemistry models, various games, and Wealth Distribution.