Monday, September 2, 2013

What Is a Binding Pose in Character Animation?

3D Character modelers always model 3D models in a pose known as T-pose where the character model stands in a certain pose and the hands and body shows a figure like T (Figure1)


     Figure1: A 3D model in T-pose and bones placed in it


Modelers model the characters in T-Pose so the riggers can rig the character easily. This helps them to place the bones to adjust vertex weights and envelopes easier. When all the bones are placed in the 3D model and the skinning process starts, the current pose of the skeleton is saved. This pose is called Binding Pose. The name speaks for itself. Binding pose is the pose where you start binding a mesh to its corresponding skeleton.

The idea of binding pose comes from Bio-mechanics where a human body motion is studied with respect to a pose called Standard Anatomical Position. Standard anatomical position is actually shows the rotation origin for each limb. Within this pose, the human body muscles has the highest level of rest hence it's the best pose to consider for the origin of the rotation of the limbs. That's the reason the 3D modelers model the characters in similar pose because the muscles are the least contracted and it's the most neutral pose for the body. Then the character riggers place the bones to the mesh to define the biding pose as the origin of the transformation of the bones.

Now why a binding pose should be saved?  You might know that all of the vertices' positions initially should be calculated in modeling space. A skinned mesh's vertices are transformed by its corresponding skeleton. When the skeleton bones transform, the vertices' positions change based on the weights they have for each bone (bones transformations are calculated in mesh's modeling space (object space)). Now if the vertices' positions change based on the orientation of the bones the whole mesh will be distorted because of an extra orientation or maybe position that the riggers applied to bones to adjust them into meshes. The rigger transforms each bone to fit them in the mesh's body. These transformations (call it Binding Pose Transformation) should not be calculated for the final vertices positions. You can consider an example in which you have a skinned mesh, so by changing bone transformations the vertices positions should be changed as well. Lets consider just one bone rotation. The orientation of the bone in mesh's modeling space is equal to:

Bone Orientation =  Binding Pose Rotation * KeyFrame Rotation

If vertices get affected by the "Bone Orientation" variable, first they will be rotated by Binding Pose Rotation and next with the key frame rotation and that binding pose rotation will distort all of the vertices because the initial model that the modeler created is affected by some extra unnecessary rotations produced by the rigger. So for bones belonging to a skeleton of a skinned mesh, a reference pose have to be saved and this reference pose is called Binding Pose. It will be saved when you apply a skin modifier to your mesh or in other words when you start the skinning process. For each bone in character animation, all of the key frames are stored relative to its binding pose. For a skinned mesh, deformations occur based on the relative transformations of each bone to its binding pose. This will prevent the mesh to be distorted.

For animation blending each bone has a weight. The weight is ranged between 0 and 1 which 1 means the full calculation of the key frame transformation and 0 means the binding pose. Now any other number between 0 and 1 for a weight shows a blended transformation between bones binding poses and key frames transformations.

As a side note, saving the keyframes relative to binding pose facilitates the process of  animation retargetting and also provides easier ways to compress the keyframes.

Friday, June 21, 2013

Blending Between Walk and Run Animations

In most games, walk and run animations can be blended together by holding analog stick. Depending on how much analog stick is pressed, two animations (usually walk and run) start blending. There is a problem in blending between walk and run animations and that is these two animations has different timings. Blending between walk and run results an unexpected motion. In this post I want to talk about how we can blend between two animations with different timings like walking and running.

Before going further, let’s consider human jogging. Jogging is a movement between run and walk. It's not as slow as walk and not as fast as run. We can say that jogging is partially run and partially walk so the gait and hands should not move as long as they move in running and should not move as short as they move in walking. We can say that the transform of bones are averaged between run and walk in jogging. That's actually what we are doing in animation blending. Obviously, animation blending is a weighted average of different animations keyframes. So for now we know that jogging actual poses can be achieved by blending between run and walk animations. One another thing should be considered is that jogging speed value is also between run and walk speed. This means that jogging is slower than run and faster than walk. To achieve a jog animation by blending between walk and run, we also need to blend between speeds of two animations.

Now let’s consider how we should blend between walk and run to make a jog animation. First it comes from animators. They should animate walk and run with same normalized time. For example, if in walk animation, left foot starts planting on the ground at normalized time 0.5 then the left foot in run animation should start planting on the ground at normalized time 0.5 too and if the right foot in walk, starts planting on the ground at normalized time 0 and 1 (loop poses) then the right foot in run animation should start planting on the ground at time 0 and 1 too.

After making walk and run animations with the rules I mentioned, you can blend the speed of two animations with the same blend factor used for animation blending:

a = blend_factor        where  0 <= blend_factor <=1

T1 = walk_length     (in seconds)

T2  = run_length       (in seconds)

T1>T2

At the most times we need to blend animations with each other linearly so if we use linear animation blending then we need to blend speeds of walk and run animations linearly as well. For this reason I wrote a linear equation to show the time length of jog animation:

jog_length = (T2 -T1)  * a + T1       where jog animation is the result of blending between walk and run animations with blend factor ‘a’ linearly and jog_length is the time length for jog animation.

At the final step we should change the playback rate of both walk and run animations to achieve a blended speed:

Walk.PlayBackRate = T1 / Jog_length
Run.PlayBackRate = T2 / Jog_length

For avoiding some problems like round-off errors in floating points you can set the Run.NormalizedTime to Walk.NormalizedTime instead of setting the Run.PlayBackRate.

Make sure to change the speed of both animations before calling your blend function, otherwise you will face unexpected results.


Thursday, May 16, 2013

Some Important Features of unit Quaternions for Animation Programming

In the previous post, Euler angles and Quaternions were compared and some important reasons which causes the unit Quaternions to become the dominant rotation system in graphics and game engines were studied. Now we know that we can't run away from quaternions if we want to become an animation programmer, gameplay programmer or graphics programmer, so lets check out two important feature of quaternions.

First lets check out the multiplication of a quaternion to a vector. One vector can be transformed by being multiplied to a quaternion. This multiplication can both scale and rotate the vector. If we multiply an unit quaternion to a vector, it just rotates it and no scale occurs. This is why all the graphics and game engines normalize quaternions before multiplying it to a vector. So after normalizing a quaternion, a vector can be rotated with this equation:

Result = q * v * inverse(q);

Where q is an unit quaternion and  v is our vector which we want it to be rotated and Result is the vector v after being rotated by q. One most important feature of quaternion multiplication is that it's not commutative. So q1*q2 is not equal to q2*q1. This order of multiplication is really important for many animation algorithms like animation blending techniques. For example you can seek the importance of this rule in additive animations. Additive animations are one type of animation which are the difference of at least two animations. Mostly they are used for asynchronous events. For example you can create a breathing additive animation in which only character's spine bone is rotating in and out and other bones have no transformation. This additive animation can be added to any animation like idle aiming, running and many more. Now if we want to add an additive animation to an existing one, we should notice about the order of quaternion multiplication. First the main animation should be calculated, then the additive animation comes up on it so the rotations should be calculated in this order.

Now let's consider a simple example in which q1 is the rotation of main animation and q2 is the rotation of additive animation. the order of additive animation blending should be like this:

1- First apply q2 to the current vector (v) :
 Result1 = q2 * v * inverse(q2)

2- Second apply q1 to the Result1:
Result2 = q1 * Result1 * inverse (q1) = q1 * q2 * v * inverse(q2) * inverse(q1)

So you can see if you want to add an animation to another you have to multiply them like:

q1*....qn-1*qn*v*inverse(qn)*inverse(qn-1)*....*inverse(qa).

This means your vector first rotates by qn and then qn-1, qn-2 ..., q1

The order of the quaternions applied in the above example shows the local additive rotations. That means it will be added in the local space of each bone. If you reverse the order you will have global additive blending. That means it will let the main animation to rotate the bones and then the additive animation will rotate the bones in the world space. The global space additive is not usually used game or graphics engines. I've seen global space additive animations just in 3DSMax CAT and 3DSMax is a DCC tool not a game engine.

Just note that most of the graphics and game engines overload the quaternion-vector multiplication  with operator '*', so you can replace the 'q * v * inverse(q)' with just 'q * v'.  If you are using an engine or API which has overloaded quaternion-vector multiplication then you can apply the multiplication order mentioned above, like this:

q1*....qn-1*qn

Now lets consider another important feature of unit quaternions. You can consider the imaginary vector part of a unit quaternion as the axis which your vector wants to rotate around and the scalar part is some value shows the amount of rotation around that axis. The imaginary vector part has a great feature and that is, its magnitude is equal to sin(a/2) where a is the rotation degree or radian which you want your vector to rotate around the imaginary vector part of your unit quaternion. Also there is another important feature for unit quaternions and that is the the scalar part is equal to cos(a/2). So whenever you want to create an unit quaternion which can rotate your object around an axis [x,y,z] with a degree equal to 'a', you can follow these steps in order:

1- First normalize your rotation axis:

 [x,y,z]/ length( [x,y,z] )

2- Multiply the normalized rotation axis to sin(a/2) and use the result as your imaginary vector part.

3- Use cos(a/2) as your quaternion scalar part.

Now for a better understanding, lets consider an example in which you want to rotate your object around y-axis with 90 degrees. First you have to normalize your rotation axis which is [0, 1, 0]. In this example, our rotation axis is already normalized so we should not normalized it again. Second, we should create our imaginary vector part by multiplying the rotation axis to sin( 90/2 ):

sin (90/2) = sin (45) = 0.7071

Imaginary vector part = [0, 1, 0] *0.7071 = [0, 0.7071, 0]

At the third step, we should calculate the scalar part of our quaternion which is equal to cos (90/2):

w = cos (45) = 0.7071

The final quaternion should be like this:

[ 0.7071, 0, 0.7071, 0]

You can write it as its mathematical form:

0.7071 + 0i + 0.7071j + 0k = 0.7071 + 0.7071j

This can be considered inversely, for the times you have an unit quaternion and you want to know how much it can rotates your vector. In this situation, for finding the desired degree you can do this:

DesiredDegree = 2 * ArcCos(w)


Conclusion

Most of the game and graphics engines use unit quaternions for rotations. Learning to work with them is very important. Some people prefer to use Euler angles for rotations because they are most understandable but the fact is that the used graphics or game engine converts Euler angles to an unit quaternion because of the reasons i mentioned in this post. So by working directly with unit quaternions the conversion step of an Euler angles rotation to unit quaternions can be omitted.

In this post two important feature of quaternions and unit quaternions were considered. These two important features are using frequently in Animation, Gameplay or Graphics programming so its good to keep them in mind.

Tuesday, April 23, 2013

Unit Quaternions vs Euler Angles: The Pros

Quaternion algebra is used to transform one vector to another in 3D graphics and unit Quaternions (Quaternions with the magnitude equal to one) are widely used to represent rotations. Almost all of the 3D applications, graphics and game engines use unit Quaternions to represent orientation. By using them, each rotation can be represented relative to a reference point uniquely. Although understanding Euler angles is much easier than Quaternions but using unit Quaternions provide some good benefits in comparison to Euler Angles. I've listed the three most important benefits here:

1- Less Computational Overhead:
Quaternions have two parts, a scalar part known as 'w' and an imaginary part known as (x,y,z). So Quaternions can be represented as a four elements vector, (w,x,y,z). Euler angles has a 3x3 matrix representation. Quaternion production makes less computational overhead in comparison to Euler angles because of it's vector representation. Also Quaternions need less memory space in comparison to Euler angles.

2- No Gimbal Lock:
Euler angles rotation system can lead the application to a problem named Gimbal Lock. Gimbal lock occurs when we lose one of the degrees of freedom.  Unit Quaternions has no similar problem for rotation in 3D space. This is the main reason that makes unit Quaternions to be the preferred system to being used in computer graphics.

3- Better keyframe interpolation:
You know that  keyframes in 3D animations mostly contain 3D rotations. Interpolating between these keyframes is very important. Using Slerp Quaternion interpolation causes good results in interpolating between two key frames. Actually Slerp selects the shortest arc in all possible paths in the rotation space (a 3D sphere with a radius equal to the magnitude of the difference of the source and destination points) to rotate one point to another and this is what we need most of the times, specially in character animation. I've provided a video which shows the difference between two interpolations. The box on the left side uses Euler XYZ and the one on the right side uses Quaternion selrp for rotation interpolation. Both of them use 2 similar key frames. You can see the differences in interpolations:








In the next post i will talk more about the important features of the unit Quaternions, as understanding them is very important for animation programming.

If you are interested to know more about Euler Angles' keyframe interpolation and find out why sometimes it interpolates in an animator unfriendly way, you may like to check this post from my blog.

Sunday, January 13, 2013

A Good Paper About AI-Based Animation

This is a very good paper about AI-Based Animation. I believe their method can be used to generate some advanced combat animations like what you can find in Batman Arkham Asylum and Assasin's Creed III. By applying it you can find best transitions to other existing animations based on direction of control and smoothness of transitions. This can give a great diversity to game by selecting best suited motions with fluid transitions at any state the character goes. Their method creates better and more responsive transitions from what you can achieve from motion graphs.


James MacCan, Nancy Pollard ,"Responsive Characters From Motion Fragments" In Proceeding of ACM SIGGRAPH - 2007

Monday, October 15, 2012

Weird hierarchy of character studio biped

This post is in the class of technical animation.

Early video games have many animations, producing such amount of animations need a flexible animation pipeline and a flexible pipeline needs a flexible working package. Here is when 3dsmax Character Studio comes in. Character studio is such a flexible animation package, it can help animators to create animations as fast as they can, it's like a plug and play device, but it has some limitations such as zero degree of extendability, this means it has low level of customization but it's still a good package. The most annoying problem it has, is that it's a huge black box. It doesn't work in the way 3dsmax works. It was a standalone plugin on older versions of max but now it becomes an inseparable part of 3dsmax.

On this post i'm gonna talk about the weird hierarchy of character studio and the problems it can make on relatime animations in another environment such as your game engine.

For all who worked with character studio they know that if they rotate the "Bip001 Spine", "Bip001 Spine1" and it's children are going to rotate. it's natural because you find that they are it's children they have to move you can see it's structure in the picture below.







 But there are some other unyielding children which they never obey their parent. So let's take a look at actual hierarchy of a biped using 3dsmax schematic view.




 You can see that "Bip001 L Thigh" and "Bip001 R Thigh" are the children of the "Bip001 Spine", but when you rotate the "Bip001 Spine" you see no effects to legs!! That's weird! Character studio internally handles the transformation of the bones not in the way max handles the standard nodes in it's scene graph and more important it's transformation computing is different from an external environments like game engines. Before i talk about the problem and it's solution let me to compare the desired hierarchy and the default one.

From top to down:

1- You expect "Bip001 Prop" to be 'Bip001  Hand' 's child but it's the "Bip001" 's child
2- You expect "Bip001 L Thigh" and "Bip001 L Thigh" to be "Bip001" or "Bip001 Pelvis" 's children but they are the "Bip001 Spine" 's children
3- You expect "Bip001 L Clavicle" and "Bip001 R Clavicle" be "Bip001 Spine3"  children but they are the "Bip001 Neck"'s children

You might never find any problem in this kind of hierarchy, because you can see what you expect in max, also there is no problem if you export the animation with normal keyframe sample rate (maybe 30 samples per second or higher) and play it on your game engine because the exporter stores all the transformations at each time step. The main problem occurs when you want to blend between two or more animations with some techniques like partial and additive animations or some procedural animation like inverse kinematics or ragdoll simulations.

Assume a situation in which you have 2 animations, a run and a shoot, if you play each one separately you will see a normal animation, an expected result, but assume that you want a make a shoot while running using partial blending. So you have to make a blend mask for upper body of shoot animation and a blend mask for lower body of run and then playing both animations. When you do that you will see an unexpected result. The legs rotate when you shoot this is because when the upper body animation plays it affects the lower body since originally Bip001 spine is the parent of Bip001 L and R Thighs so when it start rotating it affects the legs. You can find the same problem on the Bip001 Prop bones in the same situation or if you add another animation to the spine or it's children, the prop bone is "Bip001" 's child so it moves whenever Bip001 moves, in this case the additive animation applies to some bones rather than Bip001 so the prop bone inherit no orientation and it leaves the body.

There is a simple solution for this problem. Just do this simple changes on your rig once and for all when you want to start rigging your character it has no side effects and it solves all of the runtime problems:

$'Bip001 prop1'.parent = $'Bip001 R Hand'
$'Bip001 Spine'.parent = $'Bip001'
$'Bip001 L Thigh'.parent = $'Bip001'
$'Bip001 R Thigh'.parent = $'Bip001'
$'Bip001 L Clavicle'.parent = $'Bip001 Spine3'
$'Bip001 R Clavicle'.parent = $'Bip001 Spine3'

After doing this you can have any type of blending or procedural animations without sacrifice.
Beside of the blending problem, the default hierarchy forces the exporter to export some extra transformation data for translations. So doing this simple changes can help the exporter to optimize the keyframes better and with that you find more memory for your other works.

There is another problem about twist bones hierarchy. Assume that you added some upper arm twists to your rig. By default upper arm twists are children of the clavicle. This leads you to a problem when you want to add some procedural animations at run time on arms like inverse kinematics. If you make upper arm as their parent all the problems at run time will be solved. But there is last remaining problem, and that is max never save your changes to the twist bones hierarchy! so you have to make your custom changes to the twists every time you want to export the animation. I have written a simple callback script for this issue. It is triggered on the open file event. It checks if there are any biped in the scene and and make our desired changes to the twists.


Conclusion

Character studio have a weird  hierarchy. This hierarchy leads the animation to some problems while applying procedural animations and blending at runtime. Doing simple changes to it's hierarchy will help you to solve the problems during blending and procedural animations and also helps the exporter to perform a better keyframe optimization.

Intro

Hi all,

I'm a technical game developer specialized in animation, I work and study on both technical and artistic sides of real-time animations including ways to compute and optimize real-time animations, AI and Physics-Based animation, choreographed animation systems and sports bio-mechanics.
In my blog I talk about both technical and artistic sides of animation and their application in the video games.. I will categorize my posts in to three main classes:

1- Artistic animation: talking about principles of motion, some acting and aspects of 3D animating.

2- Technical Animation: These posts are related to technical animator's tasks, which techniques they apply and more.

3- Animation Programming: These posts are more technical and they talk about the various ways of computing and optimizing real-time animations.

I will tag the category of the post at the beginning of it, so you can select your favorite class and read just about what you need!