Wednesday, July 12, 2006

Block Talk: Hey! Who hit me?

Collisions, or agent-agent interactions, are at the heart of many StarLogo models. For example, in case you haven't seen it yet, the Paintball Tutorial provides an example of using collisions to determine when agents of breed Paintball collide with agents of breed Turtle.

Whenever you create a breed, new collision blocks are added to every breed drawer. If you have two breeds, say, Turtles and Giraffes, then the breed drawer for Turtles will have a Turtles-Turtles collision block as well as a Turtles-Giraffes collision block. The breed drawer for Giraffes will have Giraffes-Giraffes and Giraffes-Turtles collision blocks.

It is important to note that the Giraffes-Turtles and Turtles-Giraffes blocks are effectively the same. If you take one out onto the Workspace, the other disappears from the drawer as well, because you can only have one collision block for each pair of breeds.

Many times, knowing that an agent collided with an agent of a particular breed is enough. In the example below, whenever a turtle and giraffe collide, the turtle reacts to the collision by turning right 90 degrees. It does not matter which giraffe was involved in the collision, only that it was some giraffe, and that was enough to make the turtle want to turn 90 degrees to the right.

However, sometimes we do want to know information about the other agent in the collision so we can react specifically to that agent. For example, what if the turtle would prefer to turn in the direction of the giraffe instead of simply turning right? Well, the turtle would need to set its own heading to the heading of the giraffe, as shown below. But we're missing something in the example. We need the who number of the giraffe that collided with us in order to obtain its heading.
That's where the collidee block comes in. It is special block found in the Other Agents category (show below), which is the same category that the "heading of" block is in. The collidee block is a special kind of number block that can only be connected inside of collision blocks. It automatically provides the who number of the other agent in the collision. Thus, if you use the collidee block in the Turtles region, then it returns the who number of the giraffe. If you put it in the Giraffes region, then it returns the who number of the turtle.


Therefore, in our example, if we want the turtle to set its heading to the heading of the giraffe, we use the collidee block to get the who number of the giraffe for the "heading of" block, and we're done!

If you look at the Paintball Tutorial, you'll see that the collidee block is used to set the color of the turtle to be the color of the paintball it collided with. The collidee block is not used in the Paintball region, because the paintball does not care which turtle it hit. The paintball dies because it hit some turtle, regardless of which one it happened to be.

In your models that incorporate agent-agent interactions, you won't always need the collidee block, but if the response you need requires information from the other agent in the collision, just remember to use the collidee block and the other blocks found in the "Other Agents" category.

Labels: ,

0 Comments:

Post a Comment

<< Home