Animation Technology-keyframe)

Source: Internet
Author: User
Animation Technology-keyframe)

Key Frames are a commonly used animation technology. The basic principle is to extract key frames from the animation sequence, and other frames are computed by interpolation of these key frames based on time.

A simple key frame structure:

Typedef struct skeyframe {
DWORD time;
D3dmatrix mattransformation;
} Skeyframe

This is a very simplified structure that only contains the time and matrix. However, from this structure, we can see the most fundamental meaning of the keyframe technology. Suppose we want to create an animation for square rotation and movement.

The square rotates and moves between the three positions. We need to define four key frames. Each key frame uses a matrix to set the deflection and offset of the square relative to the initial position.
Skeyframe keyframes [4] = {
{0, 1.00000f, 0.00000f, 0.00000f, 0.00000f,
0.00000f, 1.00000f, 0.00000f, 0.00000f,
0.00000f, 0.00000f, 1.00000f, 0.00000f,
0.00000f, 0.00000f, 0.00000f, 1.00000f ;},

{400, 0.000796f, 1.00000f, 0.00000f, 0.00000f,
-1.00000f, 0.000796f, 0.00000f, 0.00000f,
0.00000f, 0.00000f, 1.00000f, 0.00000f,
50.00000f, 0.00000f, 0.00000f, 1.00000f ;},

{800,-0.99999f, 0.001593f, 0.00000f, 0.00000f,
-0.001593f,-0.99999f, 0.00000f, 0.00000f,
0.00000f, 0.00000f, 1.00000f, 0.00000f,
25.00000f, 25.00000f, 0.00000f, 1.00000f ;},

{1200, 1.00000f, 0.00000f, 0.00000f, 0.00000f,
0.00000f, 1.00000f, 0.00000f, 0.00000f,
0.00000f, 0.00000f, 1.00000f, 0.00000f,
0.00000f, 0.00000f, 0.00000f, 1.00000f ;}
};

Keyframe4 has the same time as keyframe1, so keyframe4 must be defined.
When updating an animation, you must first calculate the two key frames of the current frame based on the time when the animation has been played.
DWORD keyframe = 0; // start at 1st keyframe
For (DWORD I = 0; I <4; I ++ ){
// If time is greater or equal to
// Key-frame's time then update
// Keyframe to use
If (Time> = keyframes [I]. Time)
Keyframe = I;
}
If the current time is greater than or equal to the time specified for a key frame, the key frame is the first key frame of the current frame. The next frame is the next key frame. If the previous key frame is already the last key frame, the latter key frame also uses this frame.
DWORD keyframe2 = (keyframe = last_keyframe_id )? Keyframe: keyframe + 1;
After two key frames are calculated, interpolation is performed based on the current time and the time of the two key frames to obtain the current frame.
DWORD timediff = keyframes [keyframe2]. Time-
Keyframes [keyframe]. time;
// Make sure there's a time difference
// Avoid divide-by-zero errors later on.
If (! Timediff)
Timediff = 1;
Float scalar = (time-keyframes [keyframe]. Time)/timediff;
// Calculate the difference in Transformations
D3dxmatrix matint =/
D3dxmatrix (keyframes [keyframe2]. mattransformation )-/
D3dxmatrix (keyframes [keyframe]. mattransformation );
Matint * = scalar; // scale the difference
// Add scaled transformation matrix back to 1st keyframe Matrix
Matint + = d3dxmatrix (keyframes [keyframe]. mattransformation );

Matint is the conversion matrix of the calculated current frame. This matrix can be used to obtain the animation position of the current frame.
This example is the simplest Key Frame Animation, because the animation frame only contains a positive shape, that is, the mesh of each frame of the animation is the same. In a slightly more complex situation, such as the animation of a person moving around. You can select several actions as the key frame. If a simple animation is used, each key frame records a set of mesh information. If a skeleton animation is used, each key frame records the transformation of the skeleton. In these cases, interpolation is much more complicated. However, the current frame is still computed by interpolation based on time.

In addition to playing animations, the key frame technique can also be used for object motion. Especially for moving objects with fixed routes.

Although we talk about the key frame technology in 3D animation, it can also be used well in 2D games. In fact, this simple example can be seen as an example of a 2D animation key frame, but we only need to use a 2D conversion matrix.

References: <advanced animation with DirectX> Chapter2

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.