Unity3D自学笔记——脚本开发中的3D数学

向量概念

  • 向量:具有大小和方向的量。
  • 向量的大小常被称为向量的长度和模
  • 标量:只有大小没有方向的量

如:位移和速度是向量,因为不仅包含大小还有方向,而距离和速率是标量,因为其不指明任何方向。

向量的运算

  • 向量的模
    若向量a = (a1, a2)
    则N(a) = √(a1^2 + a2^2 )

    若向量a = (a1, a2, a3)
    则N(a) = √(a1^2 + a2^2 + a3^2)

  • 向量的加法
    若向量a = (a1, a2),向量b = (b1, b2)
    则 a + b = (a1 + b1, a2 + b2)

    若向量a = (a1, a2, a3),向量b = (b1, b2, b3)
    则 a + b = (a1 + b1, a2 + b2, a3 + b3)

  • 向量的减法
    若向量a = (a1, a2),向量b = (b1, b2)
    则 a - b = (a1 - b1, a2 - b2)

    若向量a = (a1, a2, a3),向量b = (b1, b2, b3)
    则 a - b = (a1 - b1, a2 - b2,a3 - b3)

  • 向量的点积
    若向量a = (a1, a2),向量b = (b1, b2)
    则 a • b = a1 * b1 + a2 * b2

    若向量a = (a1, a2, a3),向量b = (b1, b2, b3)
    则a • b = a1 * b1 + a2 * b2 + a3 * b3

    若a • b = 0, 则a⊥b

    点积描述了两个向量的相似程度,结果越大两向量越相似,还可表示投影

  • 向量的叉积
    若向量a = (a1, a2),向量b = (b1, b2)
    a × b = a1 * b2 - a2 * b1

    若向量a = (a1, a2, a3),向量b = (b1, b2, b3)
    则a X b = (a2 * b3 - a3 * b2, a3 * b1 - a1 * b3, a1 * b2 - a2 * b1)

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

Vector3 的静态变量和静态方法

  • 静态变量

    magnitude :向量的模
    sqrMagnitude : 未开根号的向量的模
    normailzed : 返回向量的长度为1

  • 静态方法
    public static Vector3 ClampMagnitude(Vector3 vector, float maxLength);
    由from和to两者返回一个角度。形象的说,from和to的连线和它们一个指定轴向的夹角。

public static Vector3 ClampMagnitude(Vector3 vector, float maxLength);
返回向量的长度,最大不超过maxLength所指示的长度。

```
public float radius = 1;
Vector3 v;    
void Update()
{
    //只能在半径为radius的球体内移动
    v = transform.position;
    v = Vector3.ClampMagnitude(v, radius);
    transform.position = v;
}
```

public static Vector3 Cross(Vector3 lhs, Vector3 rhs);
两个向量的叉积

public static float Distance(Vector3 a, Vector3 b);
返回a和b之间的距离。

public static float Dot(Vector3 lhs, Vector3 rhs);
两个向量的点积。对于normalized向量,如果他们指向在完全相同的方向,Dot返回1。如果他们指向完全相反的方向,返回-1。对于其他的情况返回一个数(例如:如果是垂直的Dot返回0)。
public static Vector3 Max(Vector3 lhs, Vector3 rhs);
返回一个由两个向量的最大组件组成的向量。

public Vector3 a = new Vector3(1, 2, 3);
    public Vector3 b = new Vector3(4, 3, 2);
    public void Awake() {
        print(Vector3.Max(a, b));
        // prints (4.0,3.0,3.0)
    }

public static Vector3 Min(Vector3 lhs, Vector3 rhs);
返回一个由两个向量的最小组件组成的向量。

public Vector3 a = new Vector3(1, 2, 3);
    public Vector3 b = new Vector3(4, 3, 2);
    public void Awake() {
        print(Vector3.Min(a, b));
    }
    // prints (1.0,2.0,2.0)

public static Vector3 Scale(Vector3 a, Vector3 b);
由缩放的相同的组件对应乘以这个矢量的每个组件。

print(Vector3.Scale(new Vector3(1, 2, 3), new Vector3(2, 3, 4)));
// prints (2.0,6.0,12.0)

public static void OrthoNormalize(ref Vector3 normal, ref Vector3 tangent);
public static void OrthoNormalize(ref Vector3 normal, ref Vector3 tangent, ref Vector3 binormal);
使向量规范化并且彼此相互垂直。

 public Transform obj1;
    public Transform obj2;

    Vector3 a, b, c, d, e, f;

    void Update()
    {
        a = transform.position;
        b = obj1.position;
        c = obj2.position;

        d = a;
        e = b;
        f = c;

        Vector3.OrthoNormalize(ref a, ref b, ref c);
        Debug.DrawLine(Vector3.zero, d, Color.yellow);
        Debug.DrawLine(Vector3.zero, e, 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, c, Color.blue);
    }

public static Vector3 Project(Vector3 vector, Vector3 onNormal);
vector到onNormal的投影

public Transform target;
    public Vector3 rail;

    void Update()
    {
        Vector3 heading = target.position - transform.position;
        Vector3 project = Vector3.Project(heading, rail);

        Debug.DrawLine(transform.position, target.position, Color.green);
        Debug.DrawLine(transform.position, transform.position + project, Color.red);
    }

public static Vector3 ProjectOnPlane(Vector3 vector, Vector3 planeNormal);
将一个向量投射到由一个垂直于平面的法线所定义的平面上。

public static Vector3 Reflect(Vector3 inDirection, Vector3 inNormal);
将一个向量根据inNormal定义的平面进行反射

public Transform target;
    public Vector3 rail;

    void Update()
    {

        Vector3 heading = transform.position - target.position;
        Vector3 reflect = Vector3.Reflect(heading, rail);

        Debug.DrawLine(transform.position, target.position, Color.green);
        Debug.DrawLine(transform.position, transform.position + reflect, Color.red);
    }
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值