Getting the two closest positions between two parts

It’s a little hard to explain but basically I want to get these two positions between two parts

I’ve almost got a working code but it doesn’t work at an angle
image
As you can see the small parts are still inside the big parts because of the way it works.
Here’s what I currently have and I’m unsure how I would fix this.

part0 = workspace.r
part1 = workspace.o
local Part0ClosestToPart1 = CFrame.new(part0.Position+CFrame.new(part0.Position,part1.Position).LookVector*part0.Size/2)
local Part1ClosestToPart0 = CFrame.new(part1.Position+CFrame.new(part1.Position,part0.Position).LookVector*part1.Size/2)
local p0 = Instance.new("Part",workspace)
local p1 = Instance.new("Part",workspace)
p1.CFrame = Part0ClosestToPart1
p0.CFrame = Part1ClosestToPart0
p1.Size = Vector3.new(.5,.5,.5)
p0.Size = Vector3.new(.5,.5,.5)
p1.Name = "p1"
p0.Name = "p0"

I’m guessing the issue is .LookVector*part0.Size/2 and .LookVector*part1.Size/2

I’d just use raycast for this.

local partA = workspace.A
local partB = workspace.B

local posA = partA.Position
local posB = partB.Position
local dir = posB - posA

local rayParam = RaycastParams.new()
rayParam.FilterDescendantsInstances = {partA, partB}
rayParam.FilterType = Enum.RaycastFilterType.Whitelist -- Raycast is only limited to the 2 parts

local ray1 = workspace:Raycast(posB,-dir,rayParam) -- Raycasts from partB to partA
local ray2 = workspace:Raycast(posA,dir,rayParam) -- Raycasts from partA to partB


local p1 = Instance.new('Part',workspace)
p1.Position = ray1.Position
p1.Size = Vector3.one/2
p1.Color = Color3.new(0,1,0)

local p2 = Instance.new('Part',workspace)
p2.Position = ray2.Position
p2.Size = Vector3.one/2
p2.Color = Color3.new(1,0,0)
1 Like

Using a raycast wouldn’t work for me since they wouldn’t aim for the closest position but rather the middle
image
the line is the raycast and the small parts is the method im trying to make work. As you can see the way I’m doing it would get the position closest to the other part which is what I need

example of what happens at an angle:

Nah raycasts would work, he just did the whitelisting part incorrectly, raycasting would make this system much simpler and compatible with various shapes.

Getting the magnitude between ray.Position and ray.Origin wouldn’t return the value I’d like

If you want to use your method, you can maybe divide the unit direction by the maximum number of its axis, in other words make one of the axis of unit direction be 1.

local partA = workspace.A
local partB = workspace.B

local posA = partA.Position
local posB = partB.Position
local dir = posB - posA

local sizeA = partA.Size
local sizeB = partB.Size

local unitDir = dir.Unit
local dirMax = math.max(math.abs(unitDir.X),math.abs(unitDir.Y),math.abs(unitDir.Z))
unitDir /= dirMax

local pos1 = posA + unitDir*sizeA/2
local pos2 = posB - unitDir*sizeB/2

local p1 = Instance.new('Part',workspace)
p1.Position = pos1
p1.Size = Vector3.one/2
p1.Color = Color3.new(0,1,0)

local p2 = Instance.new('Part',workspace)
p2.Position = pos2
p2.Size = Vector3.one/2
p2.Color = Color3.new(1,0,0)

I used your method except i directly took the “LookVector” of the direction

Although this only fixes the small part being inside the big part. The positions still aren’t the closest between two parts.

I think it’ll work good enough for what I’m doing

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.