My goal is simple. Calculate the shortest rotational direction between 2 CFrames. I feel like I’m close to the answer but it seems slightly off. Below is my set up:
I want the blue brick to align rotationally with the grey one. I used a basic delta equation to find the difference in rotation between the final and initial rotation:
local p1 = workspace.Part1
local p2 = workspace.Part2
local cf1 = p1.CFrame - p1.CFrame.Position
local cf2 = p2.CFrame - p2.CFrame.Position
local dir = cf2 * cf1:Inverse() -- delta = final - inital
p1.CFrame = p1.CFrame * dir
And this code works. No matter how the blue brick is aligned, running this will align it perfectly with the grey one instantaneously.
The problem comes when I try to find a way to turn this dir
variable into torque. This code makes the blue brick rotate initially in the correct direction but it misses the target somehow.
RunService.Stepped:Connect(function()
local cf1 = p1.CFrame - p1.CFrame.Position
local cf2 = p2.CFrame - p2.CFrame.Position
local dir = cf2 * cf1:Inverse()
local x, y, z = dir:ToOrientation()
p1.Torque.Torque = Vector3.new(x, y, z) * 10
end)
This is the result:
I’m not very good with rotational math, so any help is appreciated!