How it the value of C1 computed in this FPS tutorial by EgoMoose? What's the thinking behind it?

Hi, in this tutorial by EggMoose, he uses the value of C1 to translate the gun so it aligns
with the camera.

if you scroll through, to the declaration of the function below

local function aimDownSights(aiming)
	local start = joint.C1;
	local goal = aiming and joint.C0 * offset or CFrame.new();
	
	aimCount = aimCount + 1;
	local current = aimCount;
	for t = 0, 101, 10 do
		if (current ~= aimCount) then break; end
		game:GetService("RunService").RenderStepped:Wait();
		joint.C1 = start:Lerp(goal, t/100);
	end
end

What I don’t understand; how is the goal CFrame for C1 computed; what’s the thinking behind it? I’ve been analyzing the code for about a week or so now, and still don’t understand.

My main focus has been the part where he says:

joint.Part0.CFrame * joint.C0 == joint.Part1.CFrame * joint.C1
joint.C1 = joint.Part1.CFrame:inverse() * joint.Part0.CFrame * joint.C0
– recall though that joint.Part0.CFrame == camera.CFrame thus:
joint.C1 = joint.C0;

-How is joint.C1 = joint.C0 as indicated in the above quotation?

Thank you.

3 Likes

This seems to be just an extension of the math he’s already doing right

joint.Part0.CFrame * joint.C0 == joint.Part1.CFrame * joint.C1

This is a statement that is made, thus the next line is an operation.

joint.C1 = joint.Part1.CFrame:inverse() * joint.Part0.CFrame * joint.C0

This is essentially algebraic reorganization notice how this is the same as the previous line except that both sides are being multiplied by the inverse of joint.Part1.CFrame. Because CFrames are matricies joint.Part1.CFrame cancels out with joint.Part1.CFrame:Inverse() to give the identity matrix. Simply, this would be n * (1/n) = 1 if we were not dealing with matrices.

– recall though that joint.Part0.CFrame == camera.CFrame thus:

Also recall from what he mentioned previously:

and we’re setting joint.Part1.CFrame = camera.CFrame

Assuming that both statements are true from what he said you can do a substitution, for joint.Part1.CFrame:inverse() as camera.CFrame:inverse() and joint.Part0.CFrame as camera.CFrame

joint.C1 = camera.CFrame:inverse() * camera.CFrame * joint.C0

and now what we know from my previous statements, these two should cancel and allow us to say:

joint.C1 = joint.C0

This is all assuming that both statements about joint.Part0.CFrame is camera.CFrame and that joint.Part1.CFrame is set to camera.CFrame. If that fact is not true the substitution as I’m reading it does not work and these terms do not cancel.

Hope that helps, if I royally misunderstood feel free to let me know.

3 Likes

This is correct! Thanks for the write up :+1:

5 Likes