Connecting two parts in the some point?

Let’s say I have 2 different parts. How can I find the same point between them and connect them with another part?
Now I made it so that the two parts can be located anywhere, but are guaranteed to have the same point. The starting point of each part is the upper left corner.

I chose random coordinates for illustrative examples (I hope this did not confuse you even more :confused:)

Not 100% sure what you mean, but this might help you?

1 Like

I’m also not sure what you mean by the “same point between them”. Are you trying to line up two parts based on registration points on their surfaces, or are you trying to find the shortest connector that can connect two parts, or something else entirely?

If you have two arbitrary points in 3D space, the point halfway in between is just the average, (p1+p2)/2

You can orient a part along this line segment by constructing a CFrame where one of the basis axes is (p2-p1).Unit There are infinitely many ways to rotate the part about this axis, so you can pick any vector not colinear with (p2-p1), cross product it with (p2-p1) to get a second component for your CFrame, then cross product that with (p2-p1) to get the third. If the part is like a cylinder, it won’t even matter, any valid orientation will do. If the part is something like a ramp for people to walk on, it’s logical to cross (p2-p1) with the world Y axis or any vector in the X-Z plane that is not (p2-p1), so that your part is aligned with world “up” sensibly. Once you have 3 orthonormal vectors, you can use CFrame.fromMatrix(). Actually, I think you can call this with just 2 vectors and it will do the final cross product for you.

You can also get a sensible orientation from CFrame.new(p1,p2) as long as the connection axis (p2-p1) isn’t close to pointing directly up or down the world Y axis. The explicit fromMatrix method does not have this pitfall, and is preferred.

I want to create path like in a dungeon. I have two rooms and I need to connect them with a line between two points

You can use the above method, and use Attachments on your dungeon models if you don’t already have door parts that conveniently give you the two points you need. Attachment.WorldPosition will.

Here, I’ve just made you a quick example of what I describe above. Use the green “Play” in studio to run it. Don’t play solo or you’ll spawn, fall, and be sad.

PartOrientationExample.rbxl (14.8 KB)

The code in this is:

local p1 = game.Workspace.Terrain.Attachment1.WorldPosition
local p2 = game.Workspace.Terrain.Attachment2.WorldPosition
local part = game.Workspace.Part
part.CFrame = CFrame.fromMatrix((p1+p2)/2,(p2-p1).Unit,Vector3.new(0,1,0),- 
Vector3.new(0,1,0):Cross((p2-p1).Unit))
part.Size = Vector3.new((p2-p1).Magnitude,1,4)

You can re-order the 2nd, 3rd, and 4th arguments to fromMatrix however you need to get your part oriented correctly to how it’s made, to ensure proper woodgrain where applicable. This is one benefit over the CFrame.new(v1,v2) method! Notice that the Z-direction vector is negated, due to Roblox coordinate system.

6 Likes