I am developing a model of simple car driving simulation in NetLogo. However, I am facing one major and minor problem. The procedures of the model are following:
There must be a car generated in the center of the world with "size 3". Car can move only in four directions "up", "down", "to_right" and "to_left". When "go" button is pressed car starts moving from center to the right. While doing so, buttons, which function at turtle level, "up", "down", "to_right" and "to_left" dictate directions of the car. With every press of these buttons "speed" of car is increased by the amount of "acceleration" and "tank" (gas) is decreased by "dec-tank". Car moves in a rectangular world ("box"). When car collides with the border or "walls" of the world it must: first, move to the opposite direction (to the direction of opposite "wall", so to speak "bounce off"), and second, "speed" must drop to its initial level (0.1). My major problem is that when car collides with the "walls" of a world, model keeps giving me error "OF expected input to be a turtle agentset or patch agentset or turtle or patch but got NOBODY instead". The minor problem is drop of speed does not work as well. I wonder, if there is anybody out there who had similar problems and could help me out with these problems? Thank you in advance!
I checked every question and answer that I could find here in stackoverflow related with the "bouncing" problems in NetLogo models, as well as outside stackoverflow by googling. I tried every answer as a solution, but no avail. As just a part of an experiment to my solution I wrote codes that set the color of car into a "yellow" when it reaches the "walls". And to my surprize, it seems to be working, but not the codes for the reversal of the car! Also, I tried to substitute both "max-pxcor" and "max-pycor" for "nobody" (in the block of code which supposed to define the reversal of the car), but it did not work either. Also I tried to increase the distance of "patch-ahead" and decrease the "size" of the car, but results are negative. I do admit that there might some flaws in my codes that I failed to notice, or may be some codes are not "sanitized" well enough to go through program as run. My main suspicion is that there could be a problem with the lines of codes which define the reversal of car, or in part of the codes that may seem totally unrelated to the reversal such as "heading" or "forward" or "size" of the car. Following are my codes.
globals [speed-limit tank]
turtles-own [speed dec-tank]
to setup
clear-all
set speed-limit 0.5
set tank 50
set-default-shape turtles "car"
draw-walls
create-turtles 1 [set color red
set size 3
set speed 0.1
set dec-tank 0.05
]
reset-ticks
end
to draw-walls
ask patches with [abs (pxcor) = max-pxcor][set pcolor blue]
ask patches with [abs (pycor) = max-pycor][set pcolor blue]
end
to go
ask turtles [
set heading 90
fd speed
if abs [pxcor] of patch-ahead speed = max-pxcor [set heading (- heading)]
if abs [pycor] of patch-ahead speed = max-pycor [set heading (180 - heading)]]
tick
end
to up ;at turtle level
set heading 0
fd speed
if abs [pxcor] of patch-ahead speed = max-pxcor [set heading (- heading)]
if abs [pycor] of patch-ahead speed = max-pycor [set heading (180 - heading)]
set speed speed + acceleration
if speed > speed-limit [set speed speed-limit - random-float speed ]
set tank tank - dec-tank
if tank <= 0 [ set speed 0]
end
to down ;at turtle level
...
The overall result of execution of my model is that car must move to opposite direction everytime it collides with "walls" and "speed" must drop to its initial level. But, I keep having error "OF expected input to be a turtle agentset or patch agentset or turtle or patch but got NOBODY instead." I found, while reading other answers here in stackoverflow, that "NOBODY" error appears when turtle moves beyond "walls" of the world. I wonder why it keeps happening in my case though.