Direct3d Learning (6): animation BASICS (1) animation and time in motion

Source: Internet
Author: User
Time-based actions
Time unit: Ms
A slow computer can ensure the animation speed by dropping frames.

Read time in Windows
Use the timegettime () function. For details, see msdn.
You can use static variables to store the time at the beginning of the function:

Void frameupdate ()
{
Static DWORD lasttime = timegettime ();


Time-related Animation
Record the animation start time, and then store the offset of each frame relative Start Time
Key frame format, including time and transformation matrix:

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

Elapsedtime is used to determine which frame to use.
The calculated value ranges from 0 ~ A scalar value between 1 to determine the position of the current time relative to the two key frames before and after
Float scalar = (time-keyframes [keyframe]. Time)/timediff;
The scalar value can be used to calculate the current transformation matrix:

// Calculate the difference in Transformations
D3dxmatrix matint =
D3dxmatrix (keyframes [keyframe2]. mattransformation )-
D3dxmatrix (keyframes [keyframe]. mattransformation );
Matint * = scalar; // scale the difference

In this way, the animation can be played smoothly.

Time-related motion
Generally, the motion is relative to the displacement, so we need to calculate it:

Float calcmovement (DWORD elapsedtime, float pixelspersec)
{
Return (pixelspersec/1000.0f * (float) elapsedtime );
}


Orbital motion
That is, the movement along the predetermined track is not controlled by the user.
Straight Track
This is simple. You can find the starting point, ending point, and the current scalar value:

D3dxvector3 vecpos = (vecend-vecstart) * scalar + vecstart;


Curve track
The curve is not just an arc. Here we use a wider range of curves:

We need four points to determine the direction and bending degree of the curve.
So how is it drawn? See the following figure:

Divide each line segment equally. The more parts, the smoother the curve. Then connect the Split points like this:

The beaier curve is drawn like this, Hoho ~
There is a formula for calculating the position. We will not discuss how to deduce it here. For more information, see:

P0 ~ P3 is the four control points, and s is the scalar value.

Define path
A complex path is a combination of a series of tracks, not only a straight line or a curve, but also a combination of the two.

It can be defined as a form similar to a linked list:

Enum {path_straight = 0, path_curved };
Typedef struct {
DWORD type; // line or curve
D3dxvector3 vecstart, vecend; // straight point and end point
D3dxvector3 vecpoint1, vecpoint2; // if it is a curve, this is the two control points in the middle
} Spath;


It's cool to use the path to move the role and other related objects in the game, for example, your so are drifting with the wind
One of the important applications is to control the camera to achieve various Lens Effects in movies.

Example,Nnd, compilation failed! No more

Reference:
Advanced animation with DirectX

 

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.