For those of you who have booked our game engine course, make sure you force yourself to read everything at least once, because these tips will be reflected in the design of the entire game engine.


Length of vector

Now that we have explained something about the coordinate system, we can look at some of the most common operations that are used on points and vectors. These operations should be very common in any 3D program or renderer.


I’m going to unit a vector

As we explained earlier, in 3D space, a vector can be thought of as an arrow pointing from one point to another. Not only does this vector specify the direction from A to B, but it can also be used to calculate the distance between two points AB. And it’s important to note that the basis vectors on our axes are usually unit vectors.

A unitalized vector with length 1 is called a unit vector. It’s pretty easy to unit a vector. We take each of its components and divide by its length.


In mathematics, you might find a word called norm, which defines a function that can be used to specify the length of a vector. This function that we just talked about is called the Euclidean norm.


Dot product

The dot product of two vectors is just A dot B in mathematics, and the dot product of two vectors gives you A number. All we’re talking about here is vector multiplication over real numbers. The dot product is taking the product of the parts of each vector and adding them up.

In any 3D program or renderer, the dot product of two vectors is a very important operation, because the dot product gives us a cosine, and a lot of times we’re going to need the cosine to do some subsequent operations. For example, the dot product can be used to check if two vectors are perpendicular to each other, and if they are perpendicular to each other, then the dot product is 0. When the dot product is negative 1, then the two vectors are going in opposite directions. If you dot it with 1, that means that the two vectors are pointing in exactly the same direction. And you can also use the dot product to represent the Angle between two vectors or the Angle between a vector and some coordinate axis.


cross-product

The cross product also operates on two vectors. Unlike the dot product, where the dot product is a number, the cross product is a vector. And, you get this vector that’s perpendicular to both of the vectors that are participating in the cross product. The mathematical expression of the cross product is:

C = A x B

You get A C matrix that’s orthogonal to A and B. If we define A plane in terms of A and B, C is perpendicular to the plane A and B lie on. In cross products, the order of multiplication is very important, so A cross B is not the same as B cross A.

In other words, the cross product does not satisfy the commutative law of multiplication.


Other operations – addition, subtraction

Addition and subtraction are intuitive when applied to points. Just move it around a little bit. But when you add and subtract between vectors, that’s not the case. Some 3D apis distinguish between points, normals, and vectors. Therefore, it is indeed possible to create points, normals, and vectors, three completely different C++ classes. Normals, for example, don’t get converted like points and vectors. When you subtract two points, you obviously get a vector. You add a vector to another vector or to a point, you make a point and so on. However, some people also argue that doing so increases the complexity of the code. So there is no clear distinction between them in our war engine, but the user must know the difference mathematically.


Since the wechat official account can only be sent four times a month, we will start to publish articles in the style of chicken wings from next month, and we will also provide links to the original text. If you don’t want to miss every article, please check out the book readings section on our website.