unity3d-Vector Vector3

Source: Internet
Author: User
Tags acos reflection

Vectors represent direction and size, independent of position distance


The representation of three-dimensional space is as follows


The struct used in Unity3d to describe the Vector3

Namespace Unityengine
{public
	struct Vector3
	{public

		float x;
		public float y;
		public float z;
         }


The length of a vector : the size (or length) of a vector called the modulus of a vector


Public float magnitude
{
	get
	{return
		mathf.sqrt (this.x * this.x + this.y * this.y + this.z * this.z); c13/>}
}

Public float sqrmagnitude
{
	get
	{return
		this.x * this.x + this.y * this.y + this.z * THIS.Z;
	}
}


Two-point distance in three-dimensional space

public static float Distance (Vector3 A, Vector3 b)
{
	Vector3 vector = new Vector3 (a.x-b.x, A.Y-B.Y, a.z-b.z) ;
	Return mathf.sqrt (vector.x * vector.x + vector.y * vector.y + vector.z * vector.z);


Vector addition

public static Vector3 operator + (Vector3 A, Vector3 b)
{return
	new Vector3 (a.x + b.x, A.y + b.y, a.z + b.z);
}


Vector subtraction


public static Vector3 operator-(Vector3 A, Vector3 b)
{return
	new Vector3 (a.x-b.x, A.y-b.y, a.z-b.z); 
  }


Vector dot product (dot product) is also called the quantity product or the inner product


public static float Dot (Vector3 lhs, Vector3 rhs)
{return
	lhs.x * rhs.x + lhs.y * rhs.y + lhs.z * RHS.Z;
}

According to the formula A.B = | a| | B|cos (a) to derive the angle of curvature between two vectors

	public static float Anglebetween (Vector3 from, Vector3 to)
		{return
			mathf.acos mathf.clamp ( From.normalized, to.normalized), -1f, 1f);
		}

	public static float Angle (Vector3 from, Vector3 to)
		{return
			mathf.acos mathf.clamp ( From.normalized, to.normalized), -1f, 1f)) * 57.29578f;//Angle
		}

radians = angle multiplied by pi and divided by 180.

Angle = radians divided by pi and multiplied by 180.


Where a is the angle between A and B in 3D space. If two vectors are known, we can compute the angle between the two vectors by using the quantity product.


The following methods can be used to determine the target's position in front and back:

Vector3.dot (Transform.forward, target.position)

The return value is positive, the target is in front of oneself, and vice versa in its own rear



Vector cross product: The vector product of U and V (cross product, cross product, or outer volume) produces a vector that is perpendicular to u and V, the formula: UXV = n | u| | v| Sin (a), where n is the unit vector perpendicular to u and V, and a is the angle between U and V


public static Vector3 Cross (Vector3 lhs, Vector3 rhs)
{return
	new Vector3 (LHS.Y * rhs.z-lhs.z * rhs.y, Lhs.z * Rhs.x-lhs.x * rhs.z, lhs.x * rhs.y-lhs.y * rhs.x);


The following method can be used to determine the target in its left and right direction:

Vector3.cross (Transform.forward, target.position). Y

The return value is positive, the target is on the right side, and the opposite is on the left.



multiplier vector : The product of the real number λ and the vector b is a vector, which is recorded as: A=λb. Rule: When λ is positive, the same direction, when Lambda is negative, inverse, real number λ, called vector coefficients. The geometric meaning of a multiplier vector is to enlarge or shrink the vector in the same direction or in the opposite direction.

public static Vector3 operator * (Vector3 A, float D)
{return
	new Vector3 (a.x * d, A.Y * d, a.z * d);
}

public static Vector3 operator * (float D, Vector3 a)
{return
	new Vector3 (a.x * d, A.Y * d, a.z * d);
}

vector comparison:

public static BOOL operator = = (Vector3 lhs, Vector3 rhs)
{return
	vector3.sqrmagnitude (LHS-RHS) < 9.9999994 4e-11f;
}
public static bool operator!= (Vector3 LHS, Vector3 rhs)
{return
	vector3.sqrmagnitude (LHS-RHS) >= 9.99999944 e-11f;
}

Normalization of vectors: The normalization of vectors is also called (normalized) to make the model of a vector become 1, which becomes the unit vector. Vector normalization can be achieved by dividing the vector by the modulo of the vector. The normalized vector corresponds to the unit vector in the same direction as the vector, which can be used to represent the direction of the vector. Because the concept of direction is very important in 3D programming, this concept is also important, the unit vector has many important properties, in the surface of the object to represent the normal vector is more frequent

public static Vector3 normalize (Vector3 value)
{
    float num = vector3.magnitude (value);
    if (num > 1e-05f)
    {return
        value/num;
    }
    return vector3.zero;
}


projection : Generally used for perspective, the following figure U ' is the projection of U on V, the angle of the vector u and V is the length of theta,d is U ', and the direction of U ' and V is the same, and the direction of v/|v| is U '

public static Vector3 Project (Vector3 vector, Vector3 onnormal)
{
	float num = Vector3.dot (onnormal, onnormal); C2/>if (num < 1.17549435e-38f)
	{return
		vector3.zero;
	}
	Return onnormal * Vector3.dot (vector, onnormal)/num;
}

reflection vector: The following image incident Ray Vector I and plane normal vector n,r is the reflection vector, r=i-2 (i*r) R

deduced as follows:

The angle between the normal vector N of the incident ray Vector I and the reflection plane is theta. The beginning of the connection I and the end of R, there is R = 2p-i

Set Mosse 0 to P and N to the intersection of the vector s, then there is P = I + S

Vector s is vector-N (note that this is-N, because S and N are in the opposite direction.) The projection on the vector N, according to the projection formula of the vector, is simplified with the following: s=-(I.N) N, substituting R and P, with R=i-2 (I*R) r


public static Vector3 reflect (Vector3 indirection, Vector3 innormal)
{
	return-2f * Vector3.dot Innormal, indirection) * innormal + indirection;
}



Note: Partial diagrams and data from the Internet

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.