Mathematical methods in 3D games and computer graphics-transformations

Source: Internet
Author: User
Tags cos

1 transformations

Throughout the development of 3D games, it is often necessary to transform a series of vectors in some way. The transformations that are commonly used include panning, zooming, and rotating.

1.1 General-Purpose transformations

The n x n invertible matrix M can often be thought of as a transformation matrix from the coordinate system to another coordinate system. The columns of M give the mapping of the coordinate system from the original coordinate system to the new coordinate system. For example, M is an n x n invertible matrix, and when M is multiplied with a vector (1,0,0), (0,1,0) and (0,0,1), it can be obtained

Similarly, the M-1 column gives the mapping of the axis from the new axis to the original coordinate system. This allows a transformation matrix to be constructed for any given linearly unrelated vector u,v,w, which maps these vectors to vectors (1,0,0), (0,1,0), and (0,0,1).

Multiple transformations can be concatenated together, and the product of multiple transformation matrices can be represented by a matrix. It is assumed that the matrix M is used to transform an object using matrix G, and because the product satisfies the binding law, there is g (MP) = (GM) p for any vector p, so only the matrix obtained by the product of the GM is stored, and the matrix can be used as the transformation matrix of the object. This allows multiple transformations of the fixed point, while the storage space is unchanged.

An orthogonal matrix is a matrix whose transpose matrix equals its inverse matrix. An orthogonal matrix can only be used to represent a combination of rotation and reflection.

Reflection refers to an operation that mirrors a point in a certain direction. For example, the matrix

The XY plane is reflected in the z-coordinate that faces a point symmetrically.

Hand Sex

In three-dimensional space, the base & of the coordinate system composed of the vector v1,v2,v3 is chiral. For right-hand base, there is (V1*V2). V3>0. In other words, in a right-handed coordinate system, the direction of the cross product of the V1,v2 and the direction of the V3 form a sharp angle. If & is the right-hand base of an orthogonal specification, then there is v1*v2=v3. if (V1*V2) v3<0, then & is the left-hand base.

An odd number of reflections will change the direction of the hand, and an even number of times the reflection is equivalent to one rotation. By observing the determinant of a 3x3 matrix, it is possible to determine whether a matrix has reflection. If the determinant of M is negative, then there is reflection, and when the vector of any base is transformed by M, the hand of the base will change. If the determinant is positive, then M does not change the direction of the hand.

The determinant of the orthogonal matrix M can only be 1 or-1. If detm=1, the matrix M has only a rotation, and if 1, then m represents a rotation and then a reflection.

1.2 Scaling transformations

Simply put, for unified scaling it can be understood that the vector p is multiplied by a constant A, that is, p ' = AP. In three-dimensional space can be expressed by the product of a matrix.

If you are scaling a vector with different values on the XYZ axis, you can scale to non-uniform scale.

If you perform non-uniform scaling on 3 arbitrary axes, you need to use a complex scaling process. Assuming that the coefficient a is in the U-axis direction, in the direction of factor B along the V-axis, and in the direction of the W-axis of the coefficient C, you can first transform from the coordinate system (U,V,W) to the coordinate system (I,J,K), then scale in the (i,j,k) coordinate system, and finally revert to the (u,v,w) coordinate system.

1.3 Rotation transformation

First, the rotation on a two-dimensional plane, that is, in a planar Cartesian coordinate system. Suppose a vector p (px,py) is to be obtained relative to its own rotation a-degree angle after the vector p '. We need to get its orthogonal vector Q (-PY,PX) based on p counterclockwise rotation 90 degrees. P,q can be viewed as a pair of orthogonal bases of the coordinate system, then P ' can be represented by P and Q as:

P ' = Pcosa + Qsina; (1)

You can determine that the X, y component of P ' is:

Can be changed into the form of a matrix:

Extension: Adding the third and third columns of the unit matrix to the P ' matrix can be:

, The matrix is a three-dimensional rotation around the z axis.

You can also get a 3x3 rotation matrix around the x-axis and y-axis rotation angles.

Rotate around any axis

Assuming that the vector p rotates θ around an arbitrary axis, where any axis is expressed in unit vector A, the vector p can be decomposed into a component P1, P2, perpendicular to the peace line. Since the parallel component P2 is parallel to a, it remains constant during the rotation and the main requirement is the rotation of the P1.

Since a is a unit vector, for the projection of P on a, it can be expressed as: Projap = (A.P) A;

P the component perpendicular to a can be expressed as: Perpap = P – (A.P) A;

Now rotate the component perpendicular to a, and then add the Projap to get the final rotation result.

How is the rotation of the vertical component obtained?

The rotation of the vertical component is carried out in a plane perpendicular to the a axis. The rotated vector can be represented by a linear combination of the vectors formed by the PERPAP and the PERPAP in the counterclockwise direction of 9 degrees. As shown, suppose a is the angle between the original vector p and the a axis. The length of the PERPAP equals | | p| | Sina, then the PERPAP rotates 90 degrees counterclockwise around a axis PERPAP ' is the cross product of A and p.

Then Perpap rotation θ can be expressed as:[P (A.P) a]cosθ + (A x P) sinΘ. (2)

(2) Type of introduction can refer to (1) type.

Plus Projap = (A.P) A, you can get the rotation formula of P around a axis:

P ' = Pcosθ + (A x P) sinΘ + A (A.P) (1-cosθ); (3)

Replace (3) with the expression of the matrix:

Combine the same items in the formula, making C=cosθ,s=sinθ, to get a representation that the vector rotates around A Axis Θ rotation matrix of the angle RA (Θ).

1.4 Homogeneous coordinates

With regard to panning, it is not possible to use the 3 x 3 matrix to translate the point p from one coordinate system to another, simply by adding an offset vector without affecting the direction and scale of the axis.

P ' = MP + T (4)

where M is a 3 x 3 invertible matrix, T is a 3D translation vector. Using the (4) two operations can be obtained:

(5)

To perform n operations, you must know the matrix component MnMn-1 and the translation component Mntn-1+tn during each transformation.

Using the four-dimensional transformation, we can unify M and T, and we can create a four-dimensional 4 x 4 matrix to represent the panning operation. The method is to add a coordinate to the 3D point P, and the fourth coordinate of the extension is called the W coordinate, and its value is set to 1.

(6)

Multiplying the matrix F by the vector (px,py,pz,1) is equivalent to (4) transforming the x, Y, z coordinates of the vector, while keeping W at 1.

From the formula (4) can be solved (7)

Therefore, the inverse matrix of the 4 x 4 matrix F can be obtained from the formula (6) F-1:

(8)

The correctness of the (8) formula can be verified:

Mathematical methods in 3D games and computer graphics-transformations

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.