Unity3D: Use of Vector3.Dot and Vector3.Cross

Source: Internet
Author: User

In Unity3D, Vector3.Dot represents the dot product of two vectors, while Vector3.Cross represents the cross product of two vectors.
The result of dot product calculation is a numerical value, and the result of cross product calculation is a vector. Note the differences between the two.

In geometric mathematics:

1. Dot Product
The Calculation Method of dot product is as follows: a · B = | a | · | B | cos <a, B> where | a | and | B | represent the model of the vector. <, b> the angle between two vectors. In addition, the angle between <a, B> and <B, a> is not sequential.
So through dot product, we can actually calculate the angle between two vectors.
In addition, through the calculation of the dot product, we can roughly determine whether the current object is facing another object: only the transform of the current object needs to be calculated. forward vector and (otherObj. transform. position-transform. point product of position). If it is greater than 0, it is facing; otherwise, it is facing back. Of course, this computation may also have some errors, but it is roughly enough.
2. Cross Product
Cross Product Definition: c = a x B where a, B, c are vectors. That is, the cross product of the two vectors is still the Vector!
Property 1: The plane where the vector c perpendicular to the vector a and B.
Property 2: module length | c | = | a | B | sin <a, B>
Nature 3: satisfies the right-hand rule. From this point we have axb =bxa, while axb =-bxa. Therefore, we can use the positive and negative values of the cross product to determine the relative position of the vector a and B, that is, whether the vector B is in the clockwise or counterclockwise direction of the vector.
Based on the above property 2, we can also calculate the angle between the two vectors.
The following is the sample code:

Using UnityEngine; using System. collections; public class MainScript: MonoBehaviour {// vector returns vate Vector3 a; // vector bprivate Vector3 B; void Start () {// vector initialization a = new Vector3 (1, 2, 1); B = new Vector3 (5, 6, 0);} void OnGUI () {// return value float c = Vector3.Dot (a, B) of the dot product ); // The angle between vectors a and B. The obtained value is radian. we convert it to an angle for easy viewing! Float angle = Mathf. acos (Vector3.Dot (. normalized, B. normalized) * Mathf. rad2Deg; GUILayout. label ("the dot product of the vector a and B is:" + c); GUILayout. label ("the angle of vector a and vector B is:" + angle); // return value of cross product Vector3 e = Vector3.Cross (a, B); Vector3 d = Vector3.Cross (B, a); // The angle between vectors a and B. The obtained value is radian. we convert it to an angle for easy viewing! Angle = Mathf. asin (Vector3.Distance (Vector3.zero, Vector3.Cross (. normalized, B. normalized) * Mathf. rad2Deg; GUILayout. label ("vector axb:" + e); GUILayout. label ("vector bxa:" + d); GUILayout. label ("the angle between vector a and vector B is:" + angle );}}
In the above example, we define two vectors a and B. Their dot and cross product are obtained respectively, and their angle is calculated by the dot and cross product.
It should be noted that:
1. a. normalized and B. normalized represents the unit vectors of two vectors, because in the formula, there is division between vectors and modulus, and the result is the unit vector, therefore, we use the unit vector for calculation both here and later, saving a lot of trouble.

2. Mathf. Rad2Deg indicates the unit radians. For details, see my article: Mathf. Rad2Deg and Mathf. Deg2Rad in Unity3D.
3. Calculate the degree by using the formula | c | = | a | B | sin <a, B>. | C | it is actually the modulo of the cross product. In other words, it also represents the value of Vector3.Distance (Vector3.zero, Vector3.Cross (a. normalized, B. normalized.
The result is as follows:

This article is original and copyrighted. For more information, see http://blog.csdn.net/ml3947. for more information, visit http://www.wjfxgame.com.


Related Article

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.