[Introduction to Unity] 20. Three-dimensional vector

[Getting started with Unity] 3D vector

    Hello everyone, I am Lampard~~

    Welcome to the Unity Getting Started series of blogs. The knowledge I have learned comes from Teacher Afa at Station B~Thanks 

 

(1) Space vector

(1) What is a three-dimensional vector

    Why is there such a blog? The main reason is that three-dimensional vectors play an important role in unity, and the types used in the parameters of many components are Vector3 types

    If you are all judges, you are studying in university at this time, or you have just left school. That can be skipped directly, your knowledge is definitely better than mine. This article is mainly used as a review for small social animals like bloggers who have been away from campus for a long time

   As shown in the figure, a three-dimensional vector refers to a vector with magnitude and direction in three-dimensional space, usually represented by three real numbers, representing the components of the vector on the X, Y, and Z axes respectively

    In Unity, we use the vector3 type, the most common one is our transform component

(2) Vector addition and subtraction

    Numerically, addition and subtraction are very simple, such as vector (3,0,0) + (0,4,0), the result is (3,4,0)

    The same vector (3,4,0) subtracts the vector (3,0,0) to get (0,4,0), and each part of the vector is added and subtracted by itself

 

     From a graphical point of view, vector addition can obtain a new vector through the triangle theorem or the parallelogram theorem. And vector subtraction can be used to calculate the distance between two vectors

(3) The length of the vector

    How to find the length of a vector? Very simple is x²+y²+z² to open the root sign

     For example, the length of the vector (3,4,0) is 5, and the concept of a unit vector will be introduced next.

A unit vector is a vector of length 1, also known as a standard vector or a normalized vector. In 3D geometry, unit vectors are very useful in representing directions, since they only describe direction without regard to size or scaling

    Then for the (3,4,0) vector, its vector is (0.6,0.8,0)

(4) Dot product of vector

    The previous content is relatively simple. When it comes to point multiplication and cross multiplication, everyone can start to get confused and itchy scalp (dead memories suddenly attack me)

    The dot product of vectors (also known as scalar product or inner product) is a vector operation that multiplies the corresponding components of two vectors and adds the results to obtain a scalar (that is, a real number)

    The formula is as follows:

a · b = a.x * b.x + a.y * b.y + a.z * b.z

Where a and b are three-dimensional vectors, ax, ay, az represent the components of vector a on the X, Y, and Z axes respectively, bx, by, bz are the same

    For example, (3,0,0) dot multiplied by (0,4,0) = 0, what is the effect of dot multiplication?

For example, we can determine whether two vectors are perpendicular     by multiplying the same point . If vector A is multiplied by vector B = 0, it means that the two vectors are perpendicular; it can be used to calculate the angle of two vectors : cos θ = (a b) / (| |a|| * ||b||), and the projection of a vector onto another vector , etc.

(5) Cross multiplication of vectors

    Cross multiplication should be learned in freshman high mathematics? Hahaha really forgot

The cross product of vectors (also known as vector product or outer product) is a vector operation. It calculates the corresponding components of two vectors to obtain a new vector. The direction of the new vector is perpendicular to the original two vectors, and its size is equal to the original two vectors. The area of ​​the parallelogram formed by vectors

    The calculation formula is as shown in the figure below, where i, j, k are the unit vectors of xyz:

    What is the use of cross multiplication? We can calculate the normal vector perpendicular to these two vectors, and use it to determine whether a point is inside the geometry, etc.

(2) Unity vector related interface

(1) Vector addition and subtraction

    Unity provides us with a lot of interfaces for us to use, and there is nothing to say about addition and subtraction, just calculate directly:

Vector3 aaa = new Vector3(3,0,0);
Vector3 bbb = new Vector3(0,4,0);
Debug.Log("向量aaa + 向量bbb = " + (aaa + bbb));
Debug.Log("向量aaa - 向量bbb = " + (aaa - bbb));

    The output is as follows:

(2) The length of the vector magnitude

    Unity's Vector3 class provides us with a magnitude method that can output the length:

Debug.Log("向量aaa + 向量bbb的长度为 = " + (aaa + bbb).magnitude);

    The output is as follows:

(3) Unit vector normalized

    We can also unitize the vector, that is, the direction of the vector remains unchanged, and the length is converted into 1. The interface used is normalized

Debug.Log("向量aaa + 向量bbb的单位向量为 = " + (aaa + bbb).normalized);

    The output is as follows:

    Vector3 also provides several unit vector constants , which we can use directly as follows: 

(4) The dot product of the vector Dot

    Dot multiplication can use the static method of Vector3, Dot

Vector3 aaa = new Vector3(3,0,0);
Vector3 bbb = new Vector3(0,4,0);
Debug.Log("向量aaa 点乘 向量bbb的结果 = " + Vector3.Dot(aaa, bbb));

    The output is as follows:

(5) Cross multiplication of vectors

    Cross multiplication can use the static method of Vector3, Cross

Vector3 aaa = new Vector3(3,0,0);
Vector3 bbb = new Vector3(0,4,0);
Debug.Log("向量aaa 叉乘 向量bbb的结果 = " + Vector3.Cross(aaa, bbb));

    The output is as follows, one of their normal vectors:

 (6) Distance between two vectors

    There are two ways to calculate the distance between two vectors , one is to subtract the vectors first and then read the magnitude familiarity, the other can directly use the Distance method of Vector3

Vector3 aaa = new Vector3(3,0,0);
Vector3 bbb = new Vector3(0,4,0);
Debug.Log("向量aaa 和 向量bbb的距离1 = " + Vector3.Distance(aaa, bbb));
Debug.Log("向量aaa 和 向量bbb的距离2 = " +(aaa - bbb).magnitude);

    The output is as follows:

 

Alright, that's all for today, thank you all for reading! ! !
Like and follow! ! !

 

Guess you like

Origin blog.csdn.net/cooclc/article/details/130435883