Tutorial 8: Working with Crowd Animation

Lesson 4: Applying Logic to Crowd Behavior

In the previous lesson, you learned how to switch a team from one behavior to another by animating the Active status. You did this by setting up and solving the first part of the simulation, and then using that feedback to set up the second part. This isn't a very flexible method, though. If you change the target position, or the delegates' speed, the frame the behaviors should switch would change, and you'd have to move at least two keys. With more complex simulations, such as those with multiple or moving targets, or delegates that travel different distances, modifications can be difficult.

For complex situations, you can assign cognitive controllers to delegates and teams instead of behaviors. The cognitive controller has several states. Each state can contain one or more behaviors, each with a different weight. You set the states up as a diagram and link them with transitions. The transitions use MAXScript scripts to determine when one state relinquishes control to another.

In this lesson you'll use a cognitive controller to change a team's behavior from Seek to Repel when the reaches the box.

Setting up the behaviors

  1. Open the file you saved in the middle of Lesson 2 (cs_tut08_lesson4a.max) or, if you like, open cs_tut08_lesson4.max from the cstudio\tutorials\tutorial_8 directory in your 3DS MAX path.

    This scene contains 10 delegates scattered in a spherical pattern about a small, centrally located box.

  2. Select the crowd object and open the Modify panel.

  3. Use the Edit Multiple Delegates dialog to turn off Constrain to XY plane for all the delegates.

    The method for doing this is covered in the previous lesson, Applying Avoidance and Animating Behavior Assignments.

  4. Add a Seek behavior and a Repel behavior and set the box as the target for both behaviors.

  5. For the Repel behavior, turn off Use Radii.

Using the cognitive controller

Instead of assigning these behaviors to the delegates, you'll use them to build a cognitive controller and assign the controller to the delegates.

  1. In the Setup rollout, click Cognitive Controller.

  2. In the Cognitive Controller Editor window, click the New button on the right side of the toolbar.

    This creates a new cognitive controller with the name Cognitive Controller and activates the Create State tool.

  3. Click twice in the editor window, in two different locations.

    This adds two state boxes to the controller. Each state can contain any number of behaviors.

    The cognitive controller editor with two states added

    The first state you added is dark red, which means it's the Start State. It is executed first when the cognitive controller begins working. You can set any state to be the Start state.

    To make the delegates seek the box first, you'll assign the Seek behavior to the first state. You'll also make the states' names more descriptive, and assign a behavior to each state.

  4. Right-click in the window to exit Create State mode, and then right-click the red state box.

    The dialog name reflects the name of the state at the time you open it. You can change the name, and the new name is displayed the next time the screen refreshes.

  5. In the State dialog, click the name in the upper, one-line edit window and change it to Seek.

  6. Click the Add button. In the Select Behaviors dialog, click Seek Behavior and click OK.

    The Seek behavior is highlighted in the list. You can change the weight, so if there several behaviors in a state, some can predominate over others.

    When you opened the Select Behaviors dialog, the screen refreshed and the State dialog name changed to Seek.

  7. Close the Seek dialog by clicking the Close box (X) in its upper-right corner.

  8. Change the second state's name to Repel and assign it the Repel behavior.

Creating a transition

You'll add a transition between the two states and create a short script to tell the cognitive controller when to switch between them.

  1. In the editor toolbar, click Create Transition.

  2. Drag a line from the Seek state to the Repel state.

    An arrow points from the first state to the second. This is the transition.

  3. Right-click the transition arrow.

  4. In the Transition ("Seek -> Repel") dialog, change the Duration setting to 10.

    This makes the transition to take place over 10 frames instead of 25. A shorter transition causes faster reactions.

    Note the other settings. If you change the priority and a state has two or more outbound transition conditions, all of which are true, the one with the highest priority takes precedence. You can also change the rates at which a transition eases out of one state and into the next.

  5. Click the MAXScript button.

  6. In the MAXScript window, enter the following:

    fn TestDist del t = (
    get_dist=distance $box01.pos del.simpos
    if get_dist < 120 then 1
    else 0

    Alternatively, you can load the MAXScript file test_dist.ms from the cstudio\tutorials\tutorial_8 directory in your 3DS MAX path.

    This script tests, at each frame, the distance between each delegate and the box. If it is less than 120 units, the delegate moves into the next state, which uses the Repel behavior.

    The name of the function, which is the second word of the script (after "fn"), must appear in the Transition dialog as well.

    The script that starts with "get_dist" calculates the distance between the position ([name].pos) of a specific object ($box01) and the position of a generic delegate (del.simpos). "Simpos" is a special position property used to determine a delegate position during a simulation solution.

    The transition script is executed once per frame for each delegate that uses this cognitive controller. Each time the script is executed, the cognitive controller substitutes a subsequent delegate for "del".

    The first time the script is executed at a given frame, it calculates the position the distance between the objects box01 and delegate01. The second time, it calculates the position the distance between the objects box01 and delegate02, and so on. To see each delegate's name as its position is tested, insert this line in the script anywhere between the first line and the last line:

    print del.name

    Be sure to have a Listener window open (press F11) before you run the solution.

    To learn more about how scripts work in transitions, read the Procedures sections in the topics Cognitive Controller Editor and State Transition Dialog, where you'll find more sample scripts with descriptions of their components and functions.

  7. Double-click the name of the function ("TestDist") to highlight it, and then press CTRL+C to copy it into the Windows clipboard.

  8. Close the MAXScript dialog.

  9. In the Transition dialog, click in the text box in the Transition Condition group to highlight the text, and then press CTRL+V to paste the name of the function into the box.

  10. Close the Transition dialog, and then the Cognitive Controller Editor.

    Two more short steps remains before you can test the transition.

  11. Open the Behavior Assignments dialog, and create a team from all the delegates.

  12. Assign the cognitive controller (CogControl) to the team, and close the dialog.

  13. Solve the simulation.

    The delegates move towards the box until they're fairly close, at which point they turn and move directly away.

  14. Try these variations:


Using Crowd with Animated Non-Biped Objects