I wrote a code to fuse my turtles in a special way: whenever 2 turtles are near (distance<1) they must stick together and keep moving randomly together but they must keep their relative angle to each other. For example, if 2 turtles form a T shape at the moment of fusion they must meet the T shape during random movement. But my code doesn’t work and turtles are still moving randomly and don’t fuse. Would you please help?
This is my code:
to setup
clear-all
create-turtles 50 [
set shape one-of [“mtrod” “mtrod1” “mtrod2” “mtrod3”]
set color blue
set size 2.5
setxy random-xcor random-ycor
]
reset-ticks
end
to go
ask turtles [
move_mt_particles
fusion
]
tick
end
to move_mt_particles
right random 360
forward 1
end
to fusion
let nearby-turtles other turtles with [distance myself < 1]
if any? nearby-turtles [
ask nearby-turtles [
let angle-to-me towards myself
let offset-distance 0.0001
let new-xcor [xcor] of myself + (offset-distance * cos angle-to-me)
let new-ycor [ycor] of myself + (offset-distance * sin angle-to-me)
setxy new-xcor new-ycor
set color red
]
]
if color != red [
set color red
]
Added two new attributes to the turtles. Independence attribute indicates if the turtle is independent. After fusing, one of the turtles loses its independence. Second attribute is paired? which shows if the turtle is already paired. This is to prevent the independent (i.e. leading turtle) from forming more than one links with other turtles
Secondly, I used the ‘tie’ function to link two nearby turtles.
I have replaced the shapes with ‘lines’ for my simulation. The run shows fusing of turtles. However, you will have to further verify the process.
Hope this helps. The updated code is provided below:
turtles-own [independence paired?]
to setup
clear-all
create-turtles 50 [
set shape “line”
set color blue
set size 2.5
setxy random-xcor random-ycor
set independence 1
set paired? false
]
reset-ticks
end
to go
ask turtles with [independence = 1] [
move_mt_particles
if paired? = false
[
fusion
]
]
tick
end
to move_mt_particles
right random 360
forward 1
end
to fusion
let nearby-turtles other turtles with [distance myself < 1]
if any? nearby-turtles with [independence = 1 and paired? = false] [
ask nearby-turtles [
let angle-to-me towards myself
let offset-distance 0.0001
let new-xcor [xcor] of myself + (offset-distance * cos angle-to-me)
let new-ycor [ycor] of myself + (offset-distance * sin angle-to-me)
setxy new-xcor new-ycor
set color red
set independence 0 ;the turtle loses its independence
create-link-from myself [tie] ;the turtles form a tie
ask myself [set paired? true]
]
]
Glad you found the code helpful. I would suggest that you post your questions here in the forum so that either me or somebody else will be able to provide answers or suggestions to your queries.