Vector3 学习与应用

来源:互联网 发布:淘宝网京东商城电器 编辑:程序博客网 时间:2024/05/17 00:14
今天的目标是Vector3 和部分 Transform。 先说Vector3。

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

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

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

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

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

Description

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

所有的轴向都乘以scale

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

Description

Multiplies two vectors component-wise.

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

把a的所有轴向都乘以b

2)function Normalize () : void

Description

Makes this vector have a magnitude of 1.

让向量的长度变成1

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

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

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

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

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

如果这个向量太小了,他不适合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 by amount t.

t is clamped between [0...1]. When t = 0 returns from. When t = 1 returns to. When t = 0.5 returns the average of 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 by amount t. The returned vector's magnitude will be interpolated between magnitudes of from and to.

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

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

 

脚本:
var obj1: Transform;
var obj2: Transform;
var a: Vector3;
var b: Vector3;
var c=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.1 a=(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)

我们可以看到两者之间出现了差异。Vector3 <wbr>学习与应用
右图是两者移动轨迹的区别。

 

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

Description

Makes vectors normalized and orthogonal to each other.

Normalizes normal. Normalizes tangent and makes sure it is orthogonal to 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);

Vector3 <wbr>学习与应用

 

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

Description

Makes vectors normalized and orthogonal to each other.

Normalizes normal. Normalizes tangent and makes sure it is orthogonal to tangent. Normalizes binormal and makes sure it is orthogonal to both 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);
Vector3 <wbr>学习与应用



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 interpolated linearly. This is essentially the same as Vector3.Slerp but instead the function will ensure that the angular speed and change of magnitude never exceeds maxRadiansDelta and maxMagnitudeDelta.


产生一个向量从from旋转并移动到 to,跟Vector3.Slerp类似,但是可以用maxRadiansDelta和max MagnitudeDelta分别控制向量的旋转方向和长度。当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 component of 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) : Vector3

Description

Multiplies two vectors component-wise.

Every component in the result is a component of a multiplied by the 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 = [a1a2a3]
b b1i b2j b3k = [b1b2b3]

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 value is inDirection reflected from a surface with a normal inNormal.

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

 

脚本:
var obj1: Transform;
var obj2: 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);

Vector3 <wbr>学习与应用


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

Description

Dot Product of two vectors.

Returns lhs . rhs.

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

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;

  Vector3 <wbr>学习与应用

两个向量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 projected onto onNormal. Returns zero vector if onNormal is almost zero.

投射一个向量到另一个。

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

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

 

脚本:
var obj1: Transform;
var obj2: 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);
Vector3 <wbr>学习与应用


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

Vector3 <wbr>学习与应用

 

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

Description

Returns the angle in degrees between from and to.

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

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

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


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

Description

Returns the distance between a and b.

Vector3.Distance(a,b) is the 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 components of lhs and rhs.

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

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

Description

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

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

这两个也是很常用的。


14)operators

+ 和-

两个向量相加和相减

*和/

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

==和!=

不知道的该打屁股

0 0
原创粉丝点击