0

# How can I calculate the middle of two CFrames??

There's CFrame 1 and CFrame 2 and you want to find the exact center of these two CFrames to place a part there. But the catch is these two CFrames are randomized so you have to use a script to calculate the middle each time you wanna put a part.

Any Help would be appreciated

1
Finding the mean between two numbers is the same with CFrame. Add both the CFrame's together, (X+X, Y+Y, etc). Then divide the sum by 2, and that should be the centre between the two. KingDomas 148 — 17d

1
Edited 17 days ago

Hello, I can help you, but only if you dont mind the rotation, because I never tested that and I am not sure how it would work. So, if I understand, there are 2 CFrames and you want something like middle between of them. I draw the situation on the paper, and I would calculate it like that:

-- first we will eliminate unneeded information - rotation, which could only consume resources
local pos1 = CFrame1.Position
local pos2 = CFrame2.Position

-- now lets do the calculation part, we will begin with Y axe --

local diffY = math.abs(pos1.Y - pos2.Y) -- get the difference between Y levels
local targetY

if pos1.Y > pos2.Y then
targetY = pos2.Y + (diffY / 2)
else
targetY = pos1.Y + (diffY / 2)
end

local diffX = math.abs(pos1.X - pos2.X)
local targetX

if pos1.X > pos2.X then
targetX = pos2.X + (diffX / 2)
else
targetX = pos1.X + (diffX / 2)
end

local diffZ = math.abs(pos1.Z - pos2.Z)
local targetZ

if pos1.Z > pos2.Z then
targetZ = pos2.Z + (diffZ / 2)
else
targetZ = pos1.Z + (diffZ / 2)
end

local FINALPOSITION = Vector3.new(targetX, targetY, targetZ)


I tried to make it as easy as I could, I hope it will help! :D. Have a nice day. Another probably easier way:

local pos1 = CFrame1.Position
local pos2 = CFrame2.Position

local FINALPOSITION = Vector3.new((pos1.X + pos2.X)/2, (pos1.Y + pos2.Y)/2, (pos1.Z + pos2.Z)/2)