Unity | Vectors, matrices, homogeneous coordinates

Table of contents

1. Vector dot product & cross product

1. Point multiplication

1.1 Formula

1.2 Geometric meaning

2. Cross product

2.1 Formula

2.2 Geometric meaning

2. Matrix point multiplication & cross multiplication

1. Matrix

2. Dot product of matrix

3. Cross multiplication of matrices

3. Matrix rotation

4. Homogeneous coordinates


1. Vector dot product & cross product

1. Point multiplication

        Also known as the inner product, the result is a scalar,

1.1 Formula

        There are two calculation formulas for dot multiplication of vector a and vector b:

  1. a·b=(x1, y1, z1)·(x2, y2, z2)=x1×x2+y1×y2+z1×z2;
  2. a·b=‖a‖×‖b‖×cosβ。

1.2 Geometric meaning

        As shown in the figure below, when the result of dot multiplication of two vectors a and b is a positive number, the directions of the two vectors are relatively consistent, and the larger the positive number is, the more consistent the directions of the two vectors a and b are until the obtained When the positive number is the largest, the directions of both are exactly the same. When the result of the dot product of two vectors a and b is a negative number, the direction of the two vectors a and b is opposite. The smaller the negative value obtained, the more severe the opposite direction of a and b. When the negative number is the smallest, The two directions are completely opposite. 

        In addition to judging the direction of two vectors, point multiplication can also be used to calculate the angle of β (thus calculating whether it is parallel or vertical) , namely: β=arcos((a b)/(|a|×|b|) ). In Unity: β=Mathf.Acos(Vector3.Dot(a, b)/(a.magnitude×b.magnitude))

        Dot multiplication can also be used to calculate the projection length of a vector in a certain direction, so as to realize the projection transformation of the vector.

2. Cross product

        Also known as outer product, vector product. Like the Vector3 dot product, Vector3's cross product is also a calculation formula between vectors. The difference is that the result of the cross product is no longer a value, but a vector of the same dimension.

2.1 Formula

        c=a×b=(a1, a2, a3)×(b1, b2, b3)=(a2×b3-a3×b3, a3×b1-a1×b3, a1×b2-a2×b1)

        In the formula, after the cross multiplication of two vectors a and b, a vector c perpendicular to the plane formed by the a and b vectors is obtained.

        The length of c can be expressed by another formula, that is, the length of a×b is equal to the product of the size of the vector and the sin value of the angle between the vector: |c|=|a×b|=|a|×|b|× sin beta.

  • When the length of c is 0, a and b are two vectors that are parallel to each other. When the modulus of the cross product of a and b is equal to the modulus of a multiplied by the modulus of b, a and b are two vectors that are perpendicular to each other.
  • The modulus of the cross product of vector a and vector b is the area of ​​the quadrilateral formed by vector a and vector b.

2.2 Geometric meaning

  1. Get the vector perpendicular to the plane formed by the two vectors;
  2. Determine whether two vectors are parallel or perpendicular;
  3. The modulus of the vector cross product is the area of ​​the quadrilateral.

2. Matrix point multiplication & cross multiplication

1. Matrix

  • Diagonal matrix: that is, only the position with the same row number and column number has numbers, and the other positions are 0 square matrix matrix
  • Identity matrix: that is, the numbers on the diagonal with the same row number and column number are all 1, and the other positions are all 0.
  • Transpose matrix: It is to flip the matrix along the diagonal. Since we often use the "square matrix" matrix, after transposing the matrix, the square matrix is ​​still the same size, but the numbers on both sides of the diagonal are Swap.

2. Dot product of matrix

        To multiply numbers and matrices, just substitute all the variables in the matrix directly. This kind of scalar multiplication is actually to expand all the values ​​​​in the matrix. The result is a matrix.

        Matrix and matrix dot product, English hadamard product, so it is also called hadamard product. The size of the two matrices A and B that are required to be multiplied is exactly the same, that is, the sizes of A and B are both M*N. The result may be a matrix, column vector, or row vector.

A \cdot B=\begin{bmatrix} a_{11}& a_{12}\\ a_{21} & a_{22} \end{bmatrix}\cdot \begin{bmatrix} b_{11}& b_{12}\\ b_{21} & b_{22} \end{bmatrix}= \begin{bmatrix} a_{11} b_{11}&a_{12} b_{12}\\ a_{21} b_{21}& a_{22} b_{22}\end{bmatrix}

3. Cross multiplication of matrices

        Generally speaking, matrix multiplication means the cross multiplication of matrices, and the result of cross multiplication is a matrix. Matrix multiplication (A matrix × B matrix) requires some additional conditions. The condition is that the number of columns of matrix A must be equal to the number of rows of matrix B, otherwise they cannot be multiplied, or multiplication is meaningless.

A \times B=\begin{bmatrix} a_{11}& a_{12}\\ a_{21} & a_{22} \end{bmatrix}\times \begin{bmatrix} b_{11}& b_{12}\\ b_{21} & b_{22} \end{bmatrix}= \begin{bmatrix} a_{11} b_{11}+a_{12}b_{21} &a_{11} b_{12}+a_{12}b_{22}\\ a_{21} b_{11}+a_{22}b_{21}& a_{21} b_{12}+a_{22}b_{22} \end{bmatrix}

        The matrix obtained after matrix multiplication, in which each position Cij (that is, the i-th row and j-column of the C matrix) is the calculation result of the dot product of the i-th row vector of the A matrix and the j-th column vector of the B matrix.

        When a matrix multiplied by a certain matrix is ​​equal to the identity matrix, this "some" matrix is ​​the "inverse matrix" of the matrix. If a matrix has an inverse matrix, the matrix is ​​called an invertible matrix; on the contrary, if the matrix does not have an inverse matrix, then the matrix is ​​called an irreversible matrix.

3. Matrix rotation

        We can say that the matrix is ​​rotated and scaled by the standard matrix, which is the geometric interpretation of the matrix. For standard matrices, another matrix is ​​formed after rotation and scaling, and this resulting matrix is ​​the "transformation matrix" we calculate. For any vector, multiply by the "transformation matrix" to get the rotation and scale values ​​we want to express.

        A matrix wants to rotate β°, then the first row of the rotation matrix is ​​[cosβ, sinβ], and the second row is [-sinβ, cosβ], which express the standard vectors (1, 0) and (0, 1) respectively Vector after rotation by β°. As shown below.

        Any vector multiplied by this rotation matrix will be rotated by β° about the standard axis in the standard coordinate system.

4. Homogeneous coordinates

        Homogeneous coordinates are to represent an original n-dimensional vector by an n+1-dimensional vector. For example, a three-dimensional vector is represented by a four-dimensional vector, that is, Vector3 becomes Vector4, and besides x, y, and z, there is one more The same is true for w and the homogeneous matrix. Things that cannot be expressed in n dimensions can be expressed in n+1 dimensions, and things that cannot be expressed in 3×3 matrices can be expressed in 4×4

        In Euclidean geometric space, two parallel lines never intersect (quoted from "Homogeneous Coordinates"). But in projective space, two parallel lines appear to intersect at a point at infinity.

        The homogeneous coordinates (Homogeneous Coordinates) proposed by August Ferdinand Möbius allow us to geometrically process images in the projection space. Homogeneous coordinates use N+1 components to describe N-dimensional coordinates. For example, 2D homogeneous coordinates add a new component w to (x, y, w) on the basis of Cartesian coordinates (X, Y), where the large X, Y in the Cartesian coordinate system and the homogeneous The small x and y in the secondary coordinates have the following correspondence: X=x/wY=y/w. A point (1, 2) in Cartesian coordinates is (1, 2, 1) in homogeneous coordinates. If this point moves to infinity (∞, ∞), it will be (1, 2, 0) in homogeneous coordinates, which avoids using meaningless "∞" to describe the point at infinity.

        Points (1, 2, 3), (2, 4, 6) and (4, 8, 12) correspond to the same point (1/3, 2/3) in Cartesian coordinates. A product of any number (1a, 2a, 3a) always corresponds to the same point (1/3, 2/3) in Cartesian coordinates. The points are thus "homogeneous" in that they always correspond to the same point in Cartesian coordinates. In other words, homogeneous coordinates describe Scale Invariant.

        Matrix multiplication is very powerful and can express rotation, scaling, projection, mirroring, and shearing, but it cannot express translation ( due to the nature of matrix multiplication, any matrix multiplied by a zero vector is zero, so a zero vector cannot be translated ), what should I do? The homogeneous matrix can just meet our needs. The homogeneous matrix adds a dimension to the original dimension, and uses the extra dimension to express the translation operation.

Guess you like

Origin blog.csdn.net/weixin_39766005/article/details/131685707