Part Position "Snapping" - Vector Math

Hello everyone! Today I seek more knowledge about vector math.

  1. What do you want to achieve?
    In my game, players place parts of all shapes and sizes. When they place them, these parts need to “Snap” to the closest placed part (I can check this by myself). This is on an X, Y axis by the way. I don’t care about up and down. Snapping would be repositioning the part until the CLOSEST point is touching the other part. Please note: Just one point is necessary. This point can be on ANY side of the part.

I already have a list of parts to snap to. Looping through to find it isn’t a problem.

The solution uses part:GetTouching(), but I’m not sure where to go from there.

Here is an image to help:

This snaps to:

And more importantly:


Snaps to:

I hope this is understandable, and am open to any methods. However: I prefer the most efficient way.

I do not know enough vector math to do this, so I would like to learn!

Thank you,
Someperson576

2 Likes

you can use GetTouchingParts() and then get the closest part, which if you need me to explain i can. After that you can set the shapes CFrame to the Grid Parts CFrame that its touching. then to make the shape sit right on top of the Grid Part, Multiply The Shapes CFrame by the Y Axis Size then Multiply it by 0.5, or just do divided by 2, but you should multiply it. respond if you need me to explain better

Would this also work for the X axis? This is a very interesting solution, I’ll be investigating further and will respond back to here. Thank you!

what do you mean would it also work with the X axis?

Well, players can place parts in the upper left corner (respective to other parts) and I want them to essentially be move down in a straight, linear line towards to other part until they touch. Moving in both the X and Y axis.

im not really sure what you mean, the shape will always go in the middle of the Tile no matter what tile it is

This isn’t always on tiles. Like this:

Should snap to this:

As for your code, @bronsonman, I’m sorry to ask, but could you provide me with an example? I am confused as to how to go about using the methods you listed.

sure, add me on discord Bronson#9697

I’ve added you on discord. Someperson576#5463
(more than 30chars)

Alright, so after A LOT of digging (plus too many google searches), I finally solved this. I used:

local function ClosestPointOnPart(Part, Point)
	local Transform = Part.CFrame:pointToObjectSpace(Point) -- Transform into local space
	local HalfSize = Part.Size * 0.5
	return Part.CFrame * Vector3.new( -- Clamp & transform into world space
		math.clamp(Transform.x, -HalfSize.x, HalfSize.x),
		math.clamp(Transform.y, -HalfSize.y, HalfSize.y),
		math.clamp(Transform.z, -HalfSize.z, HalfSize.z)
	)
end

Which I found to calculate the two closest points. I then use an AlignPosition to bring them together: AlignPosition | Documentation - Roblox Creator Hub. Thank you bronsonman for attempting to solve this issue.

3 Likes