Tuesday 19 May 2009

Netlogo to 3D Max: Code

Based on the pseudocode for the Netlogo to 3DMax implementation, I have now decided to post a short tutorial on exporting the Netlogo traffic simulation to 3DMax. This tutorial will mainly cover the output of the turtles and visualising it in 3DMax.

Netlogo (traffic grid simulation):

For the turtles, I output during runtime, therefore, open the file at the first tick, keep writing, and close the file at the last tick, depending on how many ticks you would want to output, in my case 500.

if ticks = 0 [file-open "myfile.txt"]
ask turtles [
file-write xcor file-type " " file-show ycor
]
if ticks >= 500 [file-close]

This will output in the form:

-12 (turtle: 2) 11
-17 (turtle: 0) 5
-4 (turtle: 1) -8

I didn't find a simple way just to output only the turtle id number, so I manually delete the brackets and text using find and replace, giving me an output as seen below:

-12 2 11
-17 0 5
-4 1 -8

Now our next step is to import it into 3DMax. Once the output file has been stored, the script below can be run, and on clicking the 'Make' button, locate the output file, and it will create the animation.

The code works by importing the Netlogo output file into an array. It can be found here.

I have commented the code in order to explain what is happening at each step.

Running this script will result in the animation shown in my previous post. To get the road patches, and traffic lights in 3DMax, the code can be extended further to store the patches and traffic light states in arrays for each frame, once imported from the Netlogo output.

Update: As pointed out by a reader, there is actually a simple way to output only the turtle id number using the command:

ask turtles [print who]

Therefore, the Netlogo output above can be obtained by using the following command

ask turtles 
[
file-type word xcor " " file-type word who " " file-print ycor
]

ShareThis