06 Programming thoughts to chase or avoid each other in StarLogo TNG
This guide will show you how to program thoughts to sense what is around them and to move towards or away from other thoughts.
Before you start this guide you should look at the following guides.
Getting started with StarLogo TNG.
Importing 3D models of thoughts into StarLogo TNG
Using SketchUp to design a room in StarLogo TNG
Learn how to program thoughts to move and wander around.
You may also want to..
Learn how to program thoughts to change when they collide.
– Set up a simulation with at least 2 different breeds and program the thoughts to wander around. You can do this by following the guides above.
– 3D models of thoughts can be downloaded at www.flunstellas.org
– A complete example of this activity can be downloaded here.
– Select at least two different breeds using 3D models of thoughts. In this example the breeds have been named ‘Creative Idea’ and ‘Angry Memory’.
– Create a ‘SetUp’ block similar to the one shown here.
– Create a ‘Forever’ block with wander behaviours like the one shown here.
Thoughts smelling Thoughts
In StarLogo TNG agents find out about their environment by smelling it. This is different to seeing because agents can smell in 360 degrees. You can program agents to smell for other agents over different distances and have agents to remember who they have just smelled.
Testing what agents smell
In the Blocks Window
– Open the Logic drawer and drag out an orange ‘If/Then’ block. Place it under the Movement blocks in one of your thoughts Forever block hook. The ‘If/Then’ block lets us program our thought to do something only IF something else is true.
– Drag out a blue ‘>’ (more than) block from the Logic drawer and connect it to the ‘test’ slot in the ‘If/Then’ block.
– Drag out a purple ‘Smell List With’ block out from the Other Agents drawer and place it in the Length List socket.
Combining these two blocks programs our thought to keep a list of other thoughts it can smell of a certain breed, around a certain radius of itself. The default is for this radius to be 10 units. This is quite a large radius, SpaceLand itself is 100 x 100 units. Change the smell radius to 5.
Now we want to tell the thought on which condition it should keep a list of the other thoughts it smells.
– Drag out a blue ‘=’ (equals) block from the logic drawer.
– Drag out a pale blue ‘Breed’ block from the traits drawer and place it in the left hand side of the ‘Equals’ block.
– At the top of the Blocks Factory; click on the blue ‘My Blocks’ arrow to open the My Blocks drawers.
– Open the blue drawer of whichever breed you want your thought to smell and place it in the right hand side of the ‘Equals’ block.
– Place the ‘Equals’ block in the Condition socket of the ‘Smell List With’ block.
– Place a pink ‘Number’ block from the Maths drawer in the empty right hand socket of the ‘>’ (more than) block.
Now we have a program that tells our thought to test if it can smell at least one (more than 0) thoughts that are of the breed we have chosen to sniff out for ‘Angry Memory’. It will only smell thoughts within 5 units of itself.
To test if this Smell code works we’re going to make our thought say something when it smells an Angry Memory close by.
– Drag out a blue ‘Say’ block and a pink ‘Text’ block from the Text drawer. Connect them together, write something for your thought to say in the ‘Text’ block and connect the ‘Say’ block to the Then socket of the ‘If/Then’ block.
If you run the program by clicking on the Setup and Flunstellas block at the bottom of the Spaceland window you should see the Creative Idea thoughts say “ANGRY!!!” when they are close to the Angry memories.
Experiment by increasing or decreasing the range at which the thoughts can smell each other.
As well as programming thoughts to say things when they smell each other, you can also have them change any of the traits in the Traits drawer. Here code has been inserted into the Then socket of the ‘If/Then’ block to make the Creative Idea thought grow if it smells an Angry Memory within 5 units of itself. Another ‘If/Then’ block has been placed within the Then socket of the 1st ‘If/Then’ block to place a limit on the growth of the Creative Idea thought.
This is all well and good, but although the code above is useful to show how we can program thoughts to smell each other, they aren’t changing their direction when they smell each other yet. To do this we will need to break out code up and use a different kind of IF block to make it more flexible.
– Take the ‘IF/Then’ block and the Movement blocks out of the ‘Forever’ block.
– Drag out an orange ‘If/Then/Else’ block from the Logic drawer and place it in the Creative Idea socket of the ‘Forever’ block.
– Place the ‘>’ (More than) block and all of it’s contents in the Test socket of the ‘If/Then/Else’ block.
– Place the wander movement blocks in the Else socket of the ‘If/Then/Else’ block.
This code will tell the Creative Idea thought to test if it can smell an Angry Memory within 5 units of itself, then do nothing if it does and wander if it dosen’t.
– Drag out a blue ‘Set Heading’ block from the movement drawer.
– Replace the ‘Set Heading’ Number block with a red ‘Towards’ block.
– Drag a purple ‘XCor of ID’ block from the Other Agents drawer and connect it to the XCor slot of the red ‘Towards’ block.
– Drag an orange ‘First in List’ block from the list drawer and connect it to the ‘XCor of ID’ block.
– Drag a purple ‘Smell List With’ block from the Other Agents drawer, change its radius to 5 and connect it to the ‘Fist in List’ block.
– Drag out an ‘Equals’ block and place a ‘Breed’ block in the left hand side and a ‘Breed:Angry Memory’ block in he right hand side. Connect it to the Condition socket in the ‘Smell List With’ block.
This tells our thought that if it can smell one or more Angry Memories within 5 units, then it should make a list of those Angry Memories and move towards the x coordinate of the 1st Angry Memory it has in its list.
– To duplicate this process for the Y Coordinate, copy the ‘XCor of ID’ block by clicking on it and pressing the ‘Ctrl & C’ keys as you would in other applications. Press ‘Ctrl & V’ to paste, connect to the ‘YCor of ID’ socket and change the block from ‘XCor of ID’ to ‘YCor of ID’ using by hovering the mouse by the word ‘of’ on that block and clicking on the arrow when it appears to reveal the drop down menu.
Finally add a ‘Forward’ block to tell your thought to move once it has pointed itself in the right direction.
Experiment by changing the speed that the thought moves forwards depending on what is around it.
Experiment by programming the traits of thoughts to change when they can smell other thoughts nearby.
How to make a thought avoid another thought
To make a thoughts avoid each other needs a small change to the Follow program create above.
The Avoid program does the same as the Follow program, it makes a list of thoughts around it if they are of a certain breed. It then calculates which direction the 1st thought in it’s list is and turns around 180 degrees to face the other way.
– Drag out 1 blue ‘+’ block from the ‘Maths’drawer.
– Drag the ‘Towards’ block away from the ‘Set Heading’ block and place it on the canvas. Connect the ‘+’ block to the ‘Set Heading’ block.
– Place the code blocks you just placed on the canvas (starting with ‘Towards’), into the 1st socket of the ‘+’ block.
– Place a ‘Number’ block into the 2nd socket of the ‘+’ block and change it to 180.
How to make a thought charge around erratically when it is close to another thought.
To make a thought move around erratically when it is close to another, simple adapt the code used above. With this program, instead of programming the thought to move towards or away from the thought it is smelling, it tells the thought to change direction completely every step.
Experiment with predator/prey behaviours.
– Program different thoughts to move towards or avoid one another.
– Program different thoughts to smell each other at different distances.
– Program different thoughts to move at differently speeds depending on which other thoughts are close by, e.g. different speeds or movement behaviours.
– Program thoughts to change traits when they are close to other throughts, e.g. Get bigger, become translucent, changing shape.
– Program thoughts to play sounds or display text depending on which other thoughts are close by.
– Program thoughts to Die when they collide with other thoughts.