Sponsored By

Implementing A Group Behavioral Control System Using Maya

Crowd scenes, schooling fish, and other forms of flocking behavior once required postprocessing techniques. However, using Maya Embedded Language, you can script these behaviors within the animation environment. Check out theory, code, and a short movie that demonstrates the technique.

October 11, 1999

19 Min Read

Author: by Kian Bee Ng

We've all seen it many times - flocks of birds, crowds of people, schools of fish, hundreds and thousands of insects. Specifically, what I'm referring to is the animation of crowd scenes. Traditionally, crowd scenes were created during postproduction, through the use of an editing feature that allowed producers to overlay, edit, and then overlay some more to create the requisite scenes. Nowadays, however, with the help of both powerful processors and innovative digital tools, it is not uncommon to see such slight-of-hand in television shows, movies, and even console games.

How difficult is it to create a crowd scene? This article will outline the theory and practice of creating the simple animation of a crowd scene with the help of Maya's powerful scripting language, Maya Embedded Language (MEL). However, be it using MEL, C/C++ codes, or other software applications, the fundamental theory and techniques described here may always be applied.

The Crowd Scene - A Group Behavioral Control System

Group Behavioral Control System (GBCS) is a term that I coined to reference the technique I use for controlling a large number of actors. The actors can be insects, fish, birds, human-like characters, or whatever creatures you desire to control. Some people may have different names to describe such a system, such as behavioral animation or crowd system, but generally we are all talking about the same thing; namely, to procedurally control a large number of actors with the minimum number of functional parameters.

The keyword in the above description is functional. By functional, I am referring to a set of mathematical algorithms or functions that suitably model the desired animation. Take a primitive example: we know that a square can be described using a basic set of xy inequalities and a circle can be described by the basic equation of x2 + y2 = r2. The two sets of descriptions are essentially mutually exclusive. That is, you cannot describe a square using the equation of circle, and vice versa. However, if you desire to animate the morphing of a square to a circle, you would need to derive a method of describing the shape of a square and the shape of a circle. With your method, there must exist a parameter that takes the form of a sliding value where, say, a zero (0) represents a complete square while a one (1) represents a perfect circle, and any in-between values represent a combined form of square and circle.

The challenge in developing a GBCS therefore lies in the derivation of a good set of functions that correctly describe your desired animation with as few controlling parameters as possible. Often, to achieve this, one must tear down individual functions and descriptions and build a new set of algorithms.


Implementation Considerations...

The Basics

In implementing a GBCS, there are two main points to consider:

1. The group behavior

2. The individual member's behavior

The group behavior represents the behavior exhibited by the entire group as a whole. For example, in animating a swarm of hornets, the entire movement of the swarm is its group behavior. However, while moving as a group, each individual hornet will, for example, be flying in a slightly different direction, moving at a different speed, and traveling at a different height. Such individual characteristics constitute its individual behavior. Another common example is a human crowd control system, where the crowd movement as a whole is the group behavior, and the individual members of the crowd exhibit their own individual, distinct behavior. Thus, to implement a group control system, you will have to consider both the group as well as the individual member.

AI and Behavior

In the past, the speed of hardware used to be a major concern when designing games. Nowadays, this is becoming less and less the case. Instead, many believe that the future of the gaming industry lies not in hardware features but in software techniques. In fact, several veterans in this field believe that the future of the industry may lie in the use of Artificial Intelligence (AI). Currently, several games are already integrating AI into their gameplay (one example is Age of Empires II from Microsoft).

When implementing a GBCS, it will be of more interest to the game player if AI is also incorporated into the gameplay. Using the hornet example above, once the core of the GBCS is developed, you can, for example, integrate AI into the hornet group so that the hornets exhibit enough intelligent to decipher, say, where the common places that the players like to hide are. The feeding of this AI to control the group behavior will certainly improve gameplay for the users.

The idea of integrating AI to GBCS is really nothing new. In 1987, Academy Award winner Craig W. Reynolds suggested in one of his Computer Graphics papers that the use of AI techniques such as an expert system could be applied to controlling group behavior. In such a system, autonomous actors should have, to a certain extent, the ability to determine their own actions. These actors should to a degree have a mind of their own, so that the animator need not be concerned with every detail of each actor's movement.

The Physics

When developing your GBCS, some difficult may be encountered in striking a balance between entertainment animation and real-world physics animation. In entertainment animation, characters and motions are exaggerated to give players a greater sense of satisfaction and excitement. For example, a character may jump 20 feet high just to clear an obstacle of 5 feet in height. On the other hand, physics-based animation will allow you to design your world based on the correct set of real-world physics rules, thereby creating a more realistic environment for gameplay. The latter approach has always been very restrictive and demanding in terms of programming complexity, as everyone is quite familiar with real-world physics, and thus any slight abnormality in the simulation is immediately conspicuous.

However, as more and more games are being created based on exaggerated movements, a quality physics-based games will be too distinct to be ignored. The likelihood of an increased trend towards physics-based animation received a boost when Sony chose Mathengine as its Playstation middleware partner for its physics and 3D interactive entertainment. The recent collaboration between Mathengine and Softimage to deliver real-time physical simulation and behavior tools to game developers and animators indicate that apparently, quality, physics-based animation is upon us.

The integration of real world physics into a GBCS poses a great challenge to developers. The requirement of obtaining a functional procedure to describe the complete group behavior is a formidable task in itself, let alone the incorporation of real world animated physics. Thus, unsurprisingly, most present solutions to achieve the effects utilize tricks, cheats, and clever programming, including the example that follows.


An Example Using Maya: Requirements and Design

The example used in this article creates a group of flying fish that jump around in the ocean. Their direction and other traits are controlled procedurally by the user in real time using Maya. The fish themselves exhibit individual characteristics, such as tweaking their bodies while jumping, while still maintaining the unity of their group.

The following table shows an example of a few control parameters that are desired for the system:

Group Behavioral Control System

Parameter Name


Start Position







Advanced Control

Collision Avoidance


The Tools

Alias Wavefront's Maya is one of the most complete tools available, capable of providing users and developers with the power to do animation and game productions, from prototyping to the final delivery. The strength of Maya lies in its innovative workflow and user interface as well as its custom scripting capability. The scripting language MEL provides a good basement for users to quickly test out their ideas and methods. The ability to implement custom codes is a crucial flexibility, a fact that, unfortunately, many other software and tool vendors failed to see, instead electing to bury the flexibility deep in their program codes.

In addition to MEL, Maya also provides a good API library that allows you to implement your system as a plug-in using C++ codes. A plug-in implementation runs at least several times faster than the MEL script implementation. Best of all, the API comes with standard Maya software, and does not cost the user an extra cent.

Mathematics for the movement

The main part of this system lies in the jumping motion of the fish. To recall, a typical trajectory path can be described using a simple mathematical equation:

S = ut + ½ at2


S is the displacement of the object

u is the initial velocity of the object

t is the time of flight

and a is the acceleration, which in this case is the force of gravity.

Assuming that the air resistance is negligible, this equation is appropriate to model the movement of the fish in the air, with S and u being vector quantities. Since MEL provides a complete set of vector functions, you will be able to implement this equation easily.

Looking at the above trajectory function, however, you may realize that it could turn out to be quite complex and troublesome when you need to build in all the controls for the motion of the fish. What determines whether a function is useful or appropriate in modeling a system is not merely its accuracy in producing real-world results, but also the ease of implementation and its flexibility. In the entertainment production environment, realistic simulation is something that is great to have, but flexibility and extensibility of the functions always take precedence.

An alternative to the trajectory functions is the very simple and versatile trigonometry functions, sine and cosine. Some of you may realize that sine and cosine functions are some of the easiest to use and most versatile building blocks of many wonderful algorithms. In this case, using the frequency, amplitude, and the phase components of the sine and cosine functions, along with some modification and appropriate changes in values, you are able to create variable control for the speed and height control of the animation. And with the help of some random functions and simple vector manipulation, a rather convincing and simple control system is created.

The MEL codes shown in Listing 1 illustrate an example of the result that you could attain by expanding on the above discussion.


Listing 1: Sample MEL codes to implement a group behavioral control system.

float $t = time;
float $speed_ctrl[];
float $height_ctrl[];

int $num; // number of fish

vector $position_start[];
vector $heading[];

float $current_t[];
string $object = "fish";
$num = 6;

for ($i = 1; $i <= $num; $i++)
// Initial position

if (frame == 1)
$position_start[$I] = abs (sphrand (<>));
$speed_ctrl[$i] = rand(3.0,4.0);
$height_ctrl[$I] = rand(1.5,1.5);
$current_t[$i] = time;
$heading[$i] = <>;

// Direction control

int $lowest_point = 141.37167/$speed_ctrl[$i];
int $cycle = 188.49556/$speed_ctrl[$i];

if (((frame + $cycle)%($cycle)) == $lowest_point)
$heading[$i] = <>;
float $temp3x;
float $temp3z;
vector $temp_heading;

$temp3x = getAttr ($object + $i + ".translateX");
$temp3z = getAttr ($object + $i + ".translateZ");
$current_t[$i] = time;
$temp_heading = $heading[$i] - <<$temp3x, 0.0, $temp3z>>;
$heading[$i] = $temp_heading;
$position_start[$i] = <<$temp3x,0.0,$temp3z>>;

// Position control

vector $temp;
float $posx, $posy, $posz;
$temp = $position_start[$i];
$temp2 = $heading[$i];
$posx = $temp.x + ($t-$current_t[$i])*$temp2.x*0.15;

$posy = $temp.y + ($height_ctrl[$i]*sin($speed_ctrl[$i]*time));
$posz = $temp.z + ($t-$current_t[$i])*$temp2.z*0.15;

setAttr ($object + $i + ".translateX") $posx;
setAttr ($object + $i + ".translateY") $posy;
setAttr ($object + $i + ".translateZ") $posz;

// Rotation control

float $rotz;
$rotz = -90+50*cos($speed_ctrl[$i]*time);
setAttr ($object + $i + ".rotateZ") $rotz;

// Further Rotation control

float $roty;
float $RTOD = 57.29578;
float $theta;

$theta = $RTOD*angle(<<1.0,0.0,0.0>>,$temp2);
$normal = cross(<<1.0,0.0,0.0>>,$temp2);

if ($normal.y > 0.0)
$roty = $theta;

$roty = (360-$theta);

setAttr ($object + $i + ".rotateY") $roty;


The individual model

During the development of your system model, it is often better to utilize a simple geometry as the actor instead of employing the actual geometry, since presumably the actual geometry will be of greater complexity. In most development, I usually find it easier to use basic primitives such as cones and spheres as the stand-in model. Once the motions are accepted, the actual model is then used to further fine-tune the animation. In this example, the cone primitive is the stand-in geometry.


Further distinctive action

To add more life to the fish, you should segment the fish into a four-part skeleton structure - the head, upper body, lower body, and the tail. This four-part structure will allow you to procedurally control each of them so that no one fish will exhibit the same motion as the other.


To ensure that the actions for each fish are distinct, one must write four different functions to control each part of the fish. Of course, each function that you derive must be linked to the others, so that the fish will still appear to be moving as a whole. With the four functions derived, what is left is to further integrate them into one single procedure, with as few parameters as possible, which you can use to control and describe the range of animation that you desire.

User interface

In Maya, you have a pre-defined set of MEL commands to create various types of user interfaces. These MEL commands allow you to write your own functions such as the control system, while still hiding the complexity through the custom user interface. The custom interface will allow your users to use your system without worrying about the underlying details.

Like any interface design, you should be aware of what your users want, and be able to bring the tools to them. Listing 2 shows an example of the user interface created using MEL that corresponds to the parameter control of the system.


What has been presented here are the fundamentals of developing a system to control a large number of actors. Like any solution to a problem, there is no single best approach. A new scenario will often require you to take a fresh multi-displinary view. However, bear in mind that a good system is usually one that is well designed, able to incorporate the physics requirement, and yet be aesthetically impressive. Hopefully this article will give you some ideas on how best to incorporate your own group behavioral control system for your next animation and game. Good luck!

Kian is currently working as a Technical Director for a production company in Japan. Though extremely busy with his production schedule, he still managed to steal some of his employer's time to finish a book called Digital Effects Animation Using Maya (http://www.4deffects.com). A more detailed and complete implementation of the examplein the article may be found in the book. You can email him with any comments at [email protected].

Read more about:

Daily news, dev blogs, and stories from Game Developer straight to your inbox

You May Also Like