向量的点乘 a * b
高中数学中我们可以得到公式 a * b = |a| * |b| * cos<a,b>
可以使用点乘获取两个向量的前后位置,如下图所示
案例一(案例中将y去掉,相当于俯视坐标系之后x,z):
Vector3 a = new Vector3 (1,0,2);
Vector3 b = new Vector3 (2,0,1);
// Use this for initialization
void Start () {
Debug.LogError(Vector3.Angle(a,b));//36.8699
Debug.LogError(Vector3.Dot(b,a));//4
Debug.LogError(Vector3.Dot(b,d));//-5
Debug.LogError(Vector3.Dot(a,d));//-4
Debug.LogError(Vector3.Dot(b,c));//3
}
上面的代码中我们可以看出来 ∠aob = 36.8699°
根据点乘公式:向量a * 向量b = |a| * |b| * cos36.8699° = 根5 * 根5 * cos36.8699° = 4;