Tuesday, June 27, 2006

Turtle Speed: Faster simulations with OBJ shapes

In a recent post I demonstrated how to set the visible color of OBJ shapes. Today, I will highlight another benefit of OBJ shapes over their MD3 counterparts: they are often faster.

Recall that you can always monitor the rendered FPS and the VMPS by pressing the "H" key twice. If a model with many agents that are represented with MD3 shapes seems to be running slowly, even with the Speed Slider set to Max Speed, you may achieve better frame rates by switching to OBJ shapes.

Consider the sample project Epidemic (epidemic.sltng), found in the Projects folder in the StarLogo TNG installation directory. Epidemic uses simple OBJ spheres to represent agents that may become infected as they move around and interact with other agents. The use of OBJ shapes in Epidemic is primarily to be able to use color to differentiate between sick and healthy agents. However, you may find that Epidemic runs much more quickly thanks to the use of OBJ shapes instead of MD3s, especially on older computers.

To see the effect, try changing the shape of the agents from the sphere to an MD3, such as Mario or one of the animals. You will no longer be able to visualize sick and healthy agents with color, and you may also notice that the model runs much more slowly when many agents appear on the screen at once.

If we ever find a way to improve the performance of MD3s significantly, blog readers will be the first to know! In the meantime, if you find that your model is running sluggishly, consider changing some of the breeds to OBJ shapes.

Labels: ,

Friday, June 02, 2006

Change Agent: The amazing technicolor OBJ shapes

While StarLogo TNG stays true to its roots by providing several turtle shapes, including one that is the default shape for agents when TNG first loads, it also provides dozens of other shapes to help the user differentiate agents in a visually appealing way.

StarLogo TNG supports two different formats for 3D shapes. One is the MD3 format used by the Quake III engine. The MD3 shapes provide vividly rendered creatures to enhance your TNG models, featuring multicolored skins such as the clown fish's orange and white stripes. However, you cannot (yet) change their color to suit your model. The other format is called OBJ, first developed by Silicon Graphics, for which TNG does support changing colors.

Granted, you can always set an agent's internal pen color, but StarLogo TNG's MD3 shapes do not express these colors on their skin, fur, clothing, or even turtle shell. That is where TNG's OBJ shapes really shine. Though they only appear as one color at a time, their color changes to match the color of their pen.

Unfortunately, we do not (yet) provide an easy way to tell from the Shapes category whether a particular shape is MD3 or OBJ. One trick to figure it out is that if the image on the shape block has more than one color, then it is probably an MD3 model, and if it is all a solid color, then it is probably an OBJ model. If you want to be absolutely certain, you can test a shape by setting your agents to be that shape and then calling set color. If the agent visibly changes color, then it is an OBJ shape. In the example below, the OBJ turtle shape turns bright magenta:


StarLogo TNG Preview 2 includes several OBJ shapes. All of the letter and number shapes, as well as all of the geometric shapes (the cone, cube, cylinder, diamond, pyramid, sphere, and tetrahedron) are OBJs. We also have several creatures that come in both MD3 and OBJ format, including the clown fish and one of the turtle shapes, and future releases will include more still.

So, if your model relies on color to convey information in Spaceland, the OBJ models are there to help.

Labels: ,

Tuesday, May 16, 2006

Turtle Speed: Focus on Spaceland

In a recent post, I explained the difference between FPS and VMPS in StarLogo as well as how to monitor them by pressing the "H" key. I also showed you how to control the VMPS using the Speed Slider.

Unfortunately, there is no way to control the FPS directly. The number of frames per second that StarLogo can show you depends a lot on your computer's hardware, especially its graphics card and processor. Nevertheless, today's tip explains one way to boost Spaceland's FPS substantially.

Many 3D games take up the whole screen while they are running. They can run as fast as possible without worrying about whether it will slow down your ability to surf the web or compose an e-mail message, whereas StarLogo does not have that luxury. One of our design goals is that running a model in Spaceland, even at max speed, should not interfere with your ability to modify the blocks code or work with other pieces of software. Spaceland cannot greedily hog your computer's processor if you want to multitask.

In order to meet this goal, we designed Spaceland to automatically throttle the FPS when it is not the focused window. For example, when you push a Forever button in the Runtime workspace, Spaceland will not have focus, as shown for Mac OS X in the screenshot below, where you'll notice that the title and the three buttons on the left are grayed out:


Even at max speed, when Spaceland is not in focus, this Mac only gets about 34 frames per second and 33 virtual machine cycles per second. Now, if you click on the Spaceland window, the buttons light up red and green, and the title becomes bold:


Wow! The FPS shot up to 110 frames per second on this Mac, and all we had to do was click on the window. By giving focus to the window, we are literally telling StarLogo that we intend to give Spaceland our attention, and Spaceland speeds ahead. Then, when we want to control the model with blocks, as soon as we click on a block, Spaceland loses focus and slams on the brakes, giving the blocks a chance to move.

Labels: ,

Saturday, May 06, 2006

Change Agent: Disable keyboard camera controls

We think that one of the coolest features of StarLogo TNG is that you can fly around SpaceLand while your model is running. In aerial mode (the airplane button), you can use your mouse or the keyboard to pan the scene, rotate for the right angle, and zoom in for a better view.

Unfortunately, controlling the camera with the keyboard conflicts with another cool feature-- namely, controlling agents with the keyboard. To see what I mean, let's build a model that does just that (or download it).

First, we'll make a procedure called Move that checks whether a key is down, and, if it is, moves the agent forward one step in that compass heading. Note that since we're using if blocks instead of if-else blocks, the procedure can detect when we press more than one arrow key at the same time to make the agent move on a diagonal.

Next, drag out a Forever block and call the Move procedure we just created.

Finally, let's setup the model to create a single turtle agent in the center of SpaceLand.

Great! Now click the Runtime tab, where you'll see two buttons called Setup and Forever. Click the Setup button to create our single turtle in the center of the screen, then click Forever to start the model. Now, use the arrow keys to move the agent around.

Uh-oh. While you may be able to tell that the agent is moving in response to your key presses, you were probably more distracted by the camera moving around too. Sure, you could switch camera modes to the 2D top-down view, or perhaps use the the over-the-shoulder perspective, but what if you really want to keep the model running in aerial mode?

Fortunately, there's a menu item tucked away under Options called "Keyboard moves camera." If you look at it, you'll see a checkmark next to it, which indicates that, yes, in fact, the keyboard is controlling the camera (but we already knew that).


In the next release we intend to make this option more visible, but for now, all we have is the menu item. So choose Options... Keyboard moves camera, and the next time you look at the Options menu, you'll see that the checkmark is gone. The keyboard no longer controls the camera, and you are free to use all those keys in your model to control agents. Go ahead, try it!

Labels: ,