Smallest perpendicular distance between 2 breps

Hi everyone, I’m quite new to grasshopper so sorry for asking how to do something on a troubleshooting forum but I honestly can’t find a decent tutorial for this. I have a terrain surface and a load of buildings that are on a planar surface above. I want to move these buildings down onto the terrain so that as soon as a building hits the terrain (moving in the Z axis) it stops. I know that to do this I need to find a list of smallest perpendicular distances between the main brep (terrain) and each smaller brep (buildings) so that I can use this as a translation vector. I’m really stumped on how to find this data but I feel like there must be a way to do it. Any help, really appreciated

A similar goal was implemented here using IVRay (IsoVist Ray) to find the vertical distance between the area centroid of each building and the terrain mesh.

That’s not quite what you are asking for though because parts of the buildings may go below the terrain. To avoid that, you would have to project a 2D outline of each buildings perimeter unto the mesh, then find the highest discontinuity point of the projected curves.

I made the changes I described above in the green group (below), which no longer uses IVRay:

In this case, the “terrain” was already a brep, not a mesh. Project works only with breps, not meshes, so if your terrain is a mesh you’ll have to convert it to a brep (polysurface), like this:

Wow thats some next level stuff right there. I’ll give this a try tonight, thank you for being so informative