How to point a CFrame towards a Vector, by rotating another CFrame?

If I have 2 CFrames, A, B and a Vector “Target”, If B is relative to A, how would I rotate A on a fixed point to get B to look at “Target”?

1 Like

Use LookVectors

This post goes into good detail Explaining it: What is lookVector and how do I use it? [closed] - Scripting Helpers

LookVectors are not the problem, I’m trying to rotate CFrame A, so the attached CFrame B points at the Vector “Target”, Telling me to use “LookVector” does not solve the problem, what do I do with LookVectors? How would they solve my issue?

Thanks for trying to help but this doesn’t answer my question.

Use CFrame.lookAt. It takes 2 positions as an argument, the first one being the eye while the second being the target. It returns a CFrame whose rotational matrix is changed in a way that it looks towards the target, essentially returning a “rotated” CFrame.

Quick example on how you may use it in your case:

local cf1 = CFrame.new(0, 3, 0)
local cf2 = CFrame.new(1, 5, 1)

-- Return a CFrame in which, cf1 looks at cf2
local rotatedCF1 = CFrame.lookAt(cf1.Position, cf2.Position)

Sorry if my point wasn’t clear, I’ll try visualizing it in 3D instead.
How do I rotate CFrame A (Blue line), so that the welded CFrame B (Purple line) is looking at target?

image

I have tried making CFrame A look at the Target, substracting the rotation difference between CFrame B and CFrame A, then adding that to the CFrame A but it doesn’t return correct results.

--From the weld formula
part1.CFrame * C1 == Part0.CFrame * C0
--inverse both sides by c1 on the right hand side, order matters
part1.CFrame  == Part0.CFrame * C0* C1:Inverse()
--combine
part1.CFrame  == Part0.CFrame * SomeCFrameOffset
--solve for the initial constant offset between the welded parts of CFrame A and B
Part0.CFrame:Inverse()*part1.CFrame  ==  SomeCFrameOffset

--back to this weld formula
part1.CFrame  == Part0.CFrame * SomeCFrameOffset

--we want part1 to be looking at the target from it's current position so we replace it with CFrame .lookAt
CFrame.lookAt(somegoal)  == Part0.CFrame * SomeCFrameOffset
--Solve for Part0
CFrame.lookAt(Part1.Position,targetPosition)*SomeCFrameOffset:Inverse()  == Part0.CFrame 

Apply formula with setup. In my case Part0 CFrame A, in my case its wedge, and Part1 is wedge2.

Practice place file with the welded parts

MoreCFrameOffsetPractice.rbxl (31.2 KB)

Final code
local wedge = script.Parent
local wedge2 = workspace.Wedge2
local target = workspace.Target

local rotationOffset = wedge.CFrame:Inverse()*wedge2.CFrame

while true do
	wait()	
	local lookCFrame = CFrame.lookAt(wedge2.Position,target.Position)
	local goalRotation= lookCFrame*rotationOffset:Inverse()
	goalRotation = goalRotation-goalRotation.Position
	wedge.CFrame = CFrame.new(wedge.Position)*goalRotation
end
2 Likes