3D Graphics for Game Programming (J. Han) Chapter XI Character Animation.

39
Graphics for Game Programming (J. Han) Chapter XI Chapter XI Character Animation Character Animation

Transcript of 3D Graphics for Game Programming (J. Han) Chapter XI Character Animation.

Page 1: 3D Graphics for Game Programming (J. Han) Chapter XI Character Animation.

3D Graphics for Game Programming (J. Han)

Chapter XIChapter XI Character Animation Character Animation

Page 2: 3D Graphics for Game Programming (J. Han) Chapter XI Character Animation.

3D Graphics for Game Programming (J. Han) 11-22

Keyframe Animation in 2DKeyframe Animation in 2D

In the traditional hand-drawn cartoon animation, the senior key artist would draw the keyframes, and the junior artist would fill the in-between frames.

The similar applies to computer animation. For a 30-fps animation, for example, much fewer than 30 frames are defined per second. They are the keyframes. In real-time computer animation, the in-between frames are automatically filled at run time.

The key data are assigned to the keyframes, and they are interpolated to generate the in-between frames. Any data that change in the time domain can be interpolated.

Page 3: 3D Graphics for Game Programming (J. Han) Chapter XI Character Animation.

3D Graphics for Game Programming (J. Han)

Euler AnglesEuler Angles

2-33

Recall Euler angles and Euler transform.

Page 4: 3D Graphics for Game Programming (J. Han) Chapter XI Character Animation.

3D Graphics for Game Programming (J. Han) 11-44

Keyframe Animation in 3DKeyframe Animation in 3D

Keyframe animation in 3D: Seven teapot instances are defined by sampling the graphs seven times.

Page 5: 3D Graphics for Game Programming (J. Han) Chapter XI Character Animation.

3D Graphics for Game Programming (J. Han) 11-55

Keyframe Animation (cont’d)Keyframe Animation (cont’d)

Smoother animation may often be obtained using a higher-order interpolation.

Page 6: 3D Graphics for Game Programming (J. Han) Chapter XI Character Animation.

3D Graphics for Game Programming (J. Han) 11-66

A Problem of Euler AnglesA Problem of Euler Angles

A problem of Euler angles is that they are not always correctly interpolated.

Page 7: 3D Graphics for Game Programming (J. Han) Chapter XI Character Animation.

3D Graphics for Game Programming (J. Han) 11-77

QuaternionQuaternion

A quaternion is an extended complex number.

Conjugate

Magnitude (If the magnitude of a quaternion is 1, it’s called a unit quaternion.)

Page 8: 3D Graphics for Game Programming (J. Han) Chapter XI Character Animation.

3D Graphics for Game Programming (J. Han) 11-88

Quaternion (cont’d)Quaternion (cont’d)

Recall 2D rotation

Let us represent (x,y) by a complex number x+yi, and denote it by p. Given the rotation angle , the polar form of a unit-length complex number is

defined as cos+sini. Let us denote it by q. Then, we have the following:

Surprisingly, the real and imaginary parts of pq represent the rotated coordinates.

Page 9: 3D Graphics for Game Programming (J. Han) Chapter XI Character Animation.

3D Graphics for Game Programming (J. Han) 11-99

Quaternion (cont’d)Quaternion (cont’d)

3D rotation can be similarly described using quaternions. Consider rotating a 3D vector p about an axis u by an angle . Both the vector

to be rotated and the rotation are represented in quaternions. Vector p to a quaternion p

The rotation axis u and rotation angle define another quaternion q. (The axis u is divided by its length to make a unit vector u.)

Then, the rotated vector is equivalent to the imaginary part of .

Page 10: 3D Graphics for Game Programming (J. Han) Chapter XI Character Animation.

3D Graphics for Game Programming (J. Han) 11-1010

Quaternion (cont’d)Quaternion (cont’d)

Proof

Page 11: 3D Graphics for Game Programming (J. Han) Chapter XI Character Animation.

3D Graphics for Game Programming (J. Han) 11-1111

Quaternion (cont’d)Quaternion (cont’d)

Consider two unit quaternions, p and q, which represent rotations. They can be interpolated using parameter t in the range of [0,1]:

This is called spherical linear interpolation (slerp). If quaternions are defined for the keyframes, they can be interpolated for the

in-between frames.

Page 12: 3D Graphics for Game Programming (J. Han) Chapter XI Character Animation.

3D Graphics for Game Programming (J. Han) 11-1212

Quaternion (cont’d)Quaternion (cont’d)

Proof

Page 13: 3D Graphics for Game Programming (J. Han) Chapter XI Character Animation.

3D Graphics for Game Programming (J. Han) 11-1313

Quaternion (cont’d)Quaternion (cont’d)

A quaternion q representing a rotation can be converted into a matrix form. If q = (qx,qy,qz,qw), the rotation matrix is defined as follows:

Conversely, given a rotation matrix, we can compute the corresponding quaternion.

Page 14: 3D Graphics for Game Programming (J. Han) Chapter XI Character Animation.

3D Graphics for Game Programming (J. Han) 11-1414

Quaternion (cont’d)Quaternion (cont’d)

Proof

Page 15: 3D Graphics for Game Programming (J. Han) Chapter XI Character Animation.

3D Graphics for Game Programming (J. Han) 11-1515

Quaternion (cont’d)Quaternion (cont’d)

Summary A 3D rotation is represented in a quaternion. Quaternions are well interpolated through spherical linear interpolation. A quaternion can be converted into a rotation matrix.

Page 16: 3D Graphics for Game Programming (J. Han) Chapter XI Character Animation.

3D Graphics for Game Programming (J. Han) 11-1616

Quaternion (cont’d)Quaternion (cont’d)

Page 17: 3D Graphics for Game Programming (J. Han) Chapter XI Character Animation.

3D Graphics for Game Programming (J. Han) 11-1717

SkeletonSkeleton

A popular method for animating a character is to use a skeleton, which is composed of bones. The skeleton is embedded into the polygon mesh. When the skeleton is animated, the vertices of the polygon mesh will be accordingly animated.

Page 18: 3D Graphics for Game Programming (J. Han) Chapter XI Character Animation.

3D Graphics for Game Programming (J. Han) 11-1818

Skeleton (cont’d)Skeleton (cont’d)

Skeleton editing and embedding The skeleton template such as 3ds Max biped is positioned in the

default pose. The bones are edited. The skeleton is made to fit to the polygon mesh.

Page 19: 3D Graphics for Game Programming (J. Han) Chapter XI Character Animation.

3D Graphics for Game Programming (J. Han) 11-1919

Skeleton (cont’d)Skeleton (cont’d)

Page 20: 3D Graphics for Game Programming (J. Han) Chapter XI Character Animation.

3D Graphics for Game Programming (J. Han) 11-2020

Skeleton (cont’d)Skeleton (cont’d)

Page 21: 3D Graphics for Game Programming (J. Han) Chapter XI Character Animation.

3D Graphics for Game Programming (J. Han) 11-2121

Space Change between BonesSpace Change between Bones

The bones are connected at joints, which allow the rigid skeleton to animate in an articulated fashion. See the example, which consists of three bones (upper arm, forearm, and hand) and three joints (shoulder, elbow, and wrist).

When the forearm moves, for example, vf has to move accordingly. It is simply achieved if vf is defined in the forearm's object space.

Initially, all vertices of the default pose are defined in the object space of the character. To avoid confusion with the object space of a bone, let’s use the term, world space, instead of the character’s object space.

Every world-space vertex of the default pose needs to be transformed into the object space of the bone (which we call bone space henceforth) it belongs to. For example, vf will be transformed into the forearm's bone space so as to have the coordinates (2,0).

Page 22: 3D Graphics for Game Programming (J. Han) Chapter XI Character Animation.

3D Graphics for Game Programming (J. Han) 11-2222

Space Change between Bones (cont’d)Space Change between Bones (cont’d)

For now, consider the opposite direction, i.e., from the bone space to the world space. If we can compute such a bone-to-world transform, its inverse can be used to convert a world-space vertex into the bone space.

Consider the to-parent transform of the forearm, which transforms a forearm vertex to the space of its parent.

Now consider the to-parent matrix of the hand.

Given a vertex at a bone, we can concatenate the to-parent matrices so as to transform the vertex into the bone space of any ancestor in the skeleton.

Page 23: 3D Graphics for Game Programming (J. Han) Chapter XI Character Animation.

3D Graphics for Game Programming (J. Han) 11-2323

World Space to Bone SpaceWorld Space to Bone Space

The root node (pelvis) is associated with a transform used to position and orient it in the world space for the default pose. It is an identity matrix because the world space is in fact the object space of the character. Let us denote the world matrix by M1,d.

The spine's world transform

The clavicle’s world transform

Let’s generalize

Page 24: 3D Graphics for Game Programming (J. Han) Chapter XI Character Animation.

3D Graphics for Game Programming (J. Han) 11-2424

World Space to Bone Space (cont’d)World Space to Bone Space (cont’d)

So far, we have considered the world transform from the bone space to the world space. However, what is needed in an articulated-body animation is its inverse.

Once the default pose is fixed, the inverse world transforms can be computed for all bones. In the default pose, Mi,p

-1 can be immediately obtained.

Computing Mi,d-1 requires Mi-1,d

-1 to be computed in advance, and therefore the skeleton hierarchy is travdersed top down.

Page 25: 3D Graphics for Game Programming (J. Han) Chapter XI Character Animation.

3D Graphics for Game Programming (J. Han) 11-2525

Forward KinematicsForward Kinematics

The inverse of the world transform converts a world-space vertex “in the default pose” to the i-th bone's space.

Now the i-th bone is animated. Then, the vertices belonging to the bone are accordingly animated. The animation is often called local transform.

For rendering, the animated vertices should be transformed back to the world space. (Then, they will be transformed to the camera space and so forth, along the pipeline.) Let us call the transform matrix M5,w.

Page 26: 3D Graphics for Game Programming (J. Han) Chapter XI Character Animation.

3D Graphics for Game Programming (J. Han) 11-2626

Forward Kinematics (cont’d)Forward Kinematics (cont’d)

As the first step for computing the world-space position of “animated v5,” let us find its coordinates in the upper arm's space.

The upper arm can also be animated. Let M4,w denote the matrix that transforms the animated vertices of the upper arm into the world space. Then, M5,w for transforming “animated v5” into the world space is defined.

Let’s generalize.

Page 27: 3D Graphics for Game Programming (J. Han) Chapter XI Character Animation.

3D Graphics for Game Programming (J. Han) 11-2727

Forward Kinematics (cont’d)Forward Kinematics (cont’d)

When the artist defines the animated pose of the i-th bone, Mi,l is obtained. Mi,p was obtained from the default pose. So, computing Mi,w simply requires Mi-1,w to be computed in advance. M1,w representing the pose of the animated pelvis is defined by the artist. We can compute the world transforms of all bones “in the animated pose”

also in the top-down fashion.

When vd and vw denote the world-space vertices “in the default pose” and “in the animated pose,” respectively, we have the following relation:

Page 28: 3D Graphics for Game Programming (J. Han) Chapter XI Character Animation.

3D Graphics for Game Programming (J. Han)

When v2 belongs to the forearm, Mf,wMf,d-1 produces v2´. Similarly, v1´ and v3´

can be produced.

In skinning, v2 is transformed not only by Mf,wMf,d-1 but also by Mu,wMu,d

-1. Then, the transformed vertices are interpolated using the predefined weights. The same applies to v1 and v3.

11-2828

SkinningSkinning

Page 29: 3D Graphics for Game Programming (J. Han) Chapter XI Character Animation.

3D Graphics for Game Programming (J. Han) 11-2929

Skinning (cont’d)Skinning (cont’d)

Suppose that w5 and w6 are equal.

Page 30: 3D Graphics for Game Programming (J. Han) Chapter XI Character Animation.

3D Graphics for Game Programming (J. Han) 11-3030

Skinning in Keyframe AnimationSkinning in Keyframe Animation

Recall the following: The local transform Mi,l is an affine transform (usually a rotation). The to-parent transform Mi,p is a space-change matrix composed of a translation

and a rotation. Let us combine Mi,l and Mi,p into a single matrix and name it Mc.

Its upper-left 3x3 sub-matrix represents a combined rotation. Its fourth column represents a combined translation.

For a keyframe, the rotation part of Mc is stored as a quaternion, and the translation is stored as a vector. They form the key data “of a bone.”

For an in-between frame, both the quaternions and translational vectors of the keyframes are interpolated. The interpolated quaternion is converted into a matrix. The interpolated translation vector fills the fourth column of the matrix,

which can then be called the “interpolated Mc.” It is combined with the parent bone's world transform Mi-1,w so as to

complete Mi,w. (For this purpose, top-down traversal is needed.)

Page 31: 3D Graphics for Game Programming (J. Han) Chapter XI Character Animation.

3D Graphics for Game Programming (J. Han) 11-3131

Skinning in Keyframe Animation (cont’d)Skinning in Keyframe Animation (cont’d)

The following CPU pseudo computes Mi, which is Mi,wMi,d-1, and store it in

the matrix palette.

Page 32: 3D Graphics for Game Programming (J. Han) Chapter XI Character Animation.

3D Graphics for Game Programming (J. Han) 11-3232

Skinning in Keyframe Animation (cont’d)Skinning in Keyframe Animation (cont’d)

Page 33: 3D Graphics for Game Programming (J. Han) Chapter XI Character Animation.

3D Graphics for Game Programming (J. Han) 11-3333

Skinning in Keyframe Animation (cont’d)Skinning in Keyframe Animation (cont’d)

The indices of MatrixIndex are three times the actual indices, e.g., not (0,2,7,18) but (0,6,21,54).

Page 34: 3D Graphics for Game Programming (J. Han) Chapter XI Character Animation.

3D Graphics for Game Programming (J. Han) 11-3434

Skinning in Keyframe Animation (cont’d)Skinning in Keyframe Animation (cont’d)

Page 35: 3D Graphics for Game Programming (J. Han) Chapter XI Character Animation.

3D Graphics for Game Programming (J. Han) 11-3535

Motion CaptureMotion Capture

Page 36: 3D Graphics for Game Programming (J. Han) Chapter XI Character Animation.

3D Graphics for Game Programming (J. Han) 11-3636

Inverse KinematicsInverse Kinematics

In robotics, an end effector refers to the device located at the end of a robotic arm.

Forward kinematics computes the pose of the end effector as a function of the joint angles of the robotic arm.

The reverse process is called inverse kinematics (IK). Given the desired pose of the end effector along with the initial pose of the robotic arm, the joint angles of the final pose are calculated.

The number of independent variables defining the state of an object is called the degrees of freedom (DOF).

Page 37: 3D Graphics for Game Programming (J. Han) Chapter XI Character Animation.

3D Graphics for Game Programming (J. Han) 11-3737

Inverse Kinematics - Analytic SolutionInverse Kinematics - Analytic Solution

Page 38: 3D Graphics for Game Programming (J. Han) Chapter XI Character Animation.

3D Graphics for Game Programming (J. Han) 11-3838

Inverse Kinematics - Cyclic Coordinate DescentInverse Kinematics - Cyclic Coordinate Descent

Page 39: 3D Graphics for Game Programming (J. Han) Chapter XI Character Animation.

3D Graphics for Game Programming (J. Han) 11-3939

Inverse Kinematics - ExampleInverse Kinematics - Example