坐标转换一些小功能

 

/// <summary>
    /// 确定坐标是否在平面内
    /// </summary>
    /// <returns></returns>
    private bool IsVecPosPlane(Vector3[] vecs, Vector3 pos)
    {
        float RadianValue = 0;
        Vector3 vecOld = Vector3.zero;
        Vector3 vecNew = Vector3.zero;
        for (int i = 0; i < vecs.Length; i++)
        {
            if (i == 0)
            {
                vecOld = vecs[i] - pos;
            }
            if (i == vecs.Length - 1)
            {
                vecNew = vecs[0] - pos;
            }
            else
            {
                vecNew = vecs[i + 1] - pos;
            }
            RadianValue += Mathf.Acos(Vector3.Dot(vecOld.normalized, vecNew.normalized)) * Mathf.Rad2Deg;
            vecOld = vecNew;
        }
        if (Mathf.Abs(RadianValue - 360) < 0.1f)
        {
            return true;
        }
        else
        {
            return false;
        }
        //vecOld = vecs[0] - pos;
        //vecNew = vecs[1] - pos;
        //RadianValue += Mathf.Acos(Vector3.Dot(vecOld.normalized, vecNew.normalized)) * Mathf.Rad2Deg;
        
        //vecOld = vecs[1] - pos;
        //vecNew = vecs[2] - pos;
        //RadianValue += Mathf.Acos(Vector3.Dot(vecOld.normalized, vecNew.normalized)) * Mathf.Rad2Deg;
        
        //vecOld = vecs[2] - pos;
        //vecNew = vecs[0] - pos;
        //RadianValue += Mathf.Acos(Vector3.Dot(vecOld.normalized, vecNew.normalized)) * Mathf.Rad2Deg;
    }
//Unity计算两点之间的距离或者向量的长度常用方法

Vector3 offset = A.position - B.position;
//单位化(得到长度为1的单位向量)
V = offset.normalized;
//得到距离
float distance = offset.magnitude;
/// <summary>
    /// 计算直线与平面的交点
    /// </summary>
    /// <param name="point">直线上某一点</param>
    /// <param name="direct">直线的方向</param>
    /// <param name="planeNormal">垂直于平面的的向量</param>
    /// <param name="planePoint">平面上的任意一点</param>
    /// <returns></returns>
    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;
    }

点到直线的垂足6666666

 /// <summary>
        /// 获得线段顶点数据
        /// </summary>
        /// <param name="go"></param>
        /// <param name="pointA"></param>
        /// <param name="pointB"></param>
        /// <returns></returns>
        private List<Vector3> GetVertices(GameObject go, Vector3 pointA, Vector3 pointB)
        {
            List<Vector3> points = new List<Vector3>();

            float HorDisABx = pointB.x - pointA.x;
            float HorDisABz = pointB.z - pointA.z;
            float HorDisAB = Mathf.Sqrt(Mathf.Pow(HorDisABx, 2) + Mathf.Pow(HorDisABz, 2));
            float offsetX = HorDisABz * lineWidth / HorDisAB;
            float offsetZ = HorDisABx * lineWidth / HorDisAB;

            Vector3 Point1 = new Vector3(pointA.x - offsetX, pointA.y, pointA.z + offsetZ);
            Vector3 Point2 = new Vector3(pointA.x + offsetX, pointA.y, pointA.z - offsetZ);
            Vector3 Point3 = new Vector3(pointB.x + offsetX, pointB.y, pointB.z - offsetZ);
            Vector3 Point4 = new Vector3(pointB.x - offsetX, pointB.y, pointB.z + offsetZ);
            points.Add(Point1);
            points.Add(Point2);
            points.Add(Point3);
            points.Add(Point4);

            VerticesData verticesData = new VerticesData();
            verticesData.go = go;
            verticesData.vA = Point1;
            verticesData.vB = Point2;
            verticesData.vC = Point3;
            verticesData.vD = Point4;
            verticesData.vStart = pointA;
            verticesData.vEnd = pointB;
            verticesPoints.Add(verticesData);

            return points;
        }
 /// <summary>
        /// 求点到直线的距离,采用数学公式Ax+By+C = 0; d = A*p.x + B * p.y + C / sqrt(A^2 + B ^ 2)
        /// 此算法忽略掉三维向量的Y轴,只在XZ平面进行计算,适用于一般3D游戏。
        /// </summary>
        /// <param name="startPoint">向量起点</param>
        /// <param name="endPoint">向量终点</param>
        /// <param name="point">待求距离的点</param>
        /// <returns></returns>
        private float DistanceOfPointToVector(Vector3 startPoint, Vector3 endPoint, Vector3 point)
        {
            Vector2 startVe2 = IgnoreYAxis(startPoint);
            Vector2 endVe2 = IgnoreYAxis(endPoint);
            float A = endVe2.y - startVe2.y;
            float B = startVe2.x - endVe2.x;
            float C = endVe2.x * startVe2.y - startVe2.x * endVe2.y;
            float denominator = Mathf.Sqrt(A * A + B * B);
            Vector2 pointVe2 = IgnoreYAxis(point);
            float p = (A * pointVe2.x + B * pointVe2.y + C) / denominator;
            return Mathf.Abs((A * pointVe2.x + B * pointVe2.y + C) / denominator);
        }
 /// <summary>
        /// 三维空间点到直线的垂足
        /// </summary>
        /// <param name="startPoint">开始点</param>
        /// <param name="endPoint">结束点</param>
        /// <param name="point">鼠标点击的点</param>
        /// <returns></returns>
        private Vector3 Point(Vector3 startPoint, Vector3 endPoint, Vector3 point)
        {
            Vector3 retVal;
            float dx = startPoint.x - endPoint.x;
            float dy = startPoint.y - endPoint.y;
            float dz = startPoint.z - endPoint.z;
            if (Math.Abs(dx) < 0.00000001 && Math.Abs(dy) < 0.00000001 && Math.Abs(dz) < 0.00000001)
            {
                retVal = startPoint;
                return retVal;
            }

            float u = (point.x - startPoint.x) * (startPoint.x - endPoint.x) + (point.y - startPoint.y) * (startPoint.y - endPoint.y) + (point.z - startPoint.z) * (startPoint.z - endPoint.z);
            u = u / ((dx * dx) + (dy * dy) + (dz * dz));
            retVal.x = startPoint.x + u * dx;
            retVal.y = startPoint.y + u * dy;
            retVal.z = startPoint.z + u * dz;

            return retVal;
        }

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

山竹炒大蒜

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值