Vector vectors in Unity related use 1
Sun Guangdong 2015.10.28
The following pages provide some suggestions for using vectors effectively in your code.
Understanding Vector Algorithms
Vector arithmetic is the basis of 3D graphics, physics, and animation, and it is useful to have a deep understanding of it. Here are the main operations and some suggestions that can be used for a number of things in the description.
When two vectors are added, the result is equivalent to the original vector as "step", one by one. Note that the order of these two parameters is not important, because the results are the same either way.
If the first vector is a point in space then the second can be interpreted as an offset, or "jump" from this position to another position. For example, to find a location on 5 units above ground, you can use the following calculation methods:-
varnew Vector3(050);
If the vectors represent power, then it is more intuitive that they represent the direction and size (size of the force). Adding two forces in a new vector results in a combination of forces equal to the force. This concept is often useful when exerting force with several individual components, taking immediate action (e.g., being pushed forward the rockets may also be subject to side winds).
Vector subtraction is most commonly used to go the direction and distance from one object to another. Note that the order of the two parameters does not matter in terms of subtraction:-
// The vector d has the same magnitude as c but points in the opposite direction.var c = b - a;var d = a - b;
As a number, adding a negative vector is the inverse of these two vectors.
// These both give the same result.var=- b;var=+-b;
A negative vector has the same original size and points along the same line, but in the opposite direction.
Scalar multiplication and division
A vector multiplied by a scalar result indicates the same vector in the original direction. However, the new vector size equals the original size multiplied by the scalar value.
These operations are useful when vectors represent an offset or a force of motion. They allow you to change the size of the vector without affecting its orientation.
When any vector is divided by its own magnitude, the result is a vector whose size is 1, which is called a normalized vector. If a normalized vector is multiplied by a scalar result size will be equal to that scalar value. This is useful when the direction of the force is constant, but the strength is controllable (for example, the wheel force from a car is always pushed forward but the power is controlled by the driver).
Dot Product
The dot product takes two vectors and returns a scalar. This scalar equals the result of multiplying the two vectors by multiplying the cosine of the angle between the vectors. When the two vectors are normalized, the cosine is basically the first vector to be projected in the second direction (or vice versa-the order of the parameters is not important).
It is easy to think from the point of view and then use the calculator to find the corresponding cosine. However, it is useful to visually understand some of the main cosine values, as shown in:-
Dot product is a very simple operation that can use the Mathf.cos function instead or vector amplitude manipulation in some cases (it will not do the same thing, but sometimes the effect is equivalent). However, calculating the dot product feature requires less CPU time, so it can be a valuable optimization.
Differential product
The result vector is perpendicular to the two input vectors. The "left hand rule" can be used to remember the direction of the output vector from the ordered input vectors. If the first argument matches the second parameter of the thumb's hand and forefinger, then the result will point to the middle finger of the direction. If the order of the arguments is reversed then the resulting vector will point in the opposite direction, but will have the same magnitude.
The result size equals the input vector multiplied, and then the value is multiplied by the sine of the angle between them. Some useful sine function values are as follows:-
direction and distance from one object to another
Subtraction is the direction.
from the player‘s position to the target‘s.var heading = target.position - player.position;
Points to the direction of the target object, the size of which is equal to the distance between two positions. The distance between objects is equal to the size of the direction vector and this vector can be processed by dividing its size by the normalized direction:-
var distance = heading.magnitude;var// This is now the normalized direction.
This method is best used for size (magnitude) and normalization (normalized) attributes of two separate, because they are two quite CPU-consuming (they all involve computing square roots).
If you only need to use distance to compare (close to check etc.) then you can completely avoid calculating magnitude and so on. The Sqrmagnitude property is a square value of size, like size but without time-consuming square root operations. Compared to the size of the distance, you can compare the amount of distance squared:-
if (heading.sqrMagnitude < maxRange * maxRange) { // Target is within range.}
This is more efficient than the actual size in comparison.
Sometimes, the heading of a target on the ground is required. For example, imagine that a player standing on the ground needs to be close to a target floating in the air. If you subtract the target from the player's position then the result vector will point to the upward target. This is not suitable for player-oriented transformations, as he will also point upwards; What is really needed is a direct drop from the Player's position to the ground below the target position vector. This is easy to get the result of the subtraction operation, set the Y coordinate to zero:-
Calculates a normal/vertical vector
A normal vector (that is, a vector perpendicular to the plane) is often required during mesh generation, and may also be used for path tracking and other situations. Given that three points are on the plane, say the corner of the mesh triangle, it is easy to find normals. Take any three points, then subtract any two points to derive two vectors:-
var a: Vector3;var b: Vector3;var c: Vector3;var side1: Vector3 = b - a;var side2: Vector3 = c - a;
The cross of these two vectors results in a third vector, which is a vector perpendicular to the surface. The "left hand rule" can be used to determine normal normals.
var perp: Vector3 = Vector3.Cross(side1, side2);
If the order of the input vectors is reversed the result will be in exactly the opposite direction.
For meshes, the normal vector must be normalized. This can be done by normalization the properties of the normalized, but there is another technique that is occasionally useful. You can also divide by its size to get AH:-
var perpLength = perp.magnitude;perp /= perpLength;
The area of the original triangle is equal to PERPLENGTH/2. This is useful if you need to find the entire mesh surface area or want to randomly select a triangle based on its relative field probability.
direction and distance from one object to another
The projection size of a vector on another vector
The speedometer work of a car is usually measured by the rotational speed of the wheel. This car may not move directly forward (it may slip sideways) This case part of the motion will not be measured in the direction of the tachometer. The object rigidbody.velocity vector is in the direction of its overall motion, if the speed of the forward is required:-
var fwdSpeed = Vector3.Dot(rigidbody.velocity, transform.forward);
Naturally, the direction can be anything you like but the direction vectors always have to be normalized by this calculation. Not only is the result more accurate than the speed, but also avoids the slow square root operation involved in finding the size.
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced. From the game development lab-Sun Guangdong
Vector vectors in Unity related use 1