Chapter 2 matrix Mathematics (as3.0)

Source: Internet
Author: User
Tags sin cos

In this chapter, we will not introduce some new methods of motion, physics, or rendering graphics. What I want to introduce to you is the matrix.
(Matrix), which provides us with a new alternative.
    Matrices are frequently used in 3D system rotation, scaling, and translation (motion. In a variety of 2D
Graph conversion is also very common. You may recall that the begingradientfill method uses a matrix to set bits.
Set, size, and rotation ratio.
    In this chapter, we will see how to create a 3D matrix system to operate 3D films and see some
The built-in matrix in flash. I'm glad that no one mentioned Keanu Reeves so far,
A movie, especially the matrix. See how long I can stick to it.

Figure 18-1 A 3x3 matrix, a 1x3 matrix, and a 3x1 Matrix
    Matrices are usually described by some variables, such as M. In the matrix, we use
The column and column values are usually used as the script in the variables used. For example, if the 3x3 matrix in Figure 18-1 is m,
So m2, 3 is equal to 6 because it points to the second row and the third column.
    A matrix unit can contain not only simple numbers, but also formulas and variables. In fact, workbooks are
A large matrix. We can use a unit to save the sum of a column, and use another cell to multiply the sum by a minute.
And so on. We can see that such a matrix should be very useful.
Matrix Operations
   A Workbook is like a matrix of free combination. The matrices we want to process are more structured and can be used.
What to do and how to create Chengdu have their own rules.
   Most of the textbooks on Matrix mathematics I have seen only one of the two methods. First, the school is talking about matrix operations.
Details, the entire matrix used is almost all random numbers. We learn these rules, but we do not know why.
What are the results of these tasks. It's like playing a game where numbers are arranged in beautiful shapes.
   The second method is to describe the content of the matrix in detail but skip the manual operation. "multiply the two matrices to get this
 Such as the result... ..." Let readers not know how multiplication is calculated.
   To ensure that everyone can understand how the matrix works, I chose a method (compromise) that combines the two.
Start with some numeric matrices and describe how to perform matrix multiplication.
Matrix Addition
   A matrix is usually used to operate 3D points. A 3D point contains the x, y, and z coordinates. We can simply
Consider it as a matrix of 1x3:
   XYZ
Now let's assume that we want to move the point in space, or the translation of the point. We need to know how far each axis is moving. This
You can put it into a translation matrix. It is also a matrix of 1x3:
   DX dy DZ
Here dx, Dy, and DZ are the moving distance of each axis. Now we have to add the conversion matrix to the dot matrix. This is
Is matrix addition,Very simple. We only need to add the corresponding units to form a new one that contains each unit
And. Obviously, the size of the two matrices should be the same. The conversion method is as follows:
   X y z + dx dy DZ = (x + dx) (Y + dy) (Z + Dz)
The obtained matrix can be X1, Y1, Z1. After conversion, it contains the new coordinates of the point. Let's try it with a real number.
Assume that the positions on the X, Y, and Z axes are 100, 50, and 75 respectively. Move them to-10, 20, and-35 respectively. It should be
Such:
   100 50 75 +-10 20-35 = (100-10) (50 + 20) (75-35)
Therefore, when an addition operation is performed, the new coordinates of the obtained vertex are 90, 70, and 40. It's very simple, isn't it? Everybody
Xu has noticed the relationship between velocity, The speed on each axis is added to the corresponding position of another matrix. Public
Transaction.
   If we have a large matrix, we will continue to use the same method to match each unit. We will not go anywhere
It is greater than the matrix addition of 3 × 1, but I will give you an abstract example:
   ABC             Jkl       (A + J) (B + k) (C + l)
   Def       +   MnO     = (D + M) (E + n) (F + O)
   Ghi             Pqr       (G + p) (H + q) (I + r)
 The above is what we need to know about matrix addition. After introducing matrix multiplication, I will show you how
The number is used in the matrix 3D engine.
Matrix Multiplication
     Matrix Multiplication is widely used in 3D conversion and is often used for scaling and rotating.
In this book, we do not actually use 3D scaling, because the point scaling in the example does not have the "thickness" of 3D ",
Therefore, only two-dimensional scaling is required. Of course, you can create a more complex introduction that allows you to scale the entire 3D model.
. In this case, you need to write some functions to change 3D points based on the size of the new video. These are beyond the scope of our discussion.
However, since scaling is very simple and it is easy to implement using matrix multiplication, I will show you how
Example.
Scale by Matrix
   First, you need to know the existing width, height, and depth of an object-in other words, it is each of the three axes.
The size of the axis component. Of course, you can create a 3 × 1 matrix:
     WHD
We know that W, H, D represents width, height, and depth ). Scale the moment below
Array:
     SX00
    0Sy0
     00SZ
Here, Sx, Sy, and Sz correspond to the scaling ratio on the corresponding axis. They will all be scores or decimal places, 1.0 is 100%, 2.0 is
200%, 0.5 is 50%, and so on. Later we will see why the matrix is distributed in this form.
   To multiply two matrices, the number of columns in the first Matrix must be the number of rows in the other matrix.
Same. As long as this standard is met, the first matrix can have any number of rows, and the second matrix can have any number of columns.
In this example, the first Matrix has three columns (W, H, d), so the zoom matrix has three rows. Then how do they multiply
What about algorithm operations? Let's take a look at this mode:
          SX 0 0
   W h d * 0Sy 0
          0 0 SZ
The calculation result of the matrix is as follows:
    (W * SX + H * 0 + D * 0) (W * 0 + H * SY + D * 0) (W * 0 + H * 0 + D * sz)
Delete all numbers equal to 0:
    (W * SX) (H * Sy) (D * sz)
It is very logical because we multiply the width (x-axis component) by the X scaling factor, and the height by the y scaling factor,
Multiply the depth by the zooming factor. But what are we doing? All the numbers equal to 0 are covered,
So let's abstract this pattern more clearly.
      A BC
 Uvw * d EF
       GHI

Now we can see that the result of this mode is:
   (U * A + V * D + W * g) (u * B + V * E + W * H) (u * C + V * F + W * I)
We multiply the first row (U, V, W) of the first Matrix and the first element of each row of the second matrix. Add them together
The first element of the first row of the result is obtained. Use the same method in the second column (B, E, H) of the second matrix
The result of the second column is displayed.
    Repeat the above action in the second row to get the result of the second row:
   If the number of rows in the first matrix is greater than 1,
 U V W  A B C
 X y z * D E F
        G h I
The 3 × 2 matrix is obtained:
   (U * A + V * D + W * g) (u * B + V * E + W * H) (u * C + V * F + W * I)
   (X * A + y * D + z * g) (x * B + y * E + z * H) (x * C + y * F + z * I)
Now let's take a look at some of the actually used matrix multiplication-coordinate rotation. We hope that this scaling example will make it
It looks clearer.
Coordinate rotation using Matrices
First, we need to dig out our 3D dot matrix:
   X Y Z
It stores all coordinates of the point. Of course, there must be a rotation matrix. We can move on any of the three axes.
Line Rotation. We will create a matrix for each type of rotation. Start with the X axis rotation matrix:
   1  0    0
   0Cos Sin
   0 -SinCos
Here are some positive cosine values, "what are sin and COS ?" Obviously, this is the positive cosine of the angle from which we want to rotate.
Value. If this point is rotated by 45 degrees, the two values are the sine and Cosine values of 45.(Of course, you need
Using radians) Now let's multiply the matrix with a 3D point and look at the result.
       1    0    0
 X y z * 0  Cos Sin
       0   -Sin Cos
The result is as follows:
   (X * 1 + y * 0 + z * 0) (x * 0 + y * Cos-z * sin) (x * 0 + y * SIN + z * Cos)
The result is as follows:
   (X) (y * Cos-z * sin) (z * Cos + y * sin)
This sentence can be translated:
   X = X;
   Y = math. Cos (angle) * Y-math. Sin (angle) * z;
   Z = math. Cos (angle) * z + math. Sin (angle) * Y;
Recall Chapter 10. When discussing coordinate rotation, we will see that this is actually the coordinate rotation of the X axis. Don't be surprised
Matrix mathematics is just a way to observe and organize different formulas and equations. So far, you have to create a Y axis rotation
The matrix is very easy:
     Cos0Sin
      01 0
   -Sin 0Cos
Finally, the Z axis is rotated as follows:
     Cos sin0
  -Sin cos0
      0 0 1
This is a good attempt. Multiply the X, Y, and Z matrices by the units of each rotating matrix to prove
The coordinate rotation formulas of chapter 10 are identical.
Compile a Matrix
     Okay, now you have enough foundation to convert this knowledge into code. Next, we will
. This class has two methods: rotatex and rotatey to implement 3D sitting.
Label rotation. We want them to work in a matrix.
     Start with the rotatex function. It will use the x, y, and z coordinates of the ball, place them into the 1 × 3 matrix, and then create
Create an X rotation matrix with a given angle. This matrix is represented as an array. Use matrixmultiply
The function is used to multiply two matrices. Of course, you need to create this function! The matrix after multiplication must be saved using another array,
Because we need to save these values back to the X, Y, and Z coordinates of the ball. The method for the new version is as follows:
Private function rotatex (ball: ball3d, anglex: Number): void {
 VaR position: array = [Ball. xpos, ball. ypos, ball. zpos];
 VaR sin: Number = math. Sin (anglex );
 VaR cos: Number = math. Cos (anglex );
 VaR xrotmatrix: array = new array ();
 Xrotmatrix [0] = [1, 0, 0];
 Xrotmatrix [1] = [0, cos, sin];
 Xrotmatrix [2] = [0,-sin, cos];
 VaR result: array = matrixmultiply (Position, xrotmatrix );
 Ball. xpos = Result [0];
 Ball. ypos = Result [1];
 Ball. zpos = Result [2];
}
The following is a matrix multiplication function:
Private function matrixmultiply (matrixa: array, matrixb: array): Array {
 VaR result: array = new array ();
 Result [0] = matrixa [0] * matrixb [0] [0] +
 Matrixa [1] * matrixb [1] [0] +
 Matrixa [2] * matrixb [2] [0];
 Result [1] = matrixa [0] * matrixb [0] [1] +
 Matrixa [1] * matrixb [1] [1] +
 Matrixa [2] * matrixb [2] [1];
 Result [2] = matrixa [0] * matrixb [0] [2] +
 Matrixa [1] * matrixb [1] [2] +
 Matrixa [2] * matrixb [2] [2];
 Return result;
}
Now,The matrix multiplication function is a multiplication of the 1x3 and 3x3 matrices manually written.
Functions used in each example. You can also use the for loop to create more dynamic moments that can process any size.
Array functions, but now I want to keep the code concise.
     Finally, create the rotatey function. If you understand the rotatex function, this function should be very obvious.
You only need to create a y rotation matrix to replace the X rotation matrix.
Private function rotatey (ball: ball3d, angley: Number): void {
 VaR position: array = [Ball. xpos, ball. ypos, ball. zpos];
 VaR sin: Number = math. Sin (angley );
 VaR cos: Number = math. Cos (angley );
 VaR yrotmatrix: array = new array ();
 Yrotmatrix [0] = [cos, 0, sin];
 Yrotmatrix [1] = [0, 1, 0];
 Yrotmatrix [2] = [-sin, 0, cos];
 VaR result: array = matrixmultiply (Position, yrotmatrix );
 Ball. xpos = Result [0];
 Ball. ypos = Result [1];
 Ball. zpos = Result [2];
}
That's it. You can also create a rotatez function, because we don't actually need it in our example
I leave it as an exercise for everyone to complete.
     Now, run rotatexy. As, which looks the same as the version in Chapter 15th. As
                                      

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.