Unity之向量

向量表示的是方向和大小,与位置距离无关


三维空间的表示如下


在unity3d中采用的struct来描述的Vector3

[csharp]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. namespace UnityEngine  
  2. {  
  3.     public struct Vector3  
  4.     {  
  5.   
  6.         public float x;  
  7.         public float y;  
  8.         public float z;  
  9.          }  
  10. }  


向量的长度:向量的大小(或长度)称为向量的模


[csharp]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. public float magnitude  
  2. {  
  3.     get  
  4.     {  
  5.         return Mathf.Sqrt(this.x * this.x + this.y * this.y + this.z * this.z);  
  6.     }  
  7. }  

[csharp]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. public float sqrMagnitude  
  2. {  
  3.     get  
  4.     {  
  5.         return this.x * this.x + this.y * this.y + this.z * this.z;  
  6.     }  
  7. }  


三维空间中两点的距离

[csharp]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. public static float Distance(Vector3 a, Vector3 b)  
  2. {  
  3.     Vector3 vector = new Vector3(a.x - b.x, a.y - b.y, a.z - b.z);  
  4.     return Mathf.Sqrt(vector.x * vector.x + vector.y * vector.y + vector.z * vector.z);  
  5. }  


向量加法

[csharp]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. public static Vector3 operator +(Vector3 a, Vector3 b)  
  2. {  
  3.     return new Vector3(a.x + b.x, a.y + b.y, a.z + b.z);  
  4. }  


向量减法


[csharp]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. public static Vector3 operator -(Vector3 a, Vector3 b)  
  2. {  
  3.     return new Vector3(a.x - b.x, a.y - b.y, a.z - b.z);  
  4. }  


向量点积(dot product)又称数量积或内积


[csharp]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. public static float Dot(Vector3 lhs, Vector3 rhs)  
  2. {  
  3.     return lhs.x * rhs.x + lhs.y * rhs.y + lhs.z * rhs.z;  
  4. }  

根据公式A.B = |A||B|cos(a)得出两个向量之间的弧度的角度

[csharp]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. public static float AngleBetween(Vector3 from, Vector3 to)  
  2.     {  
  3.         return Mathf.Acos(Mathf.Clamp(Vector3.Dot(from.normalized, to.normalized), -1f, 1f));  
  4.     }  

[csharp]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. public static float Angle(Vector3 from, Vector3 to)  
  2.     {  
  3.         return Mathf.Acos(Mathf.Clamp(Vector3.Dot(from.normalized, to.normalized), -1f, 1f)) * 57.29578f;//角度  
  4.     }  

弧度=角度乘以π后再除以180

角度=弧度除以π再乘以180


其中a是A和B在3D空间中的夹角。如果已知两个向量,使用数量积我们就可以通过计算求得两个向量的夹角


判断目标在自己的前后方位可以使用下面的方法:

Vector3.Dot(transform.forward, target.position)

返回值为正时,目标在自己的前方,反之在自己的后方



向量叉积 :U和V的向量积(cross product,矢量积或外积)产生一个向量,它垂直于U和V,公式:U × V = n |U| |V| sin(a),其中n为垂直于U和V的单位向量,a是U和V的夹角


[csharp]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. public static Vector3 Cross(Vector3 lhs, Vector3 rhs)  
  2. {  
  3.     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);  
  4. }  


 判断目标在自己的左右方位可以使用下面的方法:

 Vector3.Cross(transform.forward, target.position).y

 返回值为正时,目标在自己的右方,反之在自己的左方



数乘向量:实数λ与向量b的积是一个向量,记作:a=λb。规定:当λ为正时,同向;当λ为负时,反向;实数λ,叫做向量的系数。数乘向量的几何意义就是把向量沿着相同方向或反方向放大或缩小

[csharp]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. public static Vector3 operator *(Vector3 a, float d)  
  2. {  
  3.     return new Vector3(a.x * d, a.y * d, a.z * d);  
  4. }  

[csharp]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. public static Vector3 operator *(float d, Vector3 a)  
  2. {  
  3.     return new Vector3(a.x * d, a.y * d, a.z * d);  
  4. }  

向量比较:

[csharp]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. public static bool operator ==(Vector3 lhs, Vector3 rhs)  
  2. {  
  3.     return Vector3.SqrMagnitude(lhs - rhs) < 9.99999944E-11f;  
  4. }  
[csharp]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. public static bool operator !=(Vector3 lhs, Vector3 rhs)  
  2. {  
  3.     return Vector3.SqrMagnitude(lhs - rhs) >= 9.99999944E-11f;  
  4. }  

向量的规范化: 向量的规范化也称(归一化)就是使向量的模变为1,即变为单位向量。可以通过将向量都除以该向量的模来实现向量的规范化。规范化后的向量相当于与向量同方向的单位向量,可以用它表示向量的方向。由于方向的概念在3D编程中非常重要,所以此概念也很重要,单位向量有很多重要的性质,在表示物体表面的法线向量时用的更是频繁 

[csharp]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. public static Vector3 Normalize(Vector3 value)  
  2. {  
  3.     float num = Vector3.Magnitude(value);  
  4.     if (num > 1E-05f)  
  5.     {  
  6.         return value / num;  
  7.     }  
  8.     return Vector3.zero;  
  9. }  


投影:一般用于透视,下图u'是u在v上的投影,向量u和v的夹角为theta,d就是u’的长度,而u’和v的方向是相同的,v/|v|也就是u’的方向

[csharp]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. public static Vector3 Project(Vector3 vector, Vector3 onNormal)  
  2. {  
  3.     float num = Vector3.Dot(onNormal, onNormal);  
  4.     if (num < 1.17549435E-38f)  
  5.     {  
  6.         return Vector3.zero;  
  7.     }  
  8.     return onNormal * Vector3.Dot(vector, onNormal) / num;  
  9. }  

反射向量: 下图入射光线向量I和平面法向量N,R为反射向量,R=I-2(I.N)N

推导如下:

设入射光线向量I和反射平面的法向量N之间的夹角为theta。连接I的始端和R的末端,则有R = 2P - I 

设入射点0到P与N的交点的向量为S,那么有P = I + S 

向量S即向量-N(注意,这里是-N,因为S和N的方向相反。)在向量N上的投影,根据向量的投影公式有,简化后有:S=-(I.N)N,将R和P代入,有R=I-2(I.N)N


[csharp]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. public static Vector3 Reflect(Vector3 inDirection, Vector3 inNormal)  
  2. {  
  3.     return -2f * Vector3.Dot(inNormal, inDirection) * inNormal + inDirection;  
  4. }  

  • 6
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值