Category: Projects

Character Modeling and Animation in Maya

My latest project in game development is to create a fully animated player character designed to be easily portable into a game engine be that Unity, Unreal Engine or someone’s custom made game engine.  This is still a work in progress so it is difficult to say when it will be finished, but here is a demo showing the character in its classic T-pose as well as its completed walk animation.

For the remainder of this post I would like to give out some tips to anyone wishing to create similar works in Maya.  It may not look like it but this was the first time creating a character model and animation in Maya, which is why I would to give out some tips on how to avoid some of the same beginner mistakes that I made when working on this project.

Getting Started:

The first thing any good modeler needs to get started is some reference material of what they want their character to look like from various angles.  For bipedal characters it is generally preferred to model your character starting in the T-pose and to import image planes featuring at least the front and side profiles of your character.  This may seem like a high hurdle for those of you who lack artistic talent, but don’t panic because I myself am not artistically inclined either.  If you already have a character in mind that someone has already created plenty of reference material for you to use it may not be as difficult as you think to modify the image into a T-pose.  The following images are a front and side profile of a character that I created using only Adobe Illustrator’s pen tool and built-in rulers.  I based my model on a character that appeared in a manga that I read online called Flower Flower and used various images from that manga as reference material.

shuurei_front_profile shuurei_left_profile

You may have noticed that for the side profile I didn’t bother to draw the arm since most of the detail would be lost, not to mention it is not really  necessary to model the arm from the side view.  You may find that you need additional reference material for finer details such as the hands.  When you want to save your reference or texture images to your project folder you want to make sure you save them to your sourceImages folder instead of your images folder since the images folder is where Maya saves its rendered images when doing a batch render.  One final thing to note when importing your images into Maya is that many people prefer not to use Maya’s built-in image plane feature because they find it difficult to work with, for example one problem that I frequently had when trying to work with them was that I sometimes found that was unable to select them in the viewport even when I made sure that the layer that they were in was neither a template nor a reference.  Eventually I got so annoyed that I replaced them with polygon planes with a transparent file texture.

Modeling:

I will not cover how to do the actual modeling but I can give some suggestions as to where to start.  Here are some popular tutorial series that I used to help me model my character:

Maya LT Advanced Training: Character Modeling by Matthew Doyle (recommended)

3D Character Modeling: The Geek by Carol Ashley

One place where the the first tutorial series mentioned above is particularly useful is in described how to best model the head with good topology however it doesn’t really explain why good topology is so important.  If you want to learn more on this I suggest watching Modeling a head with proper topology on the Maya Learning Channel.  In particular I suggest looking at the third video in the series since it gives a color coded visual representation of how you may want your finished head to look.

One thing I suggest for a character of this nature is to model the body and the clothes as separate objects to start off with.  This can be helpful later if you want to be able to model the character in different outfits.  In the animation demo above the coat, sash and pants are attached to the body using a simple wrap deformer while the shoes are a part of the main model.  Keep in mind that we generally want our game characters to have a range of about 5,000-25,000 tris.  My character currently has around 27,000 tris, however I can reduce this number if I delete much of the internal geometry that will be hidden by the clothes.  Ideally the pants will be merged with the main body before getting imported into our game engine.  The coat and sash may need to dealt with differently depending our game engine since we probably want it to behave in a more realistic fashion.

Texturing:

If like me you are artistically challenged like me many of you may wish to skip making your own custom textures for your character.  I only took the time to create my own texture because I wanted to see what it would like textured and although it is plain to see that my textures could use some polishing I am quite proud of the results.  However, even if you do not wish to create your own textures you should at least take the time to make sure your UV maps are clean before proceeding to the next steps.  The reason for this is that if you decide to duplicate any part of your mesh either for use in another model or for creating blend shapes for the current model then that UV map gets copied as well and it can be a nightmare having to modify the same UV map from scratch.  For more on UV mapping see the last video in the series Maya LT Advanced Training: Character Modeling by Matthew Doyle.

For those of you who do want to try your hand at creating textures here are a few useful links that I found helpful:

Creating Textures for Characters in Autodesk Maya by Ryan Bird

How Do I Make Velvet Texture in Photoshop? by David Weedmark

Create a Linen Texture in Photoshop by Howard Pinsky

Adobe Photoshop – Basic Hair Texture Tutorial for IMVU, Second Life, The Sims & More by YouTube user TheRealMrsMVP

Realistic Eye Texture Painting by Krishnamurti Martins Costa

Rigging:

When it comes time to rig your model be sure to create a new scene file and then just import your model into that file.  That way if you ever need to make changes to your model you will not break the functionality of your rig.  I highly recommend following this tutorial on character rigging:  Creating a Character Rig by the Maya Learning Channel.

There are a couple that the above tutorial does does not mention that you should keep in mind.  The first is how to properly use to pull vector constraints.  If you follow the tutorial exactly you may never run into the problem that I had, but in case you do here is how you can fix it.  When you create your pull vector constraint on your IK arm/leg you may find that the model has twisted slightly and no longer matches your FK arm/leg.  This can cause some unsightly snapping when switching between IK and FK.  The reason for this is that the object that drives the constraint is not on a plane with the joints that the IK handle controls.  To get around this problem we could just recreate our joint structure from scratch making sure that there is no unexpected bend in the joints.  Alternatively, we can create a triangular plane and snap its corners to the joints controlled by the IK handle, scale the plane out and make its surface live.  then we can  snap the object diving the pull vector constraint to that plane.  Then when you make your pull vector your IK and FK arm/leg should match.

Another thing to keep in mind is that you will need to have additional joint structures for the head and the jaw.  While jaw motion may arguably be handled using blend shapes, you must at least have a couple of head joints so that when you skin your rig you have something to paint your head weights to.  If you try to paint your head weights to your neckEnd_result_JNT then when you rotate your head control you will have your model’s eyes popping out of their head due to the spline neck’s rotation.

Although I said that jaw motion could be handled using blend shapes, I prefer the method of using joints.  Jaw motion is controlled by a control curve set that is set within a bounding bound grouped together with a bunch of other controls for manipulating facial expressions.  I also created a joint structure for my characters tongue which is based closely on the  joint structure for the spine.  I gave the tongue a dual set of controls so that the animator would have the choice of how they want to be able to manipulate the tongue.  The blend mode is controlled by an attribute on the IK head control.  In auto mode the animator can use a set of controls grouped with the facial expressions to control the position, length and width of the tongue.  In manual mode the animator  can directly manipulate the position and rotation of the tongueEnd_bind_JNT which in turn affects the squash and stretch of the tongue.

facial_ctrls manual_tongue_ctrl

Blend Shapes:

While joints are useful for animating a change between several poses, blend shapes allow us to change the mesh of our model by blending the positions or our mesh’s vertices between one set of positions and another.  This is particularly useful for blending between facial expressions.

When using blend shapes it is important to understand that every vertex in the mesh has an index or number associated with it.  When using blend shapes maya interpolates between the positions of vertices with matching indices according to the blend weight.  So in order for a model and its blend shape to deform properly the indices of their vertices need to line up.   If we duplicate a mesh the vertices of the two meshes should match up appropriately.  However, if we modify the topology of the duplicate and then create our blend shape then we are likely to result in what many refer to as the spiky-ball-of-death.  So it is incredibly important to maintain the same topology between our original mesh and its respective blend shapes.

However, often it is inconvenient to have to duplicate an entire mesh for each blend shape, especially if all we want to blend is the head.  One way we can accomplish this is by separating the part of the mesh that we want to blend from the original mesh and creating our blend shapes from duplicates of this mesh.  (I personally like to export this duplicated mesh into a separate file and create my blend shapes there since as Maya scene files grow larger and larger they use up a lot of memory and can slow your system down considerably.)  When we want to recombine our separated head to the body mesh simply make sure that you select the mesh that you wish to attach a blend shape to before you select the main body.  This ensure that the indices of the vertices that are being blended stay the same.  Then when you go to create your blend shape node simply make sure that the option Check Topology is turned off.

Another problem that might crop up when creating blend shapes is the possibility that you may wish to mirror a blend shape.  Unfortunately, Maya does not provide any built-in tools that make this easy to accomplish.  But fear not, because there is an open source MEL script that can help you accomplish this with little to no effort.  It and a video tutorial on how to use it can be found at http://www.animationmethods.com/scripts.

Finally if it turns out that you absolutely need to make changes to your topology there is a way to do it without having to recreate all your blend shapes from scratch.  There is a tool called Bake Topology to Targets which (possibly) can give you the results you need.  The YouTube video Maya BlendShape Tips and Tricks by Steven Roselle shows how this can be done.

Animation:

Before starting to create our animations it is a good idea to study up on how to use the Maya Trax Editor.  The YouTube video Maya Trax Editor Tutorial – by Matty Mac gives a pretty good explanation of how it works.  The basic premise is that first we need to create a character set of all the controls that we are going to need within the file that contains our rig.  Then starting in a new file we create a reference to our character rig file.  Making sure our character set is not selected we key in our animation to the time slider.  Next we select our character set, add it to the Trax Editor and then go to Create->Animation Clip->Options and make sure to select the TimeSlider as the source.  Then it is a simple task to export the animation clip to your clips folder.

For more advanced animation you can also check out the YouTube video on Animation Layers titled CGI 3D Tutorial HD: “Using Animation Layers in Maya” – by 3dmotive.

Additional References:

Facial Rigging Tutorial by Adam Bailey

Tips and Tricks: Blendshape Heads by Jennifer Conley

Projects

Here is where I keep a brief list of some of the projects I have worked on so that anyone who visits my blog can quickly view my past and current projects.

The following are my most recent projects:

Character Modeling and Animation in Maya

Cripple Mr. Onion

Captain Polly’s Booty

A* Pathfinder in Unity

What is Cripple Mr. Onion?

Cripple Mr. Onion was originally a gambling card game played by the characters in the fictional universe of the Discworld novels written by Terry Pratchett. Since no official rules have been set by the author himself, many Roundworld (our world) denizens have taken it upon themselves to create a set of rules for the game. One popular version was designed by Andrew C. Millard with help by Prof. Terry Tao and was even mentioned in the book Turtle Recall, which is a kind of Discworld encyclopedia written by Terry Pratchett himself. The book gives a vague description of the rules with the understanding that people playing the game are free to modify or ignore the rules as set. What follows is the unique interpretation I envisioned and I hope you will all enjoy playing it as much as I enjoyed creating it.

CMO Screen CaptureMany versions of the game envisioned it with eight suits of cards much like combining two sets of regular playing cards but with the suits all different. The book Turtle Recall describes the deck as a regular set of playing cards combined with the complete Caroc deck (Discworld tarot deck). In my version there are a total of 78 playing cards divided up into six suits of thirteen cards each. The first four suits, turtles, elephants, staves and octograms are exactly like a regular set of 52 playing cards but with their suits renamed. The remaining two suits follow the same numbering scheme as the remaining four but have unique names for each card. The suits themselves are called the Lesser Arcana and the Greater Arcana and together they form the Caroc deck.

The game itself bears striking resemblance to both Poker and Blackjack. Like Poker there are various hand ranks that become exceedingly rare the higher their rank goes. Cards have number values just like in Blackjack and the rank of one’s hand is determined by how well you group the cards in your hand to sum to 21. However, what truly separates Cripple Mr. Onion from its Roundworld counterparts are the various optional rules that can be included. These are otherwise known as the “Modifiers.” A player may choose to use as many or as few modifiers as they wish with the exception of the Crippling Rule which is always active (hence the name Cripple Mr. Onion).

The user interface is built using Java’s swing library. The main application runs on the swing library’s Event Dispatch Thread (EDT). Meanwhile, the main game loop runs on the Engine Core thread and determines which animations need to be played and what the board should look like at various stages of the game. Whenever the core thread needs to start an animation, request input from the user or change the appearance of the board it calls a static method of the Application class to get an event object which it forwards to the EDT. This allows the core thread to make thread safe changes to the GUI.

The Application object loads all the necessary game resources and initializes the various swing components, some of which include a menu bar and glass pane as well as the content panes for various dialogs. The Application provides static methods for starting these dialogs with the appropriate modality and parent window. The menu bar provides the user with a list of game options and help tools which call these methods. The Table object represents the game view and acts as the content pane for the main window. It provides various public methods for changing the appearance of the board and for enabling and disabling certain components. The Animation Pane is the glass pane of the main window and is where all the animations are drawn.

Rendering animations using Swing components can be tricky since drawing only occurs when a paint event is dispatched by the EDT, a method known as passive rendering. The Animation objects in this game use threads that repeatedly update before calling the repaint method on the component to which they are attached. Then whenever a paint event is dispatched the given component’s paint method merely calls on the paint method of its attached Animation object.

// Animation.java

import java.awt.Component;
import java.awt.Graphics;
import java.util.ArrayList;

/**
 *
 * @author GK Potts
 */
public abstract class Animation implements Runnable {
    private final int SLEEP_DELAY = 25;

    protected ArrayList<AnimationListener> listeners;
    private Component context;
    private Thread currentThread;
    private long startTime;
    private long oldTime;
    private long currentTime;
    private boolean playing;
    private boolean running;

    public Animation() {
        listeners = new ArrayList<>();
        playing = false;
        running = false;
    }

    public abstract void paint(Graphics g);

    public abstract void update(long elapsed);

    ...

    @Override
    public void run() {
        long elapsed, sleep;
        running = true;
        currentThread = Thread.currentThread();
        startTime = System.currentTimeMillis();
        oldTime = startTime;
        for (AnimationListener listener : listeners) listener.onStart();
        while (running) {
            currentTime = System.currentTimeMillis();
            elapsed = currentTime - oldTime;
            if (playing) {
                update(elapsed);
                context.repaint();
            }

            oldTime = currentTime;
            sleep = SLEEP_DELAY - elapsed;
            if (sleep < 0) sleep = 2;

            try { Thread.sleep(sleep); }
            catch (InterruptedException e) { return; }
        }
    }
}

When an animation event occurs, an Animation object is constructed using position data collected from the Table. The Animation is then fitted with its own personal listener object whose methods are called whenever the animation is started, paused, stopped or reaches its end.   The only one of these methods that is truly important is the OnComplete method because it notifies the core thread when the animation is finished.   The Animation object is then attached to the Animation Pane and finally started on a new thread. Note that the animation thread does not need to be invoked on the EDT since it does not make any direct changes to the GUI. It merely calls the repaint method of the component to which it is attached. Repaint merely schedules a paint event to be executed later, thus making our Animations thread safe. If we ran our animation thread on the EDT our GUI would freeze until the animation thread ended.

In addition to having Animations for moving objects, this game also supports sprite based animations. Each time a card flips over or a modifier effect is activated, a sprite based Animation Clip object determines which frame to use based on the total elapsed time. This Animation Clip object is based almost entirely on the sprite based animation class that I wrote for use in the game engine I built for my previous game Captain Polly’s Booty. To see my blog posting on sprite animation click here.

The various Animation subclasses each utilize an Animation Data object which stores the position, rotation, and image data for each of the cards and provides helper methods for stepping forward the position, rotation and animation. Then using Hash Maps the animation objects map the Card objects to the Animation Data objects allowing the animation to simply cycle through the list of cards to update their positions and paint them. This way the order of the list determines what order to draw the cards in. Additional these data objects also store a rotated version of the image so that the program does not need to call the image rotation function, which is rather time consuming, within the paint method.

To prevent the clipping of images as a result of rotation each image is resized to a square whose sides are equal to the diameter of the circle that completely encloses the image. The images are then rotated about their center and position data is altered to take into account the change in size.

Player hands are evaluated through the use of two Objects: the Grouping, and the Hand Evaluator. Grouping merely looks at a set of cards and determines if it forms a valid card grouping and stores its overall ranking. Hand Evaluator looks at a player’s entire hand and attempts to create the best possible Grouping from the cards available. Hand Evaluator is also useful for evaluating possible hands by being able to generate modified copies of itself. These copies can then be compared to the original to see which one is a better hand.

Captain Polly’s Booty

Captain Polly’s Booty is a side-scrolling platform game originally based on the game Super Mario Bros. 3 for the NES. Super Mario Bros. 3 was the first video game I ever played and I have loved video games ever since. That is why for my college course on Game Programming I decided to create a platform game using one of my favorite sprites from Super Mario Bros. 3 as the model for my player character. Not weighted to the ground like in most games, the player is free to soar the skies as they guide Captain Polly in the search for his stolen loot.

The goal of the game is simple – to collect all of the crackers stolen from Captain Polly’s private collection. Though free to fly most places, the Captain must beware of strong winds that would push him into dangerous enemies and traps. Enemies may be disposed of with heavy objects dropped from above, but be careful because carrying heavy objects weighs him down. He must flap his wings even harder when laden, however being weighted down can sometimes be to his advantage.

Captain Polly’s Booty is written in C++ and uses Open GL and SDL libraries as well as standard C++ libraries. I built the game engine from scratch, but used the source documentation for the Unity3D Game Engine to model the objects and libraries that make up the core functionality of my engine. The application is single threaded and all the in-game logic is done within the main game loop.

At the start of each game loop the application stores the current input and time data into the static Input and Time objects so that it may be accessed by the behavior scripts attached to the various game objects. Then the game loop calls the Update functions of all the Game Objects and their Components. The next step is to update the physics portion of the game engine and to resolve any collisions that may have occurred due to a previous update. The physics runs on a different timeline than the rest of the updates and only updates if a specific amount of time has passed, but still runs on the same thread as the main game loop. Next the engine handles any events that have been added to the event queue. Lastly the engine draws the scene after sorting the various renderer objects according to their draw layer.

Many of the objects that I created for this engine are based closely on the Unity Engine. Some of these include the use of static Input and Time objects for retrieving up-to-date system information. Another element that I based off of Unity was the hierarchical relationship between Objects, Game Objects, and Components. I also put my math skills to the test when I created my own Math, Matrix, Vector, Transform, and Physics components and libraries. Among the other components that I have implemented are Behaviors, Animations, Colliders and Renderers.

While the Animation and Renderer objects are extremely stripped down for the sake of 2D drawing, the Colliders and Physics libraries should (theoretically) be easily expandable to apply to 3-dimensional space. Using support functions provided by the Collider objects, the Physics library uses the Gilbert Johnson Keerthi (GJK) collision detection algorithm as explained by Casey Muratori on mollyrocket.com. Click here to visit his blog. I also use the Expanding Polytope algorithm to determine collision depth and normal. For more on the mathematical concepts I used in my game engine please visit the following pages:

Game Programming: Math Libraries

Vectors Part 1: Introduction to Linear Algebra

Vectors Part 2: Programming in C++

Matrices and Transformations (Not yet available)

Quaternions (Not yet available)

Raycasting (Not yet available)

GJK Collision Detection (Not yet available)

Expanding Polytope (Not yet available)

Shapecasting (Not yet available)

A-Star Pathfinder (Unity)

During a team project for a college course on game engines, I worked on creating a game called Icebox Anchorage using the Unity3D game engine.  During this time I built a pathfinding system that I intended to be used in our game.  Sadly it never made it into the final version of the game because it turned out to be unnecessary.  However, despite the fact that it was never utilized, it is still a fully functional system.

Using a set of nodes placed strategically about a scene, I formed a point-based grid that would act as the map that my pathfinding system would traverse.  Then, using the A* search algorithm, enemy AIs would then have the ability to locate and chase the player even through winding corridors and mazes by locating a path to the node closest to the player.  See a video demonstration below:

A* Algorithm:

The A* star algorithm is perhaps the most commonly used algorithm for finding the shortest path between two points on a map or grid.  It is amalgamation  of two other pathfinding algorithms: Dijkstra’s algorithm and Greedy Best-First-Search.  Dijkstra’s algorithm works by procedurally visiting vertices in the graph in search of a shortest path.  It maintains a two lists of vertices called the open set and the closed set.  The closed set contains the list of vertices that have already been visited starting with only the starting vertex or node.  As it does so it stores a value g(n) representing the total distance traveled from the start node.  Meanwhile, the open set contains all the vertices adjacent to those that have already been visited.  Dijkstra’s algorithm works by repeatedly removing vertices from the open set starting with those that have the smallest g(n) and adding them to the closed set until a path to the goal has been found or it has been determined that no such path exists.  The problem with Dijkstra’s algorithm is that often most of the paths that it checks are useless making Dijkstra’s algorithm computationally expensive.

Greedy Best-First-Search works in a similar way in that it maintains an open and closed set of vertices.  The difference is that instead of visiting the vertices closest to the starting point it visits the vertices closest to the goal vertex.  This is calculated according to some estimate h(n) called a heuristic.  This is generally much more efficient than Dijkstra’s algorithm until it runs into an obstacle.  Unfortunately, because it has to backtrack to find a way around obstacles, Greedy Best-First-Search often does not return a shortest path.

The A* algorithm takes the best of both these algorithms to find the shortest path in a way that ignores unlikely paths.  For each vertex in the graph it keeps track of not only of the travel distance g(n) to the node n, but also that nodes heuristic value h(n).  When choosing which vertices to visit first it goes by the vertices with the lowest cost value represented by f(n) = g(n) + h(n).  This way it balances the two in a way that allows it to find a shortest path without being overly expensive.

Unity Implementation:

Two nodes are considered adjacent to one another if there is a straight line path between the two nodes.  Each of the path nodes keeps a list of all the nodes that are adjacent to them.  A separate GameObject called AI Map acts as the parent of all the path nodes in the level and contains the script that contains the A* algorithm.  Here is how it was written:

public List<GameObject> FindPath(GameObject start, GameObject end) {
    // the set of tentative nodes to be evaluated
    Dictionary<GameObject, Node> open = new Dictionary<GameObject, Node>();

    // the set of nodes already evaluated
    Dictionary<GameObject, Node> closed = new Dictionary<GameObject, Node>();

    // map of the path taken to each of the node
    Dictionary<GameObject, Node> previous = new Dictionary<GameObject, Node>();

    Node startNode = new Node();  // g_value is given a default of infinity
    startNode.g_value = 0;
    startNode.f_value = Distance(start, end);
    open[start] = startNode;
    previous[start] = startNode;

    while (open.Count > 0) {
        // find the key value pair from the open set with the lowest f-score
        var current = open.Aggregate((a, b) =>
            (a.Value.f_value < b.Value.f_value)? a : b);

        // have we reached the goal?
        if (current.Key == end) {
            // reconstruct path and return;
            List<GameObject> path = new List<GameObject>();
            path.Add(end);

            Node scan = current.Value;
            // scan the list backwards
            while (scan  != null && scan.came_from != null) {
                path.Insert(0, scan.came_from);
                scan = previous[scan.came_from];
            }

            return path;
        }

        open.Remove(current.Key);
        closed[current.Key] = current.Value;

        PathNode node = current.Key.GetComponent<PathNode>();
        foreach (GameObject neighbor in node.adjacentNodes) {
            // ignore if the node has already been processed
            if (closed.ContainsKey(neighbor)) {
                continue;
            }

            // calculate the distance to the neighbor via the current node
            float tentative_g = current.Value.g_value + Distance(neighbor, current.Key);

            // Is this in the open set
            Node currentNeighbor;
            if (!open.TryGetValue(neighbor, out currentNeighbor)) {
                // if not create a new node with the maximum g-value
                // and add to the open set and map
                currentNeighbor = new Node();
                open[neighbor] = currentNeighbor;
                previous[neighbor] = currentNeighbor;
            }

            // Check if the new g-score is lower than the current one
            if (tentative_g < currentNeighbor.g_value) {
                // if so update the open set with this newly discovered shortest path
                currentNeighbor.came_from = current.Key;
                currentNeighbor.g_value = tentative_g;
                currentNeighbor.f_value = tentative_g + Distance(neighbor, end);
            }
        }
    }

    // no path found so return an empty list
    return new List<GameObject>();
}

The enemies contain a Seeker script that calls on the AI Map to find a path to its selected target.  The target object as well as the points on the returned path require a SeekerTarget script.  While the path nodes are considered to be static targets, the player is considered a moving target and in order to find a path to it, it is necessary to determine which path node it is closest to.  Path nodes store direction vectors to each of their adjacent nodes as well as the midpoint between every pair of of nodes.  This data is used to calculate the Voronoi region that each path node represents.  Using the sign of the dot product of two vectors it is possible to determine which node the seeker target is closest to.