Problems with defining heading pitch and roll in 3D-netlogo - netlogo

i'd like to simulate sun rays on a building. For the angle of the sun rays, i have the right elevation and azimuth angles. I was calculating the corresponding values for heading and pitch. But heading, pitch and roll seem to affect each other. When heading changes, also pitch changes. I have no idea how to calculate the corresponding values to my angles.
I was checking out the "Turtle and Observer Motion" code example in the netlogo library, but that was no help.
This is my code so far:
angles shall change every 60 ticks (position in the list changes)
lists contain converted heading and pitch values respectevly
globals [h ; heading
p ; pitch
r ; roll
i ; instead of tick
data_az ; a list with the azimuth angles
data_el ; a list with the elevation angles
hour ; is the position of the angles in the list, stands for the hour
i_test
i_round
]
breed [rays ray]
to setup
ca
file-open "heading.txt"
set data_az file-read
file-close
file-open "pitch.txt"
set data_el file-read
file-close
check-date
set r 0
set i 1
set hour hour - 1
set-default-shape rays "line half"
reset-ticks
end
to go
tick
if i = 1440 ; after 24x60 i stop
[
stop
]
set i_test i / 60
set i_round round i_test
if i_test = i_round or i = 1
[
set hour hour + 1
set h item hour data_az
set p item hour data_el
]
if h != 0
[
create-sunshine
run-sunshine
]
set i i + 1
close
end
to create-sunshine
create-rays 5 [ setxyz random-pxcor max-pycor random-pzcor
set color yellow
set heading h
set pitch p
set roll r
set size 2
]
end
to run-sunshine
ask rays [fd 1]
ask rays [if pxcor = 0 [die]] ;rays stop at the edges
ask rays [if pxcor = 199 [die]]
ask rays [if pycor = 0 [die]]
ask rays [if pycor = 49 [die]]
ask rays [if pzcor = 0 [die]]
ask rays [if pzcor = 199 [die]]
end
to close
file-close-all
end
to check-date
if date = "march-equinox" [set hour 0]
if date = "summer-solstice" [set hour 24]
if date = "september-equinox" [set hour 48]
if date = "winter-solstice" [set hour 72]
end
looking forward to your recommendations!
thanks in advance
Jana

From our discussion
Idea 1
Have one of the turtles facexyz a point that gives you the correct elevation and azimuth and copying those values for the rest of the turtles. Something like
Ask one-of turtles
[
Facexyz 1000 45663 4663; or something
Ask other turtles[
Set heading [heading] of myself
Set pitch [pitch] of myself.
]
]
Idea 2
If roll is set to 0 pitch should equal elevation and heading should equal azimuth.

So it worked with idea 1. Thanks!
This is the code now:
globals [x
y
z
data_x
data_y
data_z
hour
i
i_test
i_round
leader
]
breed [rays ray]
to setup
ca
file-open "x.txt" ; three files are loaded with x y and z coordinates respectevely.
set data_x file-read
file-close
file-open "y.txt"
set data_y file-read
file-close
file-open "z.txt"
set data_z file-read
file-close
set hour hour - 1
set i 1
set-default-shape rays "line half"
reset-ticks
end
to go
tick
if i = 1440 [
stop]
set i_test i / 60
set i_round round i_test
if i_test = i_round or i = 1 [
set hour hour + 1
set x item hour data_x
set y item hour data_y
set z item hour data_z]
if i_test = i_round or i = 1 [
if y != 0 [
create-leader
create-follower
run-sunshine]]
set i i + 1
close
end
to create-leader
crt 1 [setxyz x y z
facexyz 45 1 45
set leader who
]
end
to create-follower
crt 5 [setxyz random-xcor 70 random-zcor
set heading [heading] of turtle leader
set pitch [pitch] of turtle leader
]
end
to run-sunshine
ask turtles [fd 1
pendown]
end
to close
file-close-all
end

Related

Turtles hatch when crossing a line

I want my turtles to hatch (= make one more turtle) when they cross a specific line. I have tried the command ifelse?, and I can get it to work on a simple model, when my turtles randomly wanders: If they move to a patch on the left side (xcor < 0) they die, if they make a move to a patch with xcor > 0 they hatch 1.
But I want the proces to be linked to witch patch they come from. If they stand on a patch with xcor < 0 and moves to another patch with xcor < 0 they shall die - but if they change xcor from negative to positive - they should multiply (= hatch 1).
My problem is: Is it possible to write a code-string that "remembers" the turtles position one tick before and use it to either let the turtle die og multiply?
{
to setup
clear-all
create-turtles 20
ask turtles [set size 2
set heading random 45 ;; turtle is now facing northeast
setxy random-xcor random-ycor
set color white
set shape "person"]
end
to go
ask turtles
[ rt random 360 ; turns in a random direction
fd 4 ;; all turtles move forward one step
rt random 180 ;; ...and turn a random amount
fd 4
lt random 180
]
ask turtles
[ifelse pxcor > 0
[hatch random 2]
[die]]
end }
You can use a turtle variable to give each turtle a memory.
turtles-own
[
old-xcor
]
Just before the turtle moves, assign ‘xcor‘ to that variable.
To move-turtle
Set old-xcor xcor
< Your movement code >
if old-xcor < 0
[ Ifelse xcor < 0
[ Die ]
[ Hatch 1 ]
]
End

Netlogo ERROR - DISTANCE expected input to be an agent but got the number 0 instead

I am trying to create home ranges for 3 types of hosts for a model that aims to simulate the movement of ticks across a landscape based on the presence of three host-types.
Using some code that was described in a previous post I created the following code. When I run the model I eventually get the following error message:
DISTANCE expected input to be an agent but got the number 0 instead.
Any help with the issue is greatly appreciated.
to-go
if ((week-id = 13) or (week-id = 25) or (week-id = 33))
[ask patches
[sprout-mice 2
[set color gray
set shape "mouse side"
set size 0.5
setxy random-xcor random-ycor]
]
]
if week-id = 17[
let total-deer count deer
ask n-of (total-deer / 2) patches
[sprout-deer 1
[set color brown
set shape "deer"
set size 1
setxy random-xcor random-ycor]
]
]
if week-id = 17[
let total-raccoons count raccoons
ask n-of (total-raccoons) patches
[sprout-raccoons 1
[set color black
set shape "wolf 2"
set size 0.5
setxy random-xcor random-ycor]
]
]
mice-mortality
;print (count mice)
if (week-id = 40)
[deer-mortality]
;print(count deer)
if (week-id = 45)
[raccoon-mortality]
print (count raccoons)
ask deer
[deer-move]
ask raccoons
[raccoons-move]
ask mice
[mice-move]
the submodels:
to deer-move
right random 50
left random 50
forward 1
end
to raccoons-move
ifelse distance home-patch > 10
[face home-patch]
[right random 90
left random 90]
forward 1
end
to mice-move
ifelse distance home-patch > 2
[face home-patch]
[right random 90
left random 90]
forward 1
end

Netlogo: how to round heading into 90 180 270 360 only

I am currently building a model for people walking on a certain path (road patches, color = white). The agents' movement will only be limited on the white patch and when they encounter black patch they will rotate 180 deg.
The problem I found was when I tried to set the heading of the agents. I want the heading to walk to the nearest red patch (destination), but when I use:
1
set heading towards min-one-of patches with [pcolor = red ] [ distance myself ] <
the agents wont move from their patch. It turned out that the "heading" generated by the above code has the value of -360 to 360 with decimals in it. The walking procedure that I have built only allows heading with multiplies of 90 (-360, -180 ... 90, 180, 270, 360). This is the full code for my walking procedure:
2
globals [ flagl flagr ]
ask agents [
;reset flag
set flagr 0
set flagl 0
; check and note if there is a path on the left or the right
ask patch-left-and-ahead 90 1 [if (pcolor = white ) [set flagl 1]]
ask patch-right-and-ahead 90 1 [if (pcolor = white ) [set flagr 1]
]
;in T-junction, decide to turn left or right (random)
if((flagl = 1) and (flagr = 1))
[
ifelse random 100 > 50
[set heading heading - 90]
[set heading heading + 90]
]
;if it's only applicable to turn right, then turn right
if((flagl = 0) and (flagr = 1)) [set heading heading + 90]
;if it's only applicable to turn left, then turn right
if((flagl = 1) and (flagr = 0)) [set heading heading - 90]
;return if there's no mor path
if [pcolor] of patch-at-heading-and-distance heading 1 = black [rt 180]
;agent movement
**;face min-one-of patches with [pcolor = red ] [ distance myself ]**
fd 1
;stopping procedure
if [pcolor] of patch-here = red [fd 0]
]]
I tried to round the heading generated by #1 like this:
let direction heading towards min-one-of patches with [pcolor = red ] [ distance myself ]
if direction != mod 90
set direction 90 * round(arah/ 90)
However it gives error notice since "let" command cannot take that kind of input.
This is the screenshot of the map I have built:
Map
Any help to solve this problem is hugely appreciated. Thank you!
This might get you started- with minimal checking it seems to do the trick, but you will probably want to check for exceptions.
The cardinal-4 reporter takes whatever input is given and checks if the modulo is equal to 0. If not, it will check if modulo is less than/equal to 45- if so, subtracts that value from the direction (dir). If modulo is greater than 45, the subtract the modulo from the direction and add 90.
The check procedure just prints the reporter output for the values listed.
to setup
ca
reset-ticks
end
to-report cardinal-4 [ heading-input ]
let dir heading-input
let remain dir mod 90
if remain != 0 [
ifelse remain <= 45 [
set dir dir - remain
]
[
set dir dir - remain + 90
]
]
report dir
end
Edit: improved check procedure:
to check
let testVals [ -361 -359 -271 -269 -181 -179 -91 -89 -1 0 1 89 91 179 181 269 271 359 360 361 ]
print map [ i -> ( word i " becomes " cardinal-4 i "\n" ) ] testVals
end

NetLogo: creation of lattice/grid resources world without using turtles?

I would like to create a "gridded" world of resources, in specific distance from the central patch and keep distances equal between these patches. Due to calculation demand, I prefer not to use turtles to create this patchy world. I expect to create something like this:
Equally, I would like to define distance between patches as a slider tool. I was wandering to use turtle lattice walk and then turn patches to different color, but is there any way how to do that without turtles ? Thanks for any suggestions !
My not totally working exemple:
to setup
clear-all
ask patches [set pcolor green]
foreach [5 10 15] [
repeat 9 [
make-red-patch ?
]
]
reset-ticks
end
to make-red-patch [dist]
crt 1 [
fd dist
rt 90
while [pcolor = red] [
bk dist
rt 90
fd 2 * dist
]
set pcolor red
die
]
end
I am not exactly sure what you need, first you mentioned you don't want to use turtles and in your own answer you have problem with the patch without a turtle.
There might be another way to approach this question:
to setup
clear-all
ask patches with [pxcor mod Grid = 0 and pycor mod Grid = 0] [set pcolor red]
end
And these are examples with different Grid size:
After more detailed search I found my answer here: http://netlogo-users.18673.x6.nabble.com/Setting-up-agents-in-a-grid-formation-td4864083.html
They consider to distribute turtles, not patches and then attribute patches turtles' qualities.
Here is the code:
to setup
clear-all
create-turtles 1
[ let $n 0 ; actual number of turtles on this patch
let $s 0 ; current number of turtles on a side of square pattern - 1
set heading 0
ask patch-here [set pcolor red]
repeat 16 ; number of needed turtles
[ hatch 1 jump Grid ; make one turtle and move
set $n $n + 1 ; increment count of curent side
ask patch-here [set pcolor red]
if $n > $s ; if side finished...
[
right 90 set $n 0 ; turn and reset count
ask patch-here [set pcolor red]
; if headed up or down, increment side count
if heading mod 180 = 0 [ set $s $s + 1
]
]
]
die
]
end
which produce:
I still don't know how to deal with 1 patch without turtle (bottom right corner), but this exemple helped me a lot ! :)

What would i need for this code to make turtles spawn slower, but not have to hold down the mouse to kill a turtle

Im playing whack a mole, and here is my code thus far
globals [
score
three-sec-spawn
]
turtles-own [
ttl]
extensions [bitmap]
To setup
ca
reset-ticks
import-drawing "whackamole.jpg"
import-pcolors-rgb "whackamole.jpg"
set-default-shape turtles "turtle"
ask turtles [
set size 7
set color red
set ttl 100]
reset-ticks
ask patch 18 2 [set plabel
"DIRECTIONS : To kill a bug, click on it. If you misclick, the bug stays there."]
ask patch 18 -1 [set plabel
"If there are 5 bugs on the screen, or if you dont kill it fast enough, you lose"]
set score 0
end
To play
if ticks > 65000 - (difficulty * 10000) [user-message "GAMEOVER" stop]
if (count turtles with [color = red]) = 10 [user-message "GAMEOVER" stop]
ask turtles [
set size 7
set color red]
ask patch 18 2 [set plabel ""]
ask patch 18 -1 [set plabel ""]
ask one-of patches with [pcolor = [42 13 9]] [sprout 1]
ask turtles [
set size 7
set color red
set ttl ttl - .5]
wait .5
if mouse-down? [
ask turtles with [distancexy mouse-xcor mouse-ycor < 1.5]
[set score score + 1
die]
reset-ticks
]
tick
end
because of the wait .5, turtles spawn too fast. But if i make the wait time any longer, id have to hold down the mouse button in order to "whack" the turtle
In your case I would not use the "wait." primitive at all, because it creates a gap of time where no actions (for example mouse clicks) can be tracked.
Instead I would use a time counter to delay the spawning and to create turtles on every x time units. Generally I would use the tick counter itself. For example, if you would like to spawn 1 turtle on every 100th tick:
if (ticks mod 100 = 0) [ ask one-of-patches with ... [sprout 1]]
In your example, the tick counter gets reset on mouse clicks. In that case you should define an additional time-counter for the spawning which does not get reset at anytime and gets increased by 1 on every timestep (analougous to the ticks but without resetting it back to 0 on mouse clicks):
if (your-counter mod 100 = 0) [ ask one-of-patches with ... [sprout 1]]
I hope this helps..?!?