The use of the Vector3.dot and the Vector3.cross of Unity3d

Source: Internet
Author: User
Tags sin

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.

Pay attention to the difference between the two.



In geometric mathematics:

1. dot Product
The dot product is calculated as follows: a b=|a| | B|cos<a,b> |a| and |b| indicate that the modulo,<a,b> of a vector represents the angle of two vectors.

In addition, in the dot product . <a,b> and <b,a> angles are not in order.
So by dot product, we can actually calculate the angle of two vectors.


  In addition to the calculation of the dot product we can briefly infer whether the current object is facing another object: only the Transform.forward vector of the current object is calculated and ( otherobj.transform.position–transform.position) dot product is available. More than 0 is facing, otherwise it is back. Of course, this calculation will also have a little error. But roughly enough.


 2. Cross product
  cross product Definition: c =a x B   a,b,c are all vectors. That is, the cross product of two vectors gets the vector.
  Nature 1:c⊥a. C⊥b, which is the plane where vector c is perpendicular to the vector A and 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 infer the vector a by using the positive negative value of the cross product. The relative position of B, that is, whether vector b is in the clockwise or counterclockwise direction of vector a.
According to the properties of the above 2, we can also calculate the angle of the two vectors.
The following is a demo sample code:

Using unityengine;using system.collections;public class mainscript:monobehaviour{//vector aprivate Vector3 a;//vector bprivate Vector3 b;void Start () {///vector initialization of a = new Vector3 (1, 2, 1); b = New Vector3 (5, 6, 0);} void Ongui () {///dot product return value float c = Vector3.dot (A, B),///vector A, a, a, the angle of the given value is Radian, we convert it to an angle, easy to see!

float angle = Mathf.acos (Vector3.dot (a.normalized, b.normalized)) * MATHF.RAD2DEG; Guilayout.label (The dot product of the vector A, B is: "+ C); Guilayout.label ("Vector A. The angle of B is: "+ angle);//The return value of the cross product Vector3 e = Vector3.cross (A, b); Vector3 d = Vector3.cross (b, a);//The angle of the vector A and B, 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 ("Vector axb:" + e); Guilayout.label ("Vector bxa:" + D); Guilayout.label (the angle of the vector A, B is: "+ angle);}}

In the demo example above, we have defined two vectors a and b. Their dot product and cross product are calculated separately. and the point product and the cross product to calculate their angle in turn.


here is the explanation :
1.a.normalized and b.normalized represent the unit vectors of two vectors, as in the formula. With the division of vector and modulo, the result is the unit vector. So we use the unit vectors to calculate this and the back. Save a lot of trouble.

2.mathf.rad2deg represents the degree of the unit Radian.

For details, please see my 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| In fact, it is the die of the cross product. In other words, it also represents the value of Vector3.distance (Vector3.zero, Vector3.cross (a.normalized, b.normalized)).
The resulting diagram is as follows:

&NBSP;&NBSP;

  This article for personal originality, all rights reserved, please specify the source: http://blog.csdn.net/ml3947, in addition to my personal blog: http://www.wjfxgame.com.


The use of the Vector3.dot and the Vector3.cross 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.