Most efficient way to get the distance from a shape's edge?

What’s the most efficient way to get the distance from a shape’s edge at any closest point?

For example, pretend that perfectly drawn red line represents 4 studs away from the part. If a player is on any point of that line, return 4.

The first idea that springs to mind is to store the part’s corners within a table then recursively check the magnitude between them and the object. Is there a built in function to handle this for us?


I think this is what you mean? As long as you can figure out how to move the points out four studs from the origin, it should work(correct me if im wrong).

local function PointInPolygon(Point, Polygon)
	local current = Polygon
	local inside = false
	local A = current.Position
	local ax, az = A.X, A.Z
	local px, pz = Point.X, Point.Z
		local B = current.Next.Position
		local bx, bz = B.X, B.Z
		if ((az >= pz) ~= (bz >= pz)) and ((px - ax) <= (bx - ax)*(pz - az)/(bz - az)) then
			inside = not inside
		current = current.Next
		A, ax, az = B, bx, bz
	until current == Polygon
	return inside

local function CreatePolygonFromPoints(Points)
	local Polygon = {Position = Points[1]}
	local First = Polygon
	for i = 2, #Points do
		Polygon.Next = {Previous = Polygon, Position = Points[i]}
		Polygon = Polygon.Next
	Polygon.Next, First.Previous = First, Polygon
	return First

I’m more-so looking for the most efficient method to get the distance from the closest point on a part’s edge, as apposed to checking if something is within it’s area. I’ve edited the OP to make this more clear.

If I understand your issue properly (to get parts on or in the red line boundary), while this isn’t the “most efficient way to do it,” I’d create two Region3s, one that’s the “square version” of the part, being the size of the part, plus 4 studs all around. Then, I’d create the second Region3, which is essentially the “ignore” part of the region (for the bottom right corner).

Use any variant of workspace:FindPartsInRegion3 to get parts that are within both Regions, and if they match up in both tables, cancel them out. If parts that are in the first Region3 aren’t in the second, then you’ve got a part within the 4-stud boundary surrounding your part.

To get the distance from a point to some (block-shaped) part, you can find the closest point on that part to the point, and then calculate the distance between those points.

As for finding the closest point on the part to the point, I wrote some code in reply to a similar post which does this. I have also provided an explanation of how it works:

From there, you just need to take that point and calculate the distance to your original point.

Note that this will not work for non block-shaped parts (spheres, wedges, etc.) - you will have to come up with alternative methods to find the closest point on those parts. This method will not be applicable to unions or meshparts.