点乘叉乘的应用

目录

定义

Vector3.normalized 规范化

Vector3.magnitude 长度

应用

点乘(扇形攻击检测)

叉乘(判断顺逆方向)


定义

弧长:等于半径的弧,其所对的圆心角为1弧度。

圆周长: 2πr。

 

 

上图得出:半径 = r1 = r2 = r3 = 1rad。

周长 = 2πr1 = 2πr2 = 2πr3。

一周的弧 = 2π*(1rad)= 360°

1rad = 180°/π≈57.3°

1°= 1rad / 57.3° = 1rad / (180°/π) = 1rad *π/180°= π/180°rad ≈ 0.017°rad。

 

Vector3.normalized 规范化

注意:当前的向量没有被改变,只是返回一个新的规范化的向量。如果你想要规范这个向量,使用Normalize函数。

Vector3.magnitude 长度

返回向量的长度。向量的长度是(x*x+y*y+z*z)的平方根。

 

应用

点乘(扇形攻击检测)

    /// <summary>
    /// 扇形攻击检测
    /// </summary>
    public bool UmberllaAttact(Transform attacter, Transform attacked, float angle, float radius)
    {
        Vector3 deltaA = attacked.position - attacter.position;//距离
        float tempAngle = Mathf.Acos(Vector3.Dot(deltaA.normalized, attacter.forward)) * Mathf.Rad2Deg;
        Debug.Log("角度:" + tempAngle + "____检测的范围角度 :" + angle + "距离 :" + deltaA.magnitude + "__检测的范围距离 :" + radius);
        if (tempAngle < angle * 0.5f && deltaA.magnitude < radius)
        {
            return true;
        }
        return false;
    }

叉乘(判断顺逆方向)

    /// <summary>
    /// 叉乘 判断 b 在 a 的顺时针或者逆时针方向
    /// </summary>
    private void TestCross(Vector3 a, Vector3 b)
    {
        //计算向量 a、b 的叉积,结果为 向量 
        Vector3 c = Vector3.Cross(a, b);

        float radians = Mathf.Asin(Vector3.Distance(Vector3.zero, Vector3.Cross(a.normalized, b.normalized)));
        float angle = radians * Mathf.Rad2Deg;
        if (c.y > 0)
        {
            Debug.Log("b 在 a 的顺时针方向");
        }
        else if (c.y == 0)
        {
            Debug.Log("b 和 a 方向相同(平行)");
        }
        else
        {
            Debug.Log("b 在 a 的逆时针方向");
        }
    }

猜你喜欢

转载自blog.csdn.net/m1234567q/article/details/105281414
今日推荐