I need to calculate how to reverse a heading within a NetLogo script e.g. a turtle has a heading of 0, I need to flip this to 180 so they travel in the opposite direction.
I've tried [360 - heading] with limited success.
For the record heading - 180 works, of course, but if you want to be more "turtle centric", you could ask your turtle to rt 180.
Ahh got it. I knew I was close
[heading - 180]
Related
I am a beginner in Netlogo and trying to setup an agent (criminal) to shoot another agent (victim) when in range. One of the problems I am facing is how to keep the bullets - when ‘hatched’ from the criminal – to keep moving in a straight line. In other words, how can I make a turtle maintain its current heading?
Below is what I have unsuccessfully tried.
Hatch-bullets 1 [ set size 0.1
set color white
set label ""
set benergy 10
fd 1
set current-heading heading
]
ask bullets [ ifelse any? humans-here with [pcolor = green]
[ hit-target die ]
[ set heading current-heading
fd 1
set benergy benergy - 0.5
set heading current-heading
ask humans-here [die]
if benergy = 0 [die]
]
I was hoping bullet-heading (variable) will store the current heading at the poing of hatching, so that it can be used later for the life duration of the bullet as it travels in a straight line (which unfortunately does not on both counts)
The other related problem is that, one bullet will be hatched per tick (or every second tick) and each bullet needs to keep track of its own heading. Bullets should be moving on a straight line rather than following the movement of the parent.
I have seen the projectile game, but the code is not helpful
I have my turtles go around randomly in my world. The problem is, I unwrapped it horizontally and vertically. I dont want to wrap it, but instead i just want it to bounce back if it reaches the border.Thank you very much.
Turtles have a variable heading indicating their direction in grades. If you don't want your turtle to keep going that way, you need to change that variable. So, I suggest that you use the variable headingand add 180 (forcing the turtle to turn around). Something like:
ask turtles [
if (xcor = minxcor or ycor = minycor or xcor = maxxcor or ycor = maxycor)
[
set heading heading + 180
]
]
where minxcor, minycor, maxxcor, maxycor are your bounds (depending on where your axis is situated: you can check that on the interface properties). So you're saying is: if you reach the end of the world, turn back. You can decide the grades, not necessarily 180. If you want to keep heading between 0 and 360 you can simply write set heading (heading + 180) mod 360.
If you have any doubt about what I've just written, feel free to ask!
In setup, I draw a bunch of turtles--as small circles--to display two curves defined by functions. A very simple way to do this is
ask patches with [pycor = (myfunction pxcor)] [sprout 1 [...]]
and that's what my code does at present. It's kind of wasteful, since every patch has to be consulted--in random order--for each curve, but it's simple and easy to read, and it only happens during setup.
However, there's a little bit of a pause as the curves are constructed. If I move the speed slider all the way to the right, the pause is not noticeable. If I wrap the curve display routines in no-display and display, the user doesn't see the curves being constructed, but the speed is unchanged, AFAICS. If I move the slider to the left, it takes a long time to construct the curves even with no-display; the user doesn't see the points being placed one by one, but nevertheless has to wait while twiddling her/his thumbs.
Is there a way to set the model speed programmatically (for normal, "headfull" use)? I don't want to tell users "Move the speed slider to the right, then press setup, then move it back to the center before pressing go.
If not, maybe I'll code the curves properly using loops, but I thought I'd ask. Seems like there would be a way to do this, but I haven't found anything in the dictionary or programming docs so far.
(edit: no-display, if it did help, isn't available in NetLogo Web, which I am targetting along with regular NetLogo.)
I don't believe there is. However, you are asking all patches, when you could simply ask the pxcor values. This should speed it up a lot - square root of the number of iterations if a square world. Something like:
to testme
clear-all
let counter min-pxcor
while [counter <= max-pxcor]
[ let fn-out (function counter)
if fn-out >= min-pycor and fn-out <= max-pycor
[ ask patch counter fn-out [ set pcolor red]
]
set counter counter + 1
]
end
to-report function [#invalue]
report #invalue ^ 2
end
I have a problem with following the nearest candida (breed) by another turtle - neutrophil (breed). Candidas can't move and neutrophil looks for the nearest candida in the area, face to him and go forward. Everything is fine when world is allowed to wrap. Then neutrophils go througt the "walls" and chase candida.
BUT the point is that world can't wrap.
So I created condidion with help another model, which bounce the neutrophil, when they reach one of the max coordinates. But now they keep bouncing from the walls and don't looking for another target.
I will be very thankful for help.
to lookfor ;;if meet candida in near area - chase, if not - continue looking for
ifelse any? candidas [ chase ] [ move ]
end
to chase ;;set the target on the nearest candida, face to them and move one step to him
set nearest-candidas min-one-of candidas [ distance myself ]
face nearest-candidas
bounce fd 1
end
to move
bounce
fd 2
end
to bounce
if abs pxcor = max-pxcor [ set heading (- heading) ] ;; bounce off top and bottom walls
if abs pycor = max-pycor [ set heading (180 - heading) ]
if abs pzcor = max-pzcor [ set heading (- heading) ]
set nearest-candidas max-one-of candidas [ distance myself ]
end
edit: Oh, you are in NetLogo 3D. That changes everything, unfortunately. Without being able to prevent the world from wrapping, there is no easy way of doing this. If you're feeling adventurous, you could try to find a way of ensuring that the nearest turtle is on the "right side of the wall" by triangulating with other turtles in the world. It wouldn't be easy though, but I could see that working. Good luck.
Outdated response:
I think the problem might be that in your bounce procedure, you have
set nearest-candidas max-one-of candidas [ distance myself ]
Using max-one-of sets the candidas to the one furthest AWAY from the neutrophil. You have it correctly in your chase procedure in which you set it to min-one-of (that is min one of, not max one of). Does changing that give you the behavior you were expecting?
I am trying to create a program in netlogo where there are blocks that come down the screen and when their y-coordinate reaches a certain value they reverse their direction and move in the opposite way.
So far I was able to make them move in one direction and then switch directions when they reach the critical y-coordinate value, but once they take one step in the reverse direction it glitches and they get stuck moving one step forward and one step backward.
I wanted to know if there was an else command in netlogo so I could specify that if the while command wasn't fulfilled it could reverse its direction and move without glitching.
Here is my code.
to maze
while [abs pycor < 16 ] [fd 1 wait .1]
bk 1 wait .1
end
There is no separate else keyword in NetLogo, but the ifelse command allows you to specify two blocks: one that is executed if the condition is true, and another (the "else" block) that is executed if the condition is false.
It seems, however, like you should rethink your general approach to the problem. Turtles in NetLogo always face in a particular direction, and you could take advantage of that: instead of having them "back up", you could have them turn around.
Also, it's generally ill-advised to try to do things in a while loop. If you want your turtles to repeat a behavior, a "forever button" is usually the way to go.
In the following example, you should call the go procedure from a forever button:
to setup
clear-all
ask patches with [ pycor = max-pycor - 1 ] [
sprout 1 [
set heading 180 ; head down
]
]
reset-ticks
end
to go
ask turtles [
if abs pycor = max-pycor [
rt 180 ; turn around!
]
fd 1
]
tick
end
This probably doesn't achieve exactly what you wanted, but there is a good chance that you can modify it to fit your needs.
Also note that this will work better using tick-based updates.