代码改变世界

向量

2016-02-20 22:36  紫枫@猎人  阅读(113)  评论(0编辑  收藏  举报

加法

可计算位移

U + V 

减法

求得两个向量的方向

U-V = VU

using UnityEngine;
using System.Collections;

public class Test : MonoBehaviour {

    private Vector3 target = new Vector3(100,0,100);
    private Vector3 normal;
    // Use this for initialization
    void Start () {
//U - V = VU normal
= (target - transform.position).normalized; } // Update is called once per frame void Update () { if (Vector3.Distance(target,transform.position) > 0.1) { transform.position += 0.1f * normal; } else { transform.position = target; } } }

点乘

Unity中用于计算角度

一般来说,点乘结果描述了两个向量的“相似”程度,点乘结果越大,两个向量越相近,

点乘和向量间的夹角相关 

计算两向量间的夹角    θ = arccos(a·b)

叉乘

叉乘得到的向量垂直于原来的两个向量。

a × b 的长度等于向量的大小与向量夹角sin值的积,||a × b|| = ||a|| ||b|| sinθ 

  • 叉乘最重要的应用就是创建垂直于平面、三角形、多边形的向量。