unity3d - 获取飞机上的最近点

标签 unity3d

所以我有这个地形和垂直平面设置,现在我想结合这两个意思将平面的顶点移动到它们最近的 resp。地形上的顶点。 我画了一张图来说明我的 thoughts . 最终结果是地形看起来有一些厚度。

我还需要 projection of the point on the terrain

我在之前的线程中找到了这段代码:

    float distance = PointToPlaneDistance(smallObj.transform.position, wall.transform.position, wallNormal);

    private float PointToPlaneDistance(Vector3 pointPosition, Vector3 planePosition, Vector3 planeNormal)
    {
        float sb, sn, sd;

        sn = -Vector3.Dot(planeNormal, (pointPosition - planePosition));
        sd = Vector3.Dot(planeNormal, planeNormal);
        sb = sn / sd;

        Vector3 result = pointPosition + sb * planeNormal;
        return Vector3.Distance(pointPosition, result);
    }
这里的结果向量是最近点(我认为!) 但是飞机正常吗? Unity 有一个内置的 mesh.normals,它给出了地形的所有表面法线。我应该在这里使用哪一个?

最佳答案

我希望这会有所帮助:

public static Vector3 ProjectPointOnPlane(Vector3 planeNormal, Vector3 planePoint, Vector3 point){

    float distance;
    Vector3 translationVector;

    //First calculate the distance from the point to the plane:
    distance = SignedDistancePlanePoint(planeNormal, planePoint, point);

    //Reverse the sign of the distance
    distance *= -1;

    //Get a translation vector
    translationVector = SetVectorLength(planeNormal, distance);

    //Translate the point to form a projection
    return point + translationVector;
}



//Get the shortest distance between a point and a plane. The output is signed so it holds information
//as to which side of the plane normal the point is.
public static float SignedDistancePlanePoint(Vector3 planeNormal, Vector3 planePoint, Vector3 point){

    return Vector3.Dot(planeNormal, (point - planePoint));
}



//create a vector of direction "vector" with length "size"
public static Vector3 SetVectorLength(Vector3 vector, float size){

    //normalize the vector
    Vector3 vectorNormalized = Vector3.Normalize(vector);

    //scale the vector
    return vectorNormalized *= size;
}

更多有用的 3D 数学请看这里: https://github.com/GregLukosek/3DMath

关于unity3d - 获取飞机上的最近点,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28653628/

相关文章:

c# - Unity 4.6 编辑器,使用预定义数据创建脚本

c# - 在 Unity 中使用资源文件夹

c# - 奇怪的 DllImport 行为(仅在 Unity3d 中)

c# - Unity 增加一个值然后减少它(DayNightCycle)

c++ - 全屏应用

c# - 确定在 Unity C# 中顺时针还是逆时针旋转更快

c# - 如何按位置获取瓷砖游戏对象?

javascript - JInt 需要另一个 js 文件

c# - 如何使用 float.Parse 从 "5/2"等字符串中获取小数

c# - unity3d如何实现多重音频?