Unity3D
来源:互联网 发布:域名备案还是网站备案 编辑:程序博客网 时间:2024/05/22 07:57
向量(又称矢量)是游戏开发过程中非常重要的概念,它是用于描述具有大小和方向两个属性的物理量,例如物体运动的速度、加速度、摄像机观察的方向、刚体受到的力都是向量。
在数学中,既有大小又有方向的量就是向量。在几何中,向量可以用一段有向线段来表示:
向量的运算
加减
向量的加法(减法)为各自分量分别相加(相减)。在物理上可以用来计算两个力的合力,或者几个速度分量的叠加。
数乘
向量与一个标量相乘为数乘。数乘可以对向量的长度进行缩放,如果标量大于0,那么向量的方向不变,若标量小于0,则向量的方向会变为反方向。
点乘(也称为数量积,还称为内积,英文叫做:Dot Product)
两个向量点乘得到一个标量,数值等于两个向量长度相乘后再乘以二者夹角的余弦。
通过两个向量点乘结果的符号可以快速的判断两个向量的夹角情况:
若u*v = 0,则向量u、v相互垂直。
若u*v > 0,则向量u、v夹角小于90度。
若u*v < 0,则向量u、v夹角大于90度。
叉乘(也称为向量积,还称为叉积,英文叫做:Cross Product)
两个向量的叉乘得到一个新的向量,新向量垂直于原来的两个向量,并且长度等于原来向量长度相乘后再乘夹角的余弦值。
叉乘不满足交换律,即a*b != b*a。
Vector3类
官方手册:https://docs.unity3d.com/ScriptReference/Vector3.html
中文API:http://www.ceeger.com/Script/Vector3/Vector3.html
源码:https://github.com/biezhihua/Unity3DTutorials/blob/master/Api/Vector3
magnitude (向量的模长)
向量模长很简单分量平方相加再开根号,下面是其源码:
public static float Magnitude(Vector3 vector){ return Mathf.Sqrt((float) ((double) vector.x * (double) vector.x + (double) vector.y * (double) vector.y + (double) vector.z * (double) vector.z));}
normalized (单位化后的向量)
9.99999974737875E-06是科学计数法表示的,实际上是0.000009…
public static Vector3 Normalize(Vector3 value){ float num = Vector3.Magnitude(value); if ((double) num > 9.99999974737875E-06) return value / num; return Vector3.zero;}
规范化后的结果常常是:0,-1,1。
例如Vector3(-0.2,0,0)执行规范化,其num值是0.2,然后计算-0.2/0.2得到-1,那么最后的结果为(-1,0,0)。
Lerp(线性插值函数)
使用Lerp函数,可以做到简单的缓动效果(物体A线性移动到B物体处)。
// Update is called once per framevoid Update () { transform.position = Vector3.Lerp(start.position, end.position, Time.deltaTime);}
虽然Lerp是线性插值函数,但是由于受到Time.deltaTime的影响会有小幅度变化,但是很细微,实际上不用考虑。
public static Vector3 Lerp(Vector3 a, Vector3 b, float t){ t = Mathf.Clamp01(t); return new Vector3(a.x + (b.x - a.x) * t, a.y + (b.y - a.y) * t, a.z + (b.z - a.z) * t);}
SmoothDamp
如果想让值改变的更平滑一些,可以使用此函数,它和Mathf.SmoothDamp()方法效果一致。
官方手册:https://docs.unity3d.com/ScriptReference/Vector3.SmoothDamp.html
[ExcludeFromDocs]public static Vector3 SmoothDamp(Vector3 current, Vector3 target, ref Vector3 currentVelocity, float smoothTime){ float deltaTime = Time.deltaTime; float maxSpeed = float.PositiveInfinity; return Vector3.SmoothDamp(current, target, ref currentVelocity, smoothTime, maxSpeed, deltaTime);}public static Vector3 SmoothDamp(Vector3 current, Vector3 target, ref Vector3 currentVelocity, float smoothTime, [DefaultValue("Mathf.Infinity")] float maxSpeed, [DefaultValue("Time.deltaTime")] float deltaTime){ smoothTime = Mathf.Max(0.0001f, smoothTime); float num1 = 2f / smoothTime; float num2 = num1 * deltaTime; float num3 = (float) (1.0 / (1.0 + (double) num2 + 0.479999989271164 * (double) num2 * (double) num2 + 0.234999999403954 * (double) num2 * (double) num2 * (double) num2)); Vector3 vector = current - target; Vector3 vector3_1 = target; float maxLength = maxSpeed * smoothTime; Vector3 vector3_2 = Vector3.ClampMagnitude(vector, maxLength); target = current - vector3_2; Vector3 vector3_3 = (currentVelocity + num1 * vector3_2) * deltaTime; currentVelocity = (currentVelocity - num1 * vector3_3) * num3; Vector3 vector3_4 = target + (vector3_2 + vector3_3) * num3; if ((double) Vector3.Dot(vector3_1 - current, vector3_4 - vector3_1) > 0.0) { vector3_4 = vector3_1; currentVelocity = (vector3_4 - vector3_1) / deltaTime; } return vector3_4;}
- Unity3D
- Unity3D
- Unity3D
- Unity3D
- unity3d
- Unity3D
- Unity3D
- Unity3D
- UNITY3D
- Unity3D
- unity3d
- unity3D
- Unity3D
- Unity3d
- Unity3D
- unity3D
- unity3D
- Unity3D
- hibernate 实现多表连接查询(转载)
- maven依赖原则
- 一个在线音乐软件的故事(二、其实故事从这里才开始)
- Python 正确重载运算符
- 动态障碍物操作详细解析
- Unity3D
- lingo导入excel数据具体实现步骤
- Java Bean登录页面实现
- C++静态成员函数访问非静态成员的几种方法
- 2017.08.26 360笔试题:小明卖粉笔
- Linux下配置NDK、JDK环境变量
- zzuli2183—分步打表
- java 寻找数组中第k大的值
- 内核对象(2)之跨越边界进程共享内核对象