Use of Vector3.dot (dot product) and Vector3.cross (cross product) of Unity3d

Source: Internet
Author: User

In Unity3d, vector3.dot means to find the dot product of two vectors; Vector3.cross represents the cross product of two vectors.
The result of the dot product calculation is a numerical value, and the result of the cross product calculation is a vector. Be careful to distinguish between the two.

In geometric mathematics:

  dot product is calculated as:   a b=|a| | b|cos<a,b>   where |a| and |b| indicate that the modulo,<a,b> of the vector represents the angle of the two vectors. In addition, the,<a,b> and <b,a> angles in   dot product   are not sorted in sequence.  
  so by dot product, we can actually calculate the angle of two vectors.  
  In addition, by calculating the dot product, we can easily determine whether the current object is facing another object: simply calculate the transform.forward vector of the current object and (otherobj.transform.position– transform.position) dot product can be, more than 0 face, otherwise back. Of course, this calculation will be a little bit of error, but roughly enough.  


2. Cross product
Definition of cross product: C =a x B where a,b,c are vectors. That is, the cross product of two vectors gets the vector!
The nature of the 1:c⊥a,c⊥b, that is, the vector c perpendicular to the plane of the vector b.
Property 2: Die length |c|=|a| | B|sin<a,b>
Property 3: Satisfies the right hand rule. From this we have AXB≠BXA, and Axb =–bxa. So we can use the positive negative value of the cross product to determine the relative position of the vector A, B, that is, the vector A is in the clockwise or counterclockwise direction.
According to the properties of the above 2, we can also calculate the angle of the two vectors.
Here is the sample code:

usingUnityengine;usingSystem.Collections; Public classmainscript:monobehaviour{//Vector a    PrivateVector3 A; //Vector b    PrivateVector3 B; voidStart () {//Initialization of vectorsA =NewVector3 (1,2,1); b=NewVector3 (5,6,0); }        voidOngui () {//The return value of the dot product        floatc =Vector3.dot (A, b); //The angle of the vector, A, and the resulting value is radians, we convert it to an angle for easy viewing!         floatAngle = Mathf.acos (Vector3.dot (a.normalized, b.normalized)) *mathf.rad2deg; Guilayout.label ("the dot product of the vector A, B is:"+c); Guilayout.label ("the angle of the vector A, B is:"+angle); //The return value of the cross productVector3 e =Vector3.cross (A, b); Vector3 D=Vector3.cross (b, a); //The angle of the vector, A, and the resulting value is radians, we convert it to an angle for easy viewing! Angle = Mathf.asin (Vector3.distance (Vector3.zero, Vector3.cross (a.normalized, b.normalized))) *mathf.rad2deg; Guilayout.label ("the vector axb is:"+e); Guilayout.label ("the vector bxa is:"+d); Guilayout.label ("the angle of the vector A, B is:"+angle); }}

In the above example, we have defined two vectors a and b. Their dot product and cross product are calculated separately, and their angles are computed in turn by dot product and cross product.
Here is the explanation:


1. A.normalized and b.normalized represent the unit vectors of two vectors, because in the formula, there is the division of the vector and modulo, the result is the unit vector, so we here and behind are directly using the unit vector to calculate, save a lot of trouble.
2. Mathf.rad2deg represents the degree of the unit Radian. For details, please see author's article: Unity3d's mathf.rad2deg and Mathf.deg2rad
3. Calculation of degrees by cross-product is by Formula |c|=|a| | B|sin<a,b> to reverse the evaluation. |c| is actually a cross product of the mold, in other words, also represents the value of Vector3.distance (Vector3.zero, Vector3.cross (a.normalized, b.normalized)).
The result diagram is as follows:

Reprinted from: Http://www.tuicool.com/articles/jUN7Nn

Use of Vector3.dot (dot product) and Vector3.cross (cross product) of Unity3d

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.