Learn Unity while drinking coffee-Chapter 2 Preparation Knowledge System (2) Learn unity

Source: Internet
Author: User

Learn Unity while drinking coffee-Chapter 2 Preparation Knowledge System (2) Learn unity

What is a matrix?

A rectangular table with n numbers arranged in m rows and n columns is called an mxn matrix.


Row vector and column vector

We can regard a vector as a special matrix, that is, a special matrix with only one row or only one column. Therefore, we call a vector with only one row as a row vector, and a vector with only one column as a column vector.

Note that when storing the matrix, our program stores the number of mn in a linear table. Therefore, there is a priority principle to obtain the elements in the column number of the row in a matrix. Unity has the following sentence in the Matrix4x4 class manual:

Matrices in unity are column major. data is accessed as: row + (column * 4 ). matrices can be indexed like 2D arrays but in an expression likemat [a, B], a refers to the row index, while B refers to the column index (notethat this is the opposite way round to Cartesian coordinates ).

The matrix in Unity uses column priority. Data is obtained by row + (column * 4. A matrix can be indexed in a format similar to a two-dimensional array, but it must be an expression like mat [a, B. Where a represents the row number, and B represents the column number (note that this is exactly the opposite of the Cartesian coordinate system ).


Mathematical Expression of row vectors and column vectors:




Zero Matrix

All elements are 0 matrices.

Unit Matrix

The following matrix is called the unit matrix.



It has obvious characteristics: the element where the row number and column number in all elements are equal is 1, otherwise it is 0, that is, a diagonal line from top left to bottom right is 1, and all other elements are 0 matrices. The particularity of the unit matrix will be mentioned later.

Equal Matrix

Same DimensionAndAll elements are equalThe two matrices are considered to be two equal matrices.

Matrix addition, matrix subtraction, Matrix Multiplication

These three operations are relatively simple, so they are written together. They represent the number addition and subtraction matrices on the corresponding columns. If a number is multiplied by a matrix, all elements are multiplied by this number to obtain a matrix.

 

Transpose matrix



Matrix Multiplication

Matrix Multiplication is special. When we multiply a matrix, we first ensure that this process is meaningful. The number of rows in the left multiplication matrix of a meaningful premise matrix multiplication is equal to the number of columns in the right multiplication matrix.

Mathematical process: the number of columns in matrix a is equal to the number of rows in matrix B. Then, the elements in column j of row I in matrix c are obtained by multiplication.


Because only the number of columns of a is equal to the number of rows of B, we can imagine that a 4-element row vector has 4 columns, so he can take a 4x4 matrix on the left. A 4-element column vector has four rows, so he can right multiply by a 4x4 matrix. Because the row vector and the column vector seem to be different in writing form, there is actually a transpose relationship between the row vector and the column vector.

Matrix Multiplication does not meet the exchange law, but when multiplication is meaningful:

In matrix operations, a vector can be multiplied by a matrix in two ways: the left multiplication of a row vector and the right multiplication of a column vector.

There is no big difference between the left multiplication of a row vector and that of a column vector in essence. It is only because of the transpose relationship between the row vector and the column vector that each game engine has its own writing habits and aesthetic preferences. DirectX uses the left multiplication of the row vector, while OpenGL uses the right multiplication of the column vector.


Inverse Matrix

A matrix has an inverse matrix. First, a condition must be met: This matrix is a non-singular matrix or a full-rank matrix, or the determinant of this matrix is less than 0.

Matrix a that meets the condition has an inverse matrix B, so that AB = the unit matrix I.

As


Spatial transformation

After learning about coordinate systems, vectors, and matrices, we can start to understand spatial transformations, which cover most of the fields related to image development.

We have made a preliminary understanding of the definition, characteristics, and operation rules of vector matrices. However, linear algebra does not tell us the geometric meaning of vectors and matrix operations. This book will guide readers to explore the geometric meaning of vector matrix operations.

Matrices can perform any kind ofliner transform.

In general, a matrix can describe any linear transformation. The spatial transformation that satisfies two points is called linear transformation: the origin does not move, and the parallel relationship between a line segment and a straight line remains unchanged. In other words, linear transformation may stretch the coordinate system, but will not distort the coordinate system.

For the rotation and scaling of any image, it is a linear transformation. The parallel relationship between line segments does not change, and the origin does not move. The translation transformation is not a linear transformation. No matter which point is used as the coordinate system origin before the transformation, the transformed origin must have moved.

Therefore, linear transformation is a transformation that may change the length of a line segment, the angle between the line segments, the area and volume of the image.

Now we are happy to ask: how does the matrix complete the transformation? How can nxn numbers turn our coordinate system upside down?

Let's first expand our imagination from a two-dimensional space.

Suppose we have an image in the Cartesian coordinate system of the plane. The four vertices are (), respectively ).

Then assume there is such a 2x2 matrix:

What does this image mean when we transform it with such a 2x2 matrix?

In fact, this matrix can be seen as two row vectors p = [2, 1], q = [-1, 2]. We plot the two vectors in the Cartesian coordinate system of the plane:




In fact, this 2x2 matrix represents the transformation of the original X axis to the direction where the p vector is sitting, and the transformation of the original Y axis to the direction where the Q vector is located. Since the coordinate system origin does not move, this is a linear transformation. In essence, this is a Rotation Transformation. We can stretch out our left hand, point the thumb to x, point the index to y, and then imagine placing any shape between two fingers, then the thumb is changed from the original direction to the p direction, and the index finger is changed from the original direction to the Q-square image. This process is the transformation process described in this matrix. The original image is rotated 26 ° counterclockwise.

-->





It is not just a rotation, but the side length of our image changes from 1. Therefore, this matrix also stretched our graph.

In this example, the vertical cut length of p and q is equal, so a square is formed. In fact, in a more general case, they constitute a parallelogram. This parallelogram is called a deflection box ". Two-dimensional deflection boxes we can use two fingers to draw, and three fingers to draw a three-dimensional deflection box. Therefore, the reader tries to imagine a linear transformation deflection box in the 3x3 matrix.

Let's look back at our definition of linear transformation: the parallel relationship between line segments does not change, and the origin does not move. It can be seen that the 2x2 matrix and the 3x3 matrix can express linear transformations in 2D and 3D spaces.

Taking a closer look at the above transformation, assuming we rotate the paper on the right to make the image "positive" again, we rotate the Paper 26 ° clockwise, So we throw a concept:

We rotate an image or object at a certain angle, which is equivalent to rotating the coordinate system at a negative value at this angle.





Here we can see a very, very, very obvious rule, that is, the place where cos appears in the transformation matrix must be where the row and column numbers are equal (a diagonal line ), sin may appear in areas with different row and column numbers.

The three coefficients in the scaling matrix correspond to the degree of zooming on the three axes of the original image.

 

A special linear transformation is called an image. For example, the transformed image is displayed on the other side of the Y axis as the mirror.


Shear transformation

After talking about the linear transformation, let's look at the shear transformation, which is a special transformation. The transformation process is a "distorted" coordinate system. In this process, the reader is expected to view the relevant information on his own.

So far, we have studied the Matrix Expression and geometric significance of linear transformation. In space mathematics, we think that the multiplication of coordinates and matrices is equal to the transformation itself.

Is there any doubt in the reader that translation transformation is not a linear transformation, so can it be expressed using a matrix?

The answer is yes, but there is a condition: the translation transformation in the n-dimensional space cannot be expressed using nxn or a smaller matrix, at least (n + 1) x (n + 1). Let's see why?

For linear transformation matrices in 2D 3D, we split them into vector bases p, q/p, q, and r equivalent to the number of coordinate axes. We can understand that the linear transformation process is to transform the unit vectors on the corresponding coordinate axis to the vector base represented by the matrix. Its ProcessYesTranslation is not included.

So how can we express translation?

We mentioned above that the multiplication of the coordinate and matrix is equal to the transformation itself, so we assume that the line segments composed of two coordinates, write it as two row vectors [1] [2 2], and multiply it to the left by a 2x2 unit matrix (we think the 2x2 unit matrix is an x, in the y direction, the scale matrix with a scaling factor of 1 is used to complete linear transformation:


[1 1] The transformed result is still [1 1], and [2 2 2] is the same. The line segment (image) is not altered.

Modify the transformation matrix randomly: Modify the value on the diagonal line of [1], and find that the transformation process produces proportional scaling. The value on the diagonal line is not modified, and it is found that the conversion process produces a rotation.

In addition, there is no other place in the 2x2 matrix that allows us to modify the possibility of generating a translation transformation. So we have to come up with a bold idea: Can we get a matrix containing a larger number to complete the transformation, so that the matrix can have a place for us to modify its value to make the source image undergo a nonlinear transformation?

The answer is yes. In the cognitive process, changes and sublimation of thinking are essential. So the general idea of genius was proposed: we use a 3-dimensional matrix and vector to express transformations and points in a 2-dimensional space.

Let's take a look at [1 1] after the point [1 1] expands a dimension, we have the following requirements for the point coordinates after the extension:

This is not a point in a three-dimensional space. It is still a point in a two-dimensional space [1. The third dimension is called w. We can't imagine the meaning of this [1 1] point. We can only see the shadow (projection) in the 2-dimensional space ), we can imagine this shadow in turn. For vertices [1 1], [2 2 2], [3 3 3 3], they are projected to the point [1 1] in two-dimensional space, the projection process divides the first two dimensions by the third dimension w. When w = 0, division is meaningless. Therefore, we assume that [x, y, 0] indicates thatInfinite points.

This space is called the three-dimensional space. The coordinates in the form of [x, y, w] are called the three-dimensional homogeneous coordinates of the changed two-dimensional coordinates. Similarly, our 2-dimensional unit matrix corresponds to a 3-dimensional homogeneous unit matrix when w = 1.


We use the left multiplication expression transformation of the homogeneous row vector and the homogeneous unit matrix again:

[X y w]. At this time, the number of columns in the left matrix is equal to the number of rows in the right matrix, and the multiplication still makes sense.

Let's observe again, is there any place in the homogeneous matrix that can make our coordinate system translate?




We do not have to try the data items of the central 2D matrix in the homogeneous matrix. We try to modify the multiple abcd.

[X y w] = [x + a, y + B, w] (in the result matrix :)

An incredible thing happened. Because of our definition of homogeneous space, any point [x, y, w], x/w, the two vertices y/w are considered to be the same point. So we can see that this transformation matrix converts any point in the Original Coordinate System [x y] to [x + a y + B]! That is, all graphs and line segments translate a in the x direction and B in the y direction.

Conclusion: For images in n-dimensional space (Coordinate System), the nxn matrix can express linear transformation. By introducing n + 1-dimensional homogeneous space, we can use n + 1-dimensional homogeneous matrix multiplication to express the translation transformation. The original linear transformation can still express the linear transformation in the original corresponding position of the homogeneous matrix.

Therefore, the homogeneous matrix combines linear transformation and translation transformation. The transformation between linear transformation and translation transformation is calledAffine Transformation.

In other words, an nxn matrix can express any n-1 dimensional space's affine transformation.

 

Then we can obtain the translation matrix in the 3D space, which is obviously a 3 + 1 = 4-dimensional matrix:



We can go back to the geometric layer. We think that it is meaningful to translate the meaningful amount of the position. We do not have the concept of position, so it is meaningless to translate. The most typical is vertices and vectors. Although we have mentioned that vectors can be expressed by endpoints, vertices in a narrow sense have positions, while vectors only have sizes and directions. Therefore, we cannot perform translation transformation on a narrow vector.

 

Perspective Projection Transformation and Projection Matrix

Let's briefly mention the projection transformation. A projection transformation usually occurs when an object in a three-dimensional space is projected onto a plane (sunlight projects the shadow from the human being to the wall ). The simplest three planes of A Three-dimensional plane are x = d, y = d, and z = d. Each of them is a plane located at d from the dot.

Assume that a three-dimensional image is projected to the z = d plane, then any homogeneous point [x y z] is projected to the z = d plane, and the obtained coordinate points should be: [x y z]/(z/d) = [x y d] removes z to obtain a two-dimensional space (image ). therefore, we construct a homogeneous space where w is 1, and [x y z 1] After projection becomes [x y z/d], then the matrix is constructed as follows:





Any point [x y z 1] is multiplied by this matrix to obtain [x y z/d]. [x y z/d] returns to the original space and is [x y d]. This matrix is the projection matrix projected to the z = d Plane in a three-dimensional space.

 

 

Azimuth and angle displacement

After learning some basic knowledge about vectors and matrices, let's ask ourselves one question:

Does a vector have an angle displacement? Does an object have an angle displacement?

We know that the vector only has the size and direction. Even if you want to force a vector to rotate around itself, the vector size and direction remain unchanged after rotation. We think that the vector is still the vector. But if we let an object rotate around itself, for example, turning a person over, do we still think it is the same? Due to the gravity of the Earth, the people who are down can obviously feel that their status has changed. This kind of change is not just the displacement of a person from somewhere else. Physically, we say this person has an angle displacement.

We call the degree of rotation an angle displacement. Displacement is often used to express the position of an object, and displacement is often used to express the object'sLocation.

We know that the displacement in the n-dimensional space can be expressed by n numbers. For example, if the point in a two-dimensional space Moves 5 toward x and 10 toward y, the displacement can be expressed using five or ten digits.

How can we express the angle displacement in a three-dimensional space?

 

Matrix Expression angle displacement

We can construct a three-dimensional Rotation Transformation Matrix to express the angle displacement in the Three-dimensional space (this matrix we have already given ). This expression is very straightforward, but not concise. Let's take a look at the advantages and disadvantages of expressing angle displacement in a matrix.

Advantage: immediate; more suitable for the underlying image API for direct use; when multiple angular connections, you only need to multiply them consecutively to get the final angular displacement; simply transpose the Matrix to obtain the inverse of the angle displacement;

Disadvantages: occupying more space (3-dimensional space requires nine numbers to express the angle of rotation); obscure (I think readers should have some experience ); redundant data and matrix creep may occur.

 

Euler Angle expresses the Angle displacement.

Shenma? We simply describe the rotation of an object and actually want to move out a 3x3 matrix? We cannot bear it! As mentioned at the beginning of this chapter, ouarla is on the stage. He was proposed by the famous mathematician Euler and named after him. Let's take a look at what an angle is, and what is amazing about it:

The orah angle splits the rotation of a three-dimensional object into three vertical axes. Euler did not tell us which three axes are decomposed in what order, but what makes the most sense is that the three axes in a three-dimensional coordinate system are rotated in a certain order. There are many conventions in this order. Here we will mention the "heading-pitch-bank" convention.

That is, in the left-hand coordinate system, we continue to stretch out the left hand, and the three fingers are perpendicular to each other. We get the left-hand coordinate system we mentioned above. The index finger points to the y + direction, which is called heading, we call the bank as the number of z+ directions to which the middle finger points, and the number of z+ directions to which the thumb points becomes pitch.

Therefore, we divide the arbitrary rotation of an object into three rotation quantities and obtain three angles respectively. We can describe almost all the rotations in a sequence composed of three numbers! However, oarla can't express all the rotations. The most famous phenomenon is the deadlock of universal joints. That is, when pitch = +-90 °, heading and bank are both vertical, thus reducing the degree of rotation. So far, there is no good solution to this problem.

 

Advantages: it is easy to use; it occupies the least amount of space; it is concise; all the angular angles expressed by the orah are legal.

Disadvantages: Non-uniqueness (360 ° and 0 °, although the value has changed, but the orientation of the expression is not changed); the most difficult interpolation (it is difficult to perform interpolation in the middle of two orah angles, this is very annoying when we add animations); Vientiane lock problem.

 

 

Next we will understand the last way to express the angle displacement:

 

Quaternion

For readers to learn more about the concept of the Quaternary element, we only propose that the Quaternary element is essentially a geometric interpretation of the complex numbers (a, B, c, d.

The plural (a, B, c, d) has a real part a, and the three virtual parts are B, c, and d respectively. The complex number can be used for addition, subtraction, dot multiplication, and cross multiplication. It can be used to calculate its bounded plural number or its modulus and inverse number.

How can I express the angle displacement of the Quaternary element? Let's set n to the unit vector in the direction of the rotation axis. Then, the Quaternary element q = []

Expressed this angle of rotation. Nx ny nz is the projection vector of the rotation axis on the axis line.

In particular, we propose an slerp interpolation method ). In the Interpolation Process of the Quaternary element, we only need to know that there is a start state q, end state p, and the interpolation parameter t (0 <= t <= 1 ), the slerp function can insert a rotation state in the q and p statuses very quickly and smoothly. Its Mathematical and geometric interpretations can be further read by readers.

Advantages: slerp provides the smoothest interpolation between two orientations; the connection and inverse of the angular displacement are more rapid (compared to the matrix); each angular displacement occupies four memory overhead, medium overhead;

Disadvantage: it is the most difficult to use (it is already quite sci-fi to understand it); there may be a group of Quaternary elements that express an illegal angle displacement.

 

We have learned three ways to express the angle of rotation. Which one is used in development? We need to understand their respective advantages and select them based on the actual situation. The angle displacement expressed in these three methods can be converted to each other, and the mathematical process is complicated. We can directly call the existing API to complete the process.

 

 

Geometric elements

 

Continue tomorrow ....

 





Related Article

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.