• QQ
  • nahooten@sina.com
  • 常州市九洲新世界花苑15-2

游戏开发

U3D立体几何-点到直线的距离计算

原创内容,转载请注明原文网址:http://homeqin.cn/a/wenzhangboke/jishutiandi/youxikaifa/2018/1024/120.html

Unity立体几何-点到直线的距离计算

 

Unity引擎在开发一款3D游戏时,经常要用到一些3D运算,比如两点之间的距离,向量的一些操作等。

 

Unity我们常州游戏设计培训专家幻天网络已经封装好了一些常用函数在Vector3Plane类中,如两点之间的距离,向量之间的夹角,向量的投影计算等。

 

这里就给大家介绍下点到直线的距离计算,根据投影及勾股定理计算。

    /// <summary>

    /// 点到直线距离

    /// </summary>

    /// <param name="point">点坐标</param>

    /// <param name="linePoint1">直线上一个点的坐标</param>

    /// <param name="linePoint2">直线上另一个点的坐标</param>

    /// <returns></returns>

    public static float DisPoint2Line(Vector3 point,Vector3 linePoint1,Vector3 linePoint2)

   

        Vector3 vec1 = point - linePoint1;

        Vector3 vec2 = linePoint2 - linePoint1;

        Vector3 vecProj = Vector3.Project(vec1, vec2);

        float dis =  Mathf.Sqrt(Mathf.Pow(Vector3.Magnitude(vec1), 2) - Mathf.Pow(Vector3.Magnitude(vecProj), 2));

        return dis;

   

下面提供了点到面的距离及面夹角的计算,不过是对一些函数调用的二次封装,仅供参考

using UnityEngine;

/// <summary>

/// http://www.homeqin.cn/suifcd

/// </summary>

public class GeometryTools

    /// <summary>

    /// 点到直线距离

    /// </summary>

    /// <param name="point">点坐标</param>

    /// <param name="linePoint1">直线上一个点的坐标</param>

    /// <param name="linePoint2">直线上另一个点的坐标</param>

    /// <returns></returns>

    public static float DisPoint2Line(Vector3 point,Vector3 linePoint1,Vector3 linePoint2)

   

        Vector3 vec1 = point - linePoint1;

        Vector3 vec2 = linePoint2 - linePoint1;

        Vector3 vecProj = Vector3.Project(vec1, vec2);

        float dis =  Mathf.Sqrt(Mathf.Pow(Vector3.Magnitude(vec1), 2) - Mathf.Pow(Vector3.Magnitude(vecProj), 2));

        return dis;

   

    /// <summary>

    /// 点到平面的距离 自行推演函数

    /// </summary>

    /// <param name="point"></param>

    /// <param name="surfacePoint1"></param>

    /// <param name="surfacePoint2"></param>

    /// <param name="surfacePoint3"></param>

    /// <returns></returns>

    public static float DisPoint2Surface(Vector3 point,Vector3 surfacePoint1,Vector3 surfacePoint2,Vector3 surfacePoint3)

   

        //空间直线一般式方程 Ax + By + Cz + D = 0;

        //假定 A = 1 ,推演B C DA来表示,约去A,可得方程

        float BNumerator =(surfacePoint1.x - surfacePoint2.x)*(surfacePoint2.z - surfacePoint3.z) - (surfacePoint2.x - surfacePoint3.x)*(surfacePoint1.z - surfacePoint2.z);

        float BDenominator = (surfacePoint2.y - surfacePoint3.y) * (surfacePoint1.z - surfacePoint2.z) - (surfacePoint1.y - surfacePoint2.y) * (surfacePoint2.z - surfacePoint3.z);

        float B = BNumerator/BDenominator;

        float C = (B * (surfacePoint1.y - surfacePoint2.y) + (surfacePoint1.x - surfacePoint2.x)) / (surfacePoint2.z - surfacePoint1.z);

        float D = -surfacePoint1.x - B * surfacePoint1.y - C * surfacePoint1.z;

        return DisPoint2Surface(point,1f, B, C, D);

   

    public static float DisPoint2Surface(Vector3 point, float FactorA,float FactorB,float FactorC,float FactorD)

   

        //点到平面的距离公式 d = |Ax + By + Cz + D|/sqrt(A2 + B2 + C2);

        float numerator = Mathf.Abs(FactorA * point.x + FactorB * point.y + FactorC * point.z + FactorD);

        float denominator = Mathf.Sqrt(Mathf.Pow(FactorA,2) + Mathf.Pow(FactorB,2) + Mathf.Pow(FactorC,2));

        float dis = numerator / denominator;

        return dis;

   

    /// <summary>

    /// 点到平面距离 调用U3D Plane类处理

    /// </summary>

    /// <param name="point"></param>

    /// <param name="surfacePoint1"></param>

    /// <param name="surfacePoint2"></param>

    /// <param name="surfacePoint3"></param>

    /// <returns></returns>

    public static float DisPoint2Surface2(Vector3 point, Vector3 surfacePoint1, Vector3 surfacePoint2, Vector3 surfacePoint3)

   

        Plane plane = new Plane(surfacePoint1, surfacePoint2, surfacePoint3);

        return DisPoint2Surface2(point,plane);

   

    public static float DisPoint2Surface2(Vector3 point, Plane plane)

   

        return plane.GetDistanceToPoint(point);

   

    /// <summary>

    /// 平面夹角

    /// </summary>

    /// <param name="surface1Point1"></param>

    /// <param name="surface1Point2"></param>

    /// <param name="surface1Point3"></param>

    /// <param name="surface2Point1"></param>

    /// <param name="surface2Point2"></param>

    /// <param name="surface2Point3"></param>

    /// <returns></returns>

    public static float SurfaceAngle(Vector3 surface1Point1, Vector3 surface1Point2, Vector3 surface1Point3, Vector3 surface2Point1, Vector3 surface2Point2, Vector3 surface2Point3)

   

        Plane plane1 = new Plane(surface1Point1, surface1Point1, surface1Point1);

        Plane plane2 = new Plane(surface2Point1, surface2Point1, surface2Point1);

        return SurfaceAngle(plane1, plane2);

   

    public static float SurfaceAngle(Plane plane1,Plane plane2)

   

        return Vector3.Angle(plane1.normal, plane2.normal);

   



上篇:上一篇:unity wav 格式音频写入本地
下篇:下一篇:Unity知识总结(一):物体移动方法