Unity判断线和平面的交点

//线和平面的交点
    private Vector3 GetIntersectWithLineAndPlane(Vector3 point, Vector3 direct, Vector3 planeNormal, Vector3 planePoint)
    {
        float d = Vector3.Dot(planePoint - point, planeNormal) / Vector3.Dot(direct.normalized, planeNormal);
        //print(d);
        return d * direct.normalized + point;
    }

 

//判断交点是否在平面内
    public bool IsVecPosPlane(List<Vector3> vecs, Vector3 pos)
    {
        float RadianValue = 0;
        Vector3 vecOld = Vector3.zero;
        Vector3 vecNew = Vector3.zero;
        for (int i = 0; i < vecs.Count; i++)
        {
            if (i == 0)
            {
                vecOld = vecs[i] - pos;
            }
            if (i == vecs.Count - 1)
            {
                vecNew = vecs[0] - pos;
            }
            else
            {
                vecNew = vecs[i + 1] - pos;
            }
            //RadianValue += Mathf.Acos(Vector3.Dot(vecOld.normalized, vecNew.normalized)) * Mathf.Rad2Deg;
            RadianValue += Vector3.Angle(vecOld.normalized, vecNew.normalized);
            vecOld = vecNew;
        }
        //Debug.Log("RadianValue: " + RadianValue);
        if (Mathf.Abs(RadianValue - 360) < 0.1f)
        {
            return true;
        }
        else
        {
            return false;
        }
    }
posted @ 2022-12-18 15:52  ljy3268  阅读(392)  评论(0编辑  收藏  举报