Create a part based on 2 opposite corners?

My goal is to create this red transparent part with its corners lined up with the corners on the 2 gray parts here in the image below.

I want to have green lined up with green, red lined up with red.
My current code:

local part1, part2, p = script.Parent.White1, script.Parent.White2, Instance.new("Part", workspace)

local startPos = part1.Position + Vector3.new(part1.Size.Z/2, 0, -part1.Size.X/2)
local endPos = part2.Position + Vector3.new(-part2.Size.Z/2, 0, part2.Size.X/2)

local dist = (endPos - startPos).Magnitude
local mid = startPos:Lerp(endPos, .5)

p.CFrame = CFrame.new(mid, endPos)
p.Size = Vector3.new(5, .5, dist)

The red part is “p”
The green poles indicate the startPos and endPos


How can I shift it so the corners are actually lined up?

Is this what you want?

image

My bad, I mean this:

image

This is what I’m looking for, sorry.

Actually tried using your gapfill plugin but it only put one of the edges on the side i actually wanted


Do you want all the edges like this?


I’ll be back in a bit.

Not sure I understand what you’re asking

Oh, didn’t see those extra posts.

The reason you’re having trouble is that the problem is under-specified. You also have to specify the width of the part created for there to be a unique solution.

image

Then you can do this:

local part1, part2, p = script.Parent.White1, script.Parent.White2, Instance.new("Part", workspace)

local startPos = part1.CFrame:PointToWorldSpace(Vector3.new(part1.Size.X/2, 0, -part1.Size.Z/2))
local endPos = part2.CFrame:PointToWorldSpace(Vector3.new(-part2.Size.X/2, 0, part2.Size.Z/2))

-- First solve for the length using Pythagorean theorem
-- so we know the exact dimensions of the part we're working with.
local diagonal = (endPos - startPos).Magnitude
local width = 5
local length = math.sqrt(diagonal^2 - width^2)

local offsetThetaFromDiagonal = math.atan2(width / 2, length / 2)

local mid = startPos:Lerp(endPos, .5)

p.Anchored = true
p.CFrame = 
	CFrame.new(mid, endPos) * 
	CFrame.Angles(0, -offsetThetaFromDiagonal, 0)
p.Size = Vector3.new(width, .5, length)
5 Likes

Thank you very much!

Just had to make the offsetThetaFromDiagonal in the CFrame.Angles positive, and swap the negatives on the Z sizes in start and end Pos and I got exactly what I was looking for.
Assuming we had different numbers because of different part orientations.

In case someone in the future wants to see my end product for some reason
local part1, part2, p = script.Parent.White1, script.Parent.White2, Instance.new("Part", workspace)

local startPos = part1.CFrame:PointToWorldSpace(Vector3.new(part1.Size.X/2, 0, part1.Size.Z/2))
local endPos = part2.CFrame:PointToWorldSpace(Vector3.new(-part2.Size.X/2, 0, -part2.Size.Z/2))

local diagonal = (endPos - startPos).Magnitude
local width = 5
local length = math.sqrt(diagonal^2 - width^2)

local offsetThetaFromDiagonal = math.atan2(width / 2, length / 2)

local mid = startPos:Lerp(endPos, .5)

p.Anchored = true
p.CFrame =
    CFrame.new(mid, endPos) *
    CFrame.Angles(0, offsetThetaFromDiagonal, 0)
p.Size = Vector3.new(width, .5, length)

1 Like