Tutorial 8: Working with Crowd Animation
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
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.
Select the crowd object and open the Modify panel.
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.
Add a Seek behavior and a Repel behavior and set the box as the target for both behaviors.
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.
In the Setup rollout, click Cognitive Controller.
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.
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.
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.
In the State dialog, click the name in the upper, one-line edit window and change it to Seek.
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.
Close the Seek dialog by clicking the Close box (X) in its upper-right corner.
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.
In the editor toolbar, click Create Transition.
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.
Right-click the transition arrow.
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.
Click the MAXScript button.
In the MAXScript window, enter the following:
fn TestDist del t = (
get_dist=distance $box01.pos del.simpos
if get_dist < 120 then 1
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:
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.
Double-click the name of the function ("TestDist") to highlight it, and then press CTRL+C to copy it into the Windows clipboard.
Close the MAXScript dialog.
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.
Close the Transition dialog, and then the Cognitive Controller Editor.
Two more short steps remains before you can test the transition.
Open the Behavior Assignments dialog, and create a team from all the delegates.
Assign the cognitive controller (CogControl) to the team, and close the dialog.
Solve the simulation.
The delegates move towards the box until they're fairly close, at which point they turn and move directly away.
Try these variations:
Add a second transition from the Repel state to the Seek state that turns the delegates back around after they get a certain distance away. Hint: You need to change only three items in the first script.
Animate the box so the delegates have to chase it before they can get close enough for the transition to begin. Tip: You might have to increase the End Solve setting.
Use Edit Multiple Delegates to set random speeds for the delegates so they don't all reach the box and turn around at the same time.
Using Crowd with Animated Non-Biped Objects