What is the most efficient approach to determine the precise shortest distance between 2 peculiar MeshParts, regardless of their orientation?

I need to get the closest exact distance between these two peculiar mesh parts.

The only potential idea I have come up with is shooting a ray out of the mesh in every direction (like a ball), preferably every 22.5 degrees.

Example:

A potential problem here would be the rays shooting out from the center attachment. As shown in the picture, the closest two points would likely be from the lip of the sink to the edge of the anvil. However, because the rays are coming out of the center, I wouldn’t get the exact closest distance.

Is there an easier or more reliable method anyone can suggest for solving this problem?

Thanks for any help or advice in advance!

1 Like

Using magnitude, then verifying the magnitude by firing a raycast that travels towards the target object (must can only travel in a certain amount of studs).

Pseudo Code:

-- this code works for any type of object such as a part or a union.
local center = workspace.CenterMesh
local target = workspace.TargetMesh

local calculation = (center.Position - target.Position)

local mag = calculation.Magnitude

local studamount = calculation/2

local rayResult = workspace:Raycast(center.Position, studamount)

if mag > 0 then
      if rayResult then
             print("detected hit, cannot use mag")
      else
             print(tostring(math.round(mag))) -- you could always just remove 'tostring' so it looks nicer.
      end
end
1 Like