# Nearest position on the surface of a collection of parts

Given arbitrary position a, and collection of parts cP, find the position b which denotes, or best approximates the nearest surface projection on the collective surface formed of the parts constituted by the constituent baseParts (of cP), where a, cP are provided and b must be found.

Example use case: detecting how far a character is from the edge of a volume.
cP’s surface can be thought of as the idealized surface a Union instance of cP’s baseParts.

Conceptual advice, recommended topics, or psuedocode implementation appreciated, thanks.

I’m sure many of us can help you, but there’s really no need to use math-lingo. It makes it harder for us to actually understand the practical solution you want out of this. There’s a reason only math textbooks speak like that.

Can you give a visual example or explain in more layman terms what you’re wanting? My minor is in math and I still have 0 idea what you’re trying to say.

1 Like

Just as @MrNicNac said try to be more clear in what your saying.

cP is a collection of baseParts.
a and b are vector3’s.

We want to determine b, the nearest position on the surface of cP to a, which is the position the player’s torso might be.

cP can be thought as the Union of its baseParts regarding its surface. It is not actually a Union, as Union hitboxes are undesirable approximations:

Personally I would start stripping the RotatedRegion3 Module by @EgoMoose for its maths to achieve this. Iterating through each part we are first detected to be in, and then calculating each surface distance/ position. Though this is kind of guess work. It might be more wise to pursue it directly looking for something on stackexchange like this:

If you do physics engines, this is probably just a matter of reducing the system to what I need. Thing is, I don’t do physics engines.

Hope that has more clarity.

Using rectangular parts.

You can find the closest surface to an inner piece.

The red are possible, valid points and the yellow is the chosen point closest to the surface of the intersecting part the blue point is in.

Non-intersecting surfaces

Complex multi-intersection handling.

Place file attached for your review. Uses multiple modules. Find it in ServerScriptStorage. The main script does the logic.

closest-surface-to-point.rbxl (25.0 KB)

This assumes straight lines directly to the closest surface from the point. Arbitrary directions are not considered.

EDIT: If you want to consider many possible points on a surface then that is simple as well. You simply need to “pixelate” the surface into a specific resolution instead of doing what I did.

Then filter out surfaces which are colliding with other surfaces. Finally, judge the distance to all possible points from the original.

My code is capable of doing this. Right now I only pick + shaped directional points from to surfaces of intersecting parts. Let me know if you need assistance pixelating the surfaces for considerations.

On second thought, this might be more of what you’re looking for.

closest-surface-to-point-v2.rbxl (25.9 KB)

The resolution can be changed with performance sacrifice or gain.

3 Likes