instructions

In the last video, we had the distance from the point to the plane, and to figure out the coordinates of the projection point, we could use the distance that we had.

The geometric

Following the previous procedure, we have the length of the projection of the vector AC onto the normal line of the plane, which is the length of the vector AD. So to figure out what the projection point is, we just move C a certain distance in some direction, which is the distance from C to the plane, in the direction of the plane normal AB.

code

static func projectionOnPlane(from point:simd_float3, to plane:Plane) -> simd_float3 {
// We also need to use vector changes, so we will rewrite the code in 'projonplane (from point:simd_float3, to plane: plane)' directly
    let vector = point - plane.position
    let normalizedNormal = normalize(plane.normal)
    
    let dotValue = dot(vector, normalizedNormal)
    let tarPoint = point - dotValue * normalizedNormal
    return tarPoint
}
Copy the code

I’m going to use the directionality of the distance from the point to the plane, that is, whether the positive or negative sign indicates whether the point is in the positive or negative direction of the normal. Note here: when the point is behind the plane, it should move along the normal line; When a point is in the positive direction of the plane, it should move in the opposite direction of the normal.