Simple bone animation and bone animation Theory

Source: Internet
Author: User

Simple bone animation and bone animation Theory
Keep in touch: zpxocivuby@126.com

Objective: To instruct others to use the Matrix
Task: Compile the tutorial and code
Intended audience: d3d beginners, C/C ++ proficiency, Windows Programming proficiency, VC ide proficiency
Description:
In this session, we will make a simple bone animation (very simple! There are only two line segments !) What is the implementation idea?

In the 3D world, there are two main ways to implement the animation process of a character:
1. Key Frame Animation (Key Frame Animation)
A) key frames to be played are all put into the storage medium (file, memory). During playback, a frame is triggered.
2. (bone animation)
A) it is to break down an action object into a pair of keys (joint), and bone (bone). To make an object action, you only need to perform matrix coordinate transformation between joint and bone.

The first method is not studied for the moment (I will introduce it again after I finish writing the skeleton animation). We only care about the second method.
Is the most basic skeleton construction mode.
Figure 1

The key columns include a, B, c, d, and A, B, and C. We assume that the coordinates of ABCD are a (, Z), B (, Z) C (, Z) D (, Z), we want to let the and C bones move, while B does not move

Figure 2

After the transformation, Figure 1 is changed to Figure 2. Now let's take a look at how to use mathematical expressions to express the above conversions. We set C> C' to r degree, and D> d' to remain unchanged in C. (what? Why does C remain unchanged? This ...... If you think about it by yourself), then the coordinates of C' are (x', y', Z), and d 'are (x', y '', Z ). What coordinates are calculated? You are really worried. Of course, it's through matrix transformation.

Calculation steps:
Step 1: We first think that C and D are the same, that is, when C moves, D also moves, in this way, we can calculate that the coordinates of C and D are all rotated around the point using a vector perpendicular to the plane of AC and CD, through the conversion matrix, we can obtain the vector coordinates of C 'D,
  

Step 2: Re-process the '''d to d' rotation. This is to regard the "c" Point as a turning point and calculate the vector coordinate of the d '.

Okay. Let's take a look at the code.

To facilitate the operation, I put the vertices variable in the setup function out to form a global variable (this is not a good method, but this is just a temporary action, I will correct it later)

Vertex * vertices;

In the program, we set the key (joint) to a point, and the bone (bone) is a line segment.
Note that there is only one piece of code, that is, the following statements in the display () function

// Four vertices are affirmed. Tempa, tempb, and tempc represent three vertices of ACD, tempd represents a temporary data storage space, it is mainly used to store the converted location data of C 'C '.
D3dxvector3 * Tempa = new d3dxvector3 (vertices [0]. _ x, vertices [0]. _ y, vertices [0]. _ z); // copy ABC to temp
D3dxvector3 * tempb = new d3dxvector3 (vertices [1]. _ x, vertices [1]. _ y, vertices [1]. _ z );
D3dxvector3 * tempc = new d3dxvector3 (vertices [2]. _ x, vertices [2]. _ y, vertices [2]. _ z );
D3dxvector3 * tempd = new d3dxvector3 ();

// Calculate the Ba
D3dxvector3 * Ba = & (* tempb-* Tempa); // calculates the output vector Ba
// Computing ca
D3dxvector3 * CA = & (* tempc-* Tempa); // calculates the vector ca

// Rotate the BA in the vertical Ba direction. Ax

D3dxmatrixrotationaxis (& Raba, (d3dxvec3cross (tempd, Ba, CA), ax); // This function is described earlier
// The function of the d3dxvec3cross function is to perform X (which cannot be read as "ekiss" or "Cross multiplication") on the BA vector and the CA vector, (a vector perpendicular to Ba AND Ca is obtained)
D3dxmatrixrotationaxis (& racb, (d3dxvec3cross (tempd, Ba, CA), ax );
D3dxvec3transformnormal (Ba, ba, & Raba); // the function of this function is to convert the BA vector to Ba' through the transformation matrix Raba'
D3dxvec3transformnormal (Ca, CA, & racb );

* Tempb = (* Ba + * Tempa );
// Re-copy the data in the memory
Vertices [1]. _ x = tempb-> X;
Vertices [1]. _ y = tempb-> Y;
Vertices [1]. _ z = tempb-> Z;
* Tempc = (* Ca + * Tempa );

// Calculate C 'B'
D3dxvector3 * cb = & (* tempc-* tempb );
// Rotate C' B 'in the vertical direction of C' B' ax
D3dxmatrixrotationaxis (& Raba, (d3dxvec3cross (tempd, Ba, CA),-Ax );
D3dxvec3transformnormal (CB, CB, & Raba );
* Tempc = (* Cb + * tempb );
Vertices [2]. _ x = tempc-> X;
Vertices [2]. _ y = tempc-> Y;
Vertices [2]. _ z = tempc-> Z;

Run it and see what the effect is!

Is it "naked"? (this is a word that I often say when I went to college. It means: It's really bad, it's an idiot, it's incompetent, it's just a blow, it's not a good thing. It was invented by our team. You are not authorized to use it. "^_^ ")

It is true that this code can deal with these two line segments, But what should we do if there are hundreds of thousands? It doesn't matter. We can refactor the code. Writing is more complex, and some Oo (Object-Oriented) methods, and design pattern (design pattern) are used, which will be introduced in the next field.

Preparation: It's so tiring to write these things. It's really tiring to come back to work and write things during the day and night, so I will take a break this week and write it again next week.

Download routine Forum Communication

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.