[unity基础知识]之Unity3d之Vector3 学习与应用

来源:互联网 发布:visual studio c编程 编辑:程序博客网 时间:2024/04/29 22:17

今天的目标是Vector3 和部分 Transform。先说Vector3。

首先是Vector3的中英文API

Struct

Representation of 3D vectors and points.

表示3D的向量和点。

This structure is used throughout Unity to pass 3D positions and directions around. It also contains functions for doing common vector operations.

这个结构用于在Unity传递3D位置和方向。它也包含做些普通向量运算的函数。

Besides the functions listed below, other classes can be used to manipulate vectors and points as well. For example the Quaternion and the Matrix4x4 classes are useful for rotating or transforming vectors and points.

除了下面列出的函数,其他类用于处理向量和点。例如Quaternion和Matrix4x4类用于旋转或变换向量和点。

Variables变量
  • x
    X component of the vector.
    向量的X组件。
  • y
    Y component of the vector.
    向量的Y组件。
  • z
    Z component of the vector.
    向量的Z组件。
  • this [int index]
    Access the x, y, z components using [0], [1], [2] respectively.
    使用[0], [1], [2]分别访问组件x, y, z组件。简单来说就是用索引号代替x, y, z组件。
  • normalized
    Returns this vector with a magnitude of 1 (Read Only).
    返回向量的长度为1(只读)。
  • magnitude
    Returns the length of this vector (Read Only).
    返回向量的长度(只读)。
  • sqrMagnitude
    Returns the squared length of this vector (Read Only).
    返回这个向量的长度的平方(只读)。
Constructors构造器
  • Vector3
    Creates a new vector with given x, y, z components.
    创建一个新的具有给定x, y, z组件的向量。
Functions函数
  • Scale
    Multiplies every component of this vector by the same component of scale.
    由缩放的相同的组件对应乘以这个矢量的每个组件。
  • Normalize
    Makes this vector have a magnitude of 1.
    使向量的长度为1。
  • ToString
    Returns a nicely formatted string for this vector.
    返回此向量格式化的字符串。
Class Variables类变量
  • zero
    Shorthand for writing Vector3(0, 0, 0)
    写Vector3(0, 0, 0)的简码。
  • one
    Shorthand for writing Vector3(1, 1, 1)
    写Vector3(1, 1, 1)的简码。
  • forward
    Shorthand for writing Vector3(0, 0, 1)
    写Vector3(0, 0, 1)的简码,也就是向z轴。
  • up
    Shorthand for writing Vector3(0, 1, 0)
    写Vector3(0, 1, 0)的简码,也就是向y轴。
  • right
    Shorthand for writing Vector3(1, 0, 0)
    写Vector3(1, 0, 0)的简码,也就是向x轴。
Class Functions类函数
  • Lerp
    Linearly interpolates between two vectors.
    两个向量之间的线性插值。
  • Slerp
    Spherically interpolates between two vectors.
    球形插值在两个向量之间。
  • OrthoNormalize
    Makes vectors normalized and orthogonal to each other.
    使向量规范化并且彼此相互垂直。
  • MoveTowards
    Moves a point current towards target.
    当前的地点移向目标。
  • RotateTowards
    Rotates a vector current towards target.
    当前的向量转向目标。
  • SmoothDamp
    Gradually changes a vector towards a desired goal over time.
    随着时间的推移,逐渐改变一个向量朝向预期的目标。
  • Scale
    Multiplies two vectors component-wise.
    两个矢量组件对应相乘。
  • Cross
    Cross Product of two vectors.
    两个向量的交叉乘积。返回lhs x rhs
  • Reflect
    Reflects the vector along the normal.
    沿着法线反射向量。
  • Dot
    Dot Product of two vectors.
    两个向量的点乘积。
  • Project
    Projects a vector onto another vector.
    投影一个向量到另一个向量。
  • Angle
    Returns the angle in degrees between from and to.
    由from和to两者返回一个角度。
  • Distance
    Returns the distance between a and b.
    返回a和b之间的距离。
  • ClampMagnitude
    Returns a copy of vector with its magnitude clamped to maxLength.
    返回向量的长度,最大不超过maxLength所指示的长度。
  • Min
    Returns a vector that is made from the smallest components of two vectors.
    返回一个由两个向量的最小组件组成的向量。
  • Max
    Returns a vector that is made from the largest components of two vectors.
    返回一个由两个向量的最大组件组成的向量。
  • operator +
    Adds two vectors.
    两个向量相加。
  • operator -
    Subtracts one vector from another.
    一个向量减另一个向量。
  • operator *
    Multiplies a vector by a number.
    由一个数乘以一个向量。
  • operator /
    Divides a vector by a number.
    由一个数除一个向量。也就是a/b。
  • operator ==
    Returns true if the vectors are equal.
    如果两个向量相等,返回真。
  • operator !=
    Returns true if vectors different.
    如果向量不同返回真。


Vector3向量既可以用来表示位置,也可以用来表示方向。在数学与物理中,既有大小又有方向的量叫做向量(亦称矢量),与标量相对。

在立体三维坐标系中,分别取与x轴、y轴,z轴方向相同的3个单位向量i,j,k作为一组基底。若a为该坐标系内的任意向量,以坐标原点O为起点作向量OP=a。由空间基本定理知,有且只有一组实数(x,y, z),使得a=向量OP=xi+yj+zk,因此把实数对(x,y, k)叫做向量a的坐标,记作a=(x,y,z)。这就是向量a的坐标表示。其中(x,y,k),也就是点P的坐标。向量OP称为点P的位置向量。

更多向量知识请参考 www.google.com

另外Vector2的函数Vector3都有了,而且作用基本一致,就不另说了。

向量Vector3是我们最常打交道的一个类了。

首先是Vector3的变量
 1)x,y,z this[int index]
三个轴向,也可以用Vector3[i]i=[0,2]的整数来表示。

2)normalized (ReadOnly)
返回从坐标轴原点(0,0,0)到点P(x,y,z)的方向,向量的长度为 1。也就是说返回的向量的点P(x,y,z)到原点(0,0,0)的距离为1。这个很多时候被用来指示一个方向,然后再乘以想要的距离就可以得到我们想要的位置坐标。只能读取。

3)magnitude (ReadOnly)
返回向量的长度,也就是点P(x,y,z)到原点(0,0,0)的距离。最常用的是用来返回物体的移动速度
speed=rigidbody.velocity.magnitude;

只能读取。如果想自行规定距离可以先normalized然后乘以距离
speed=speed.normalized*objSpeed;

4)sqrMagnitude (ReadOnly)
返回向量的长度的两次方。大家知道向量的长度是用勾股定理计算出来的,计算机计算两次方和开跟的运算量比加减法要费时的多。所以如果是想比较两个向量的长度的话,用sqrMagnitude可以快很多。

函数
1)function Scale(scale : Vector3) : void
Description

Multiplies every component of this vector by the same componentof scale.

所有的轴向都乘以scale

static function Scale (a :Vector3, b : Vector3) : Vector3Description

Multiplies two vectors component-wise.

Every component in the result is a componentof a multiplied bythe same component of b.

把a的所有轴向都乘以b

2)function Normalize() : void
Description

Makes this vector have a magnitude of1.

让向量的长度变成1

When normalized, a vector keeps the same direction but its lengthis 1.0.

当normalized, 向量的方向不会变但是长度会变成1,0

Note that this function will change the current vector. If you wantto keep the current vector unchanged, use normalized variable.

这个函数会改变使用这个函数的向量,如果你不想改变这个向量,应该使用normalized

If this vector is too small to be normalized it will be set tozero

如果这个向量太小了,他不适合normalized并会被设置成zero (0,0,0)

 

3)function ToString() : string
Description

Returns a nicely formatted string for this vector

返回x,y,z的值,一般到小数点后一位。(0.0, 0.0, 0.0)


Class Variables

zero=(0,0,0)

one=(1,1,1)

forward=(0,0,1)

up=(0,1,0)

right=(1,0,0)

还有一个文档没写但也可以用的left=(-1,0,0)

Class Functions

1)static function Lerp(from : Vector3, to : Vector3, t : float) : Vector3

Description

Linearly interpolates between two vectors.

Interpolates from towards to byamount t.

t is clamped between [0...1].When t = 0returns from.When t = 1returns to.When t = 0.5 returnsthe averageof from and to.

跟Quaternion里的lerp一样,返回from 和to之间的一个Vector3 点p p=form+(to-form)*t   t[0,1]

2)static function Slerp (from: Vector3, to : Vector3, t : float) : Vector3

Description

Spherically interpolates between two vectors.

Interpolates from towards to byamount t. The returnedvector's magnitude willbe interpolated between magnitudesof from and to.

t is clamped between [0...1].

作用跟lerp差不多,但是不是呈直线靠近。

 

脚本:
var obj1:Transform;
var obj2:Transform;
var a:Vector3;
var b:Vector3;
varc=0.0;

a=Vector3.Lerp(obj1.position,obj2.position,c);
b=Vector3.Slerp(obj1.position,obj2.position,c);

if(Input.GetKeyDown("w"))
c+=0.1;
if(Input.GetKeyDown("s"))
c-=0.1;

输入:obj1.position=(0,0,0) obj2.position=(0,0,10)
输出:
c=0,a=(0,0,0), b=(0,0,0)
c=0.1a=(0,0,1), b=(0,0,1)
c=0.5,a=(0,0,5), b=(0,0,5)
c=1,a=(0,0,10), b=(0,0,10)

当from在(0,0,0)的时候,两个函数的效果差不多。但是当from和to都不在原点的时候

输入: obj1.position=(0,10,0)obj2.position=(0,10,10)
输出:
c=0, a=(0,10,0),b=(0,10,0)
c=0.1 a=(0,10,1),b=(0,10.4,0.8)
c=0.5, a=(0,10,5),b=(0,11.2,4.6)
c=1,a=(0,10,10), b=(0,10,10)

我们可以看到两者之间出现了差异。<img src="http://s16.sinaimg.cn/middle/4a2183a6t939097b01faf&690" name="image_operate_34941288149189062" alt="Vector3 学习与应用" title="Vector3 学习与应用" height="139" width="160" style="border: none; max-width: 100%;">
右图是两者移动轨迹的区别。

 

3)static function OrthoNormalize(ref normal : Vector3, ref tangent : Vector3): void

Description

Makes vectors normalized and orthogonal to each other.

Normalizes normal.Normalizes tangent andmakes sure it is orthogonalto normal (that is,angle between them is 90 degrees).

See Also: Normalize function.

这个函数读入两个向量normal和tangent, 使tangent 在由tangent和normal构成的平面上,并与normal垂直。两个向量都被Normalize,也就是长度为1。大家可以用这个脚本在编辑器中看出这个函数是如何工作的。

 

脚本:
var obj1: Transform;
var obj2: Transform;
var a: Vector3;
var b: Vector3;
var c:Vector3;
var d: Vector3;

a=obj1.position;
b=obj2.position;
c=a;
d=b;
Vector3.OrthoNormalize(a,b);
Debug.DrawLine(Vector3.zero,c,Color.blue);
Debug.DrawLine(Vector3.zero,d,Color.white);
Debug.DrawLine(Vector3.zero,a,Color.red);
Debug.DrawLine(Vector3.zero,b,Color.green);

<img src="http://s15.sinaimg.cn/middle/4a2183a6t93a311a5bb2e&690" alt="Vector3 学习与应用" title="Vector3 学习与应用" height="278" width="509" style="border: none; max-width: 100%;">

 

static function OrthoNormalize(ref normal : Vector3, ref tangent : Vector3, ref binormal :Vector3) : voidDescription

Makes vectors normalized and orthogonal to each other.

Normalizes normal.Normalizes tangent andmakes sure it is orthogonalto tangent.Normalizes binormal andmakes sure it is orthogonal toboth normal and tangent.

See Also: Normalize function.

 这个用法多了一个向量binormal,这个向量同时垂直于normal和tangent,三者关系类似三维的直角坐标轴。其中binormal的方向决定binormal在那边垂直于normal和tangent。

脚本:
var obj1: Transform;
var obj2: Transform;
var a: Vector3;
var b: Vector3;
var c:Vector3;
var d: Vector3;

a=obj1.position;
b=obj2.position;
e=transform.position;
c=a;
d=b;
f=e;
Vector3.OrthoNormalize(a,b,e);
Debug.DrawLine(Vector3.zero,c,Color.yellow);
Debug.DrawLine(Vector3.zero,d,Color.white);
Debug.DrawLine(Vector3.zero,f,Color.black);
Debug.DrawLine(Vector3.zero,a,Color.red);
Debug.DrawLine(Vector3.zero,b,Color.green);
Debug.DrawLine(Vector3.zero,e,Color.blue);
<img src="http://s14.sinaimg.cn/middle/4a2183a6t93a331bad04d&690" name="image_operate_69981288229067037" alt="Vector3 学习与应用" title="Vector3 学习与应用" height="344" width="564" style="border: none; max-width: 100%;">



4)static function RotateTowards(from : Vector3, to : Vector3, maxRadiansDelta: float,maxMagnitudeDelta : float) : Vector3
Description

Rotates a vector/from/towards to.

The vector will be rotated on an arc instead of being interpolatedlinearly. This is essentially the same as Vector3.Slerp butinstead the function will ensure that the angular speed and changeof magnitude neverexceeds maxRadiansDelta and maxMagnitudeDelta.


产生一个向量从from旋转并移动到 to,跟Vector3.Slerp类似,但是可以用maxRadiansDelta和maxMagnitudeDelta分别控制向量的旋转方向和长度。当maxRadiansDelta=0时,向量指向from, 1则指向to。当maxMagnitudeDelta=0时,向量的长度=from.magnitude,1则=to.magnitude。向量的最大有效取值范围为1, 但是最小取值范围可以小于0

maxMagnitudeDelta, maxRadiansDelta [0,-∞]

当maxRadiansDelta和maxMagnitudeDelta为负时,向量会向相反方向旋转和延长。


5)function Scale(scale : Vector3) : void

Description

Multiplies every component of this vector by the same componentof scale.

将两个向量的对应轴相乘。

a,b ∈Vector3

a.Scale(b);

a.x=a.x * b.x     a.y=a.y*b.y   a.z=a.z*b.z

static function Scale (a :Vector3, b : Vector3) : Vector3Description

Multiplies two vectors component-wise.

Every component in the result is a componentof a multiplied bythe same component of b.

跟上一个差不多

a,b,c ∈Vector3

 a=Vector3.Scale(b,c);

a.x=b.x * c.x    a.y=b.y*c.y   a.z=b.z*c.z


6)static function Cross (lhs: Vector3, rhs : Vector3) : Vector3

Description

Cross Product of two vectors.

Returns lhs x rhs.

两个向量的叉积,具体关于叉积的解释大家可以自己去网上搜搜,或点击传送门。

http://zh.wikipedia.org/zh/向量积

http://baike.baidu.com/view/865221.htm

 

a = a1i + a2j + a3k =[a1, a2, a3]b = b1i + b2j + b3k =[b1, b2, b3]

a × b =[a2b3 −a3b2,a3b1 −a1b3,a1b2 −a2b1]

 

7)static function Reflect(inDirection : Vector3, inNormal : Vector3) : Vector3

Description

Reflects the vector along the normal.

The returned valueis inDirection reflectedfrom a surface with anormal inNormal.

又一个很常用的函数,返回一个向量,让这个向量与另一个向量inDirection以坐标轴inNormal为准镜像。

 

脚本:
varobj1: Transform;
varobj2: Transform;
var a:Vector3;
var d:Vector3;
var e:Vector3;
var f:Vector3;

a=obj1.position;
b=obj2.position;
e=Vector3.Reflect(a,Vector3.right);//以y轴,z轴组成的面为分割线,让a和e在x轴上镜像。
f=Vector3.Reflect(b,Vector3.right);
Debug.DrawLine(Vector3.zero,f,Color.black);
Debug.DrawLine(Vector3.zero,e,Color.blue);
Debug.DrawLine(Vector3.zero,a,Color.red);
Debug.DrawLine(Vector3.zero,b,Color.green);

<img src="http://s4.sinaimg.cn/middle/4a2183a6t93a4548b2063&690" name="image_operate_4541288233949955" alt="Vector3 学习与应用" title="Vector3 学习与应用" height="389" width="438" style="border: none; max-width: 100%;">


 8static function Dot(lhs : Vector3, rhs : Vector3) : float

Description

Dot Product of two vectors.

Returns lhs . rhs.

For normalized vectorsDot returns 1 if they point in exactly the same direction; -1 ifthey point in completely opposite directions; and a number inbetween for other cases (e.g. Dot returns zero if vectors areperpendicular).

For vectors of arbitrary length the Dot return values are similar:they get larger when the angle between vectors decreases.

点积,跟quaternion里的用法一样。对于normalized后的lhs和rhs,如果指向相同的方向,返回1。返回-1如果他们指向完全相反的方向。其他情况下根据角度返回两者之间的小数。如果两个向量互相垂直,返回0;

 


两个向量u,v的点积是一个标量,用u·v表示。通用公式:|u||v|cos<u,v>。在三维空间中代数公式:uxvx+ uyvy +uzvz。(该公式可以先由二维证明,再推广到多维。二维中的证明:利用点线距公式和勾股定理推出|u|*cos<u,v>的表达式,再根据定义化简即可。)
  点积的值由以下三个值确定:
  u的大小v的大小u,v夹角的余弦。在u,v非零的前提下,点积如果为负,则u,v形成的角大于90度;如果为零,那么u,v垂直;如果为正,那么u,v形成的角为锐角。
  点积得到两个向量的夹角的cos值,通过它可以知道两个向量的相似性,利用点积可判断一个多边形是否面向摄像机还是背向摄像机

 一般情况下还是Vector3.Angle()这个函数用的比较多,两者的功能基本是一样的。


9)static function Project(vector : Vector3, onNormal : Vector3) : Vector3

Description

Projects a vector onto another vector.

Returns vector projectedonto onNormal. Returns zero vectorif onNormal isalmost zero.

投射一个向量到另一个。

返回一个向量,这个向量由vector投射到onNormal。 返回0如果onNormal几乎等于或等于0;

具体工作原理可以通过这个脚本了解到

 

脚本:
varobj1: Transform;
varobj2: Transform;
var a:Vector3;
var b:Vector3;
var c:Vector3;


a=obj1.position;
b=obj2.position;
c=Vector3.Project(a,b);

Debug.DrawLine(Vector3.zero,c,Color.yellow);
Debug.DrawLine(a,c,Color.white);
Debug.DrawLine(Vector3.zero,a,Color.red);
Debug.DrawLine(Vector3.zero,b,Color.green);
<img src="http://s16.sinaimg.cn/middle/4a2183a6t93a70938aa5f&690" alt="Vector3 学习与应用" title="Vector3 学习与应用" height="332" width="388" style="border: none; max-width: 100%;">


当把obj2的位置设定为(0,1,0)的时候,大家就很容易看出这个函数是如何工作的了

<img src="http://s11.sinaimg.cn/middle/4a2183a6t93a6da99e09a&690" alt="Vector3 学习与应用" title="Vector3 学习与应用" height="360" width="289" style="border: none; max-width: 100%;">

 

10)static function Angle (from: Vector3, to : Vector3) : float

Description

Returns the angle in degreesbetween from and to.

这个基本上是我用的非常多的一个函数,用来确定两个向量指向的位置间的夹角

返回从from到to的夹角,单位为度。

我一般用这个来测量坦克之类的车辆的y轴和空间坐标y轴之间的夹角,阻止其登上大于固定角度的斜坡。或者ai只会看见正前方多少度内的敌人,因为他不应该拥有360度的视野。


11)static function Distance (a: Vector3, b : Vector3) : float

Description

Returns the distancebetween a and b.

Vector3.Distance(a,b) isthe same as (a-b).magnitude

返回从a到b的距离

使用这个函数Vector3.Distance(a,b)的结果跟直接在两个向量间作减法(a-b)后求出的向量的长度是一样的。

也是非常非常常用到的一个函数。

12)static function Min(lhs : Vector3, rhs : Vector3) : Vector3

Description

Returns a vector that is made from the smallest componentsof lhs and rhs.

返回lhs和rhs中最小的那个向量

13)static function Max (lhs :Vector3, rhs : Vector3) : Vector3

Description

Returns a vector that is made from the largest componentsof lhs and rhs.

返回lhs和rhs中最大的那个向量

这两个也是很常用的。


14)operators

+ 和-

两个向量相加和相减

*和/

一个向量的所有轴乘以或除以一个float或int, 得到一个新的向量

原创粉丝点击