Getting closest point on a plane

aditya sarma

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 also need the projection of the point on the terrain

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);
    }
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?

Greg Lukosek

I hope this will help:

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;
}

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

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related