Reprint Annotated smartdot:http://my.oschina.net/u/243648/blog/67193

1. Mathematics (point multiplication/fork multiplication)/unity3d Mathematics Auxiliary class

2. Coordinate system (local/world/screen)

3. Unity3d Execution Process

4. Calculate the angle between the role and the target point. Rotate the character toward the target point and then move the role (sample)

5. gizmos/inspector/Map Editor

6. Data interaction between script files

7. Yield return/co-threading/Events

8. Socket

9. Unity3d Call C++dll

---------------------------------------------------------------

---------------------------------------------------------------

A Basic mathematical knowledge required in Unity3d (Vector/matrix/transform)

See these several words, I think in the game engine is very common, unity3d inside is no exception!

Even though unity has given us such a good package. You can use it, but you know it. It's better to be more helpful to yourself. What if I had to use a different engine later? I don't want to be pinned down by these tools all my life. You have to understand the mathematical knowledge that might be more boring. Of course, there's no way to talk about complicated math. Of course there is interest, can see more, but do not have to spend more time (except to want to be a mathematician), not clear when you look at the OK. With this knowledge, and then to see those unity3d give me the math auxiliary class, it is very refreshing to use up very much!

1. Vectors (vector)

The vector has two important attribute lengths and orientations, for example! It is convenient to describe the mathematical tool for moving objects in space to know the direction and distance of moving objects. And the camera's viewing direction, the light trend and so on.

The location information is not included, so it can be independent of the coordinate system. Only the vector length and direction can be thought of as equal.

The introduction of coordinate systems does not add additional information to vectors.

So the position of the vector does not affect his attributes.

In the discussion of the position and vector, there is a very easy to confuse the concept of the point and vector, in unity3d sometimes with the vector description of a point, because the vector has x, y, z three float variable. Vector (x, y, z) is a point or a vector, to be considered carefully. Or I understand wrong, have known friends want to tell.

The two concepts of unit vectors and vectors are very basic and very important. Detailed concepts I will not repeat, but to know what the two vectors do.

The unit vector can be used to represent the direction. Modules can be used to denote distances. We can use these two concepts to calculate the direction in which the object moves, and how far it moves. As for object rotation. To relate to the calculation of vectors, it is mentioned later.

Vector operations include: addition, subtraction, multiplication, cross product, for the operation, there is no mention of mathematical calculations and concepts.

Baidu under all know. Everyone is a cultural person, haha. You're kidding!

Give me a sample! Imagine you're going to track an object moving. You find that your tracking direction is biased. How are you going to fix the direction? Suppose you say with eyes, that can, but the computer is blind Ah!

You give the computer an eye! The computer is only numerical, and of course you have to use a mathematical model to express it. and calculate!

At this time you can use the vector subtraction, draw the image of the vector subtraction, is not very convenient to conquer!

For example, you have to chase an object, he always turns east. I want to speed faster than him, but the direction is not correct in the words is very difficult to intercept him down. What am I going to do about it? Imagine a graph of the addition of vectors. According to the speed of two people, I just want to speed faster than you, with the vector addition and multiplication (multiplication can be used to control the speed). I will be able to put you on the road to the stake, let you hit the dead, Waiting for the future.

Let me give you another example. Suppose I want to know what the target object is, what is my angle. I can use dot product to calculate the angle of my door. There are a lot of examples, too lazy to lift, or become a weightlifting champion, to a connection:

Http://wenku.baidu.com/view/f279471514791711cc79176a.html

Speaking of the vector, I would like to mention a small detail, very necessary details. Because the vector's X, y, and X are of type float. Due to the imprecision of float. We should think that two floating-point equals can have a certain error. I'm looking at some game code. Two floating-point numbers equal may be this:

Const Float epsilon=0.001f;//Error Range

BOOL Equal (float num1,float num2)

{

Just to NUM1 and num2 in the error range, it feels equal to put back true, otherwise put back false

Return fabs (num1-num2) <EPSILON?true:false;

}

2. Matrix and Transformation (transform)

Matrix What is this thing used for? The college teacher didn't tell us, so why not tell us? It was not discussed. Anyway, teach me is amateur! After all, the teacher didn't tell me. Wrong hope to correct me ah!

The matrix is a mathematical book that is composed of coefficients and constants of a set of equations. Mathematically, the matrix represents a transformation from a linear space to a linear space.

Through a textbook example, a simple description of the calculation of the formula, is expected to be more stupid reason. I don't like to describe the narrative directly with mathematical symbols. Just like to see pictures.

Matrix plainly. Your personal understanding is to let you "cross" the vector from one space to another in a space. Move (or rotate) one position to another.

As for the space has the local coordinate system, the world coordinate system, the screen coordinate system and so on, will discuss later. I look at the matrix transformation there are several: translation, rotation, scaling, these unity3d are already encapsulated.

I want to look at the details of how they change. I am too lazy to draw the drawing board, go to the network to find a picture!

According to the algorithm of the matrix, the vector is multiplied by the transformation matrix to set the formula, the lower the conversion before and after the value, you know!

Each transformation has a corresponding transformation matrix. By multiplying the transformation matrix with vectors or coordinates, you can transform them over. The transformation matrix can also do multiplication superposition, the geometric meaning of superposition is to add the transformation to a matrix in the order of superposition. Note that the matrix overlay does not satisfy the commutative law. The transformation matrix is a 4 x 4 matrix, so the vectors and coordinates need to be extended to the homogeneous space.

Vector: (x, y, Z, 0)

Coordinates: (x, Y, Z, 1)

Their difference lies in the fourth item. The 4th item of the vector takes 0. It can invalidate the translation transformation of the matrix without affecting the rotation and scaling operations.

The 4th item of coordinates takes 1, making the translation effective, and the scale of the translation transformation is not changed.

Suppose to take 2. The distance of the translation is twice times that defined in the matrix.

And so on

Note that after the transformation, it is possible that the 4th item is not 0/1. At this point, we will need to do a mapping action to map it back to the 3-dimensional space from the homogeneous space. The method is very easy:

(x, Y, Z, W)--(x/w, y/w, z/w, w/w)--(x/w, y/w, z/w, 1)--(x/w, y/w, z/w)

3. Plane (plane) and Ray (Ray)

Plane:

n Dot P + d = X;

X<0, point P is on the back of the plane, | X| That is, the distance from the point p to the plane

X>0. Point P is on the front of the plane, | X| The distance from the point p to the plane

Ray:

Set the starting point to P0, and the direction is u,t. T belongs to [0, Infinity) when T belongs to (-infinity. + Infinity) represents a straight line.

P (t) = p0 + T * u

The reason they're going to be together is that they are both in the actual development and are often used together. At least I use a lot more, give me a sample.

For example, we click on the 2D screen coordinates to determine the location of the game in 3D space. How do you know? How to use a two-dimensional mouse with only x, y axes. It is a topic that has been discussed from three dimensions as far as possible to obtain the position of three dimensional space of X, Y and Z accurately. Even with the development of graphic technology today, the three-dimensional space of the click can still only say "probably accurate." Mouse click to determine the position of the three-dimensional space and the camera's relationship is very large. Why is it that there is a big relationship with the camera? Assuming that you are familiar with drawing flowing water inside the direcx3d, it is important to know that the camera transforms 3d space coordinates into screen coordinates, such as cropping (clipping). Projection form (projection window). Near cut, far cut, projection plane and so on.

Write a piece of unity code!

(The code format is not good, just look at it).

void Update ()

{

if (Input.getmousebuttondown (0))

{

Raycontrol ();

}

if (Flagmove)

{

if (Vector3.distance (transform.position,mousepos) >1)

{

Transform. Translate (transform.worldtolocalmatrix* ransform.forward* time.deltatime*5);// Transform.forward is the world coordinate, the Transform.worldtolocalmatrix transformation matrix goes to the local coordinates and then moves in the local coordinates, it is not necessary to move in the local coordinate system but must pay attention to unify together.

}

Else

{

Flagmove=false;

}

}

}

void Raycontrol ()

{

Ray Ray=camera.main.screenpointtoray (input.mouseposition);//Send a ray to the screen (note that this object is the main camera)

if (Physics.raycast (ray,out hit,200)) Ray length is 200 and the ground collision box is tested

{

Gameobject targetpos=gameobject.createprimitive (Primitivetype.sphere);//Instantiate a Sphere

Targetpos.transform.localscale=new Vector3 (0.5f,0.5f,0.5f);

mousepos=hit.point;//Get collision Point coordinates

MOUSEPOS.Y=TRANSFORM.POSITION.Y;

Targetpos.transform.position=mousepos;//sphere put the mouse click on the place

targetdir=mousepos-transform.position;//calculates the direction

Vector3 Tempdir=vector3.cross (transform.forward,targetdir.normalized);//cross-multiply to infer whether two vectors are in the same direction

Float Dotvalue=vector3.dot (transform.forward,targetdir.normalized);//point multiplication calculates the angle between two vectors, and the angle between the character and the target point

Float Angle=mathf.acos (dotvalue) *mathf.rad2deg;

if (tempdir.y<0)//This block illustrates the opposite direction of the two vectors. This inference is used to determine if the angle between two and 30 degrees is clockwise or counterclockwise rotation.

{

angle=angle* (-1);

}

Print (TEMPDIR.Y);

Print ("2:" +angle);

Transform. Rotatearound (Transform.position,vector3.up,angle);

Flagmove=true;

}

}

Note: After writing, I found that it is not necessary to be so troublesome, so transform.forward= (mousepos-transform.position). Normalized can direct the character to the target point Raycontrol function A lot of them can be saved.

4. Unity3d Mathematics Auxiliary Class

4.1 MATHF provides functions and constants for mathematical calculations, functions that are required for all mathematical calculations.

The MATHF object has no constructors and is an intrinsic object. is not a class of objects like string, so there is no constructor for Math ().

4.2 matrix4x4 a standard 4x4 transformation matrix A transform matrix can run arbitrary linear 3D transformations (e.g.. Panning, rotating, zooming, trimming, and so on) and perspective transformations use homogeneous coordinates. Matrices are rarely used in scripts: most often use vector3,quaternion, and the functionality of the transform class is simpler. Simple matrices are used in special cases, such as setting up non-standard camera projections.

4.3 quaternion The four-dollar number is used to indicate that rotating them based on complex numbers is not easy to understand intuitively. So you do not need to access or alter individual quaternion components (x,y,z,w); Usually you just need to take existing rotations (for example, from transform) and use them to construct new rotations (for example, to interpolate smoothly between two rotations). Four dollar function you will use it for 99% of the time (other functions are only extra)

quaternion.lookrotation,quaternion.angle, quaternion.euler ,quaternion.slerp,quaternion.fromtorotation, Quaternion.identity.

4.4 Ray Ray is an infinite line that starts at Origin and follows the direction direction

4.5 rect a two-dimensional rectangle defined by x, y position, and width, height size, and the RECT structure is used primarily for 2D operations. The Unitygui system is very popular to use it. and positioning the camera on the screen.

4.6 Vector2/vector3/vector4 represents vectors and points, and structures are used to deliver 3D positions and orientations in unity. It also includes functions that do common vector operations, such as angles. modulo, unit vector, fork multiply, point multiply, forward left to right upward. Interpolated values. Projection, reflection, steering ...

4.7 The last large controller, transform the position, rotation, and scaling of the object, each object in the scene has a transform. Used to store and manipulate the position, rotation, and scaling of objects.

Each transform can have a parent that allows you to apply positions, rotations, and scales hierarchically.

Ability to view hierarchical relationships in the hierarchy panel. They also support counters (enumerator). So you can use loops to traverse sub-objects.

Copyright notice: This article blog original articles, blogs, without consent, may not be reproduced.

Mathematical Foundations and auxiliary classes of Unity3d Mathematics