Determine a part's distance from a region of parts

I want to determine a part’s distance from a region of parts. This is happening on a 2d plane btw. Look at the example below :point_down: if you don’t know what I mean by region.

I DONT WANT:
The part’s distance from the center of the region
The part’s closest vector distance

All help is welcome, thank you

1 Like

part.CFrame - part2.CFrame. make sure to define both parts

Part.Position. Cframes can’t perform arithmetic.

You can do: local distance = (part.CFrame.Position - part2.CFrame.Position).Magnitude

yeah but didnt he want a vector

cant You do: local distance = (part.Position - part2.Position)

.Magnitude doesn’t return a vector it returns the length of a vector which in this case will be the distance between the positions.

ik but he wanted a vector for the distance

and isn’t it on a 2d plane? Vector2 is for 2d

Since this a 2D plane I don’t know if you could use workspace:RayCast() but if you can then fire a ray cast from the parts position to the center of the region and find the ray cast hit position and get the magnitude of the difference of those. This is assuming that the region is collidable and

lowk you could do sqrt(abs(part.Position.X - part2.Position.X)^2 + abs(part.Position.Y - part.Position.Y)^2 ) if it’s a 2d plane. if it is topdown 2d just use Z instead of Y

You only get vectors there is no parts, the region is defined by a dictionary of vectors

is it like a matrices array of vectors

yes, it is a matrices array of vectors

  • I see the confusion here but I want the distance from the region (meaning the minimal distance from the region) please see the figure. Btw rn what I have done is selected the closest vector to my point, then selected the left and right points and formed triangles respectively so a triangle would be formed with the original point, selected point, and left point. Then I would measure the base angles of these triangles if they are both less than 90 degrees (both individually not Together) then the original point is inbetween the 2 points meaning we can either solve for the height of said triangle. But if both triangles formed (with right and left points) have base angles less than 90 degrees then we select the triangle with the least distance. Now this solution has a couple problems, once you get really close to region Roblox doesn’t get a super specific side lines which breaks solving for the height of the triangle. And also this is very inefficient having to solve for angles, sides, and magnitude multiple times. This is why I was curious how others would do it.