获取飞机上的最近点 [英] Getting closest point on a plane

查看:79
本文介绍了获取飞机上的最近点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我有这个地形,并设置了垂直平面,现在我想结合这两种含义将平面的顶点移动到最接近的顶点.地形上的顶点. 我画了一个图来说明我的想法. 最终结果是地形看起来好像有些厚度.

So I have this terrain and with a perpendicular plane setup, now I want to combine those two meaning to move the vertices of the plane to their closest resp. vertices on the terrain. I made a drawing to illustrate my thoughts. Final result being the terrain will look like it has some thickness.

我还需要该点在地形上的投影

我在上一个线程中找到了此代码:

I found this code on a previous thread :

    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,可提供地形的所有表面法线.我应该在这里使用哪一个?

the result vector here is the closest point(i think!) But what is the plane normal? Unity has a built in mesh.normals which gives all the surface normals for the terrain. Which one should I use here?

推荐答案

我希望这会有所帮助:

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

For more useful 3D Math look here: https://github.com/GregLukosek/3DMath

这篇关于获取飞机上的最近点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆