Application of point product and cross product

Table of contents

definition

Vector3.normalized normalization

Vector3.magnitudelength

application

Dot Multiplication (Fan Attack Detection)

Cross product (judging forward and reverse direction)


definition

Arc Length: An arc equal to the radius subtended by a central angle of 1 radian.

Circumference: 2πr.

 

 

The above figure gives: Radius = r1 = r2 = r3 = 1rad.

Circumference = 2πr1 = 2πr2 = 2πr3.

One week of arc = 2π*(1rad) = 360°

1rad = 180°/π≈57.3°

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

 

Vector3.normalized normalization

Note: The current vector is not changed, just returns a new normalized vector. If you want to normalize this vector, use the Normalize function.

Vector3.magnitudelength

Returns the length of the vector. The length of the vector is the square root of (x*x+y*y+z*z).

 

application

Dot Multiplication (Fan Attack Detection)

    /// <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;
    }

Cross product (judging forward and reverse direction)

    /// <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 的逆时针方向");
        }
    }

 

Guess you like

Origin blog.csdn.net/m1234567q/article/details/105281414