## Posted By

Xtreme101 on 08/21/13

## Who likes this?

1 person have marked this snippet as a favorite

# Matrix Interpolation

/ Published in: Lua

For Notunknown99

`function lerp(a, b, t) -- Linear interpolation	return a + (b - a)*tend function slerp(a, b, t) --Spherical interpolation	dot = a:Dot(b)	if dot > 0.99999 or dot < -0.99999 then		return t <= 0.5 and a or b	else		r = math.acos(dot)		return (a*math.sin((1 - t)*r) + b*math.sin(t*r)) / math.sin(r)	endend function matrixInterpolate(a, b, t)	local ax, ay, az, a00, a01, a02, a10, a11, a12, a20, a21, a22 = a:components()	local bx, by, bz, b00, b01, b02, b10, b11, b12, b20, b21, b22 = b:components()	local v0 = lerp(Vector3.new(ax, ay, az), Vector3.new(bx , by , bz), t) -- Position	local v1 = slerp(Vector3.new(a00, a01, a02), Vector3.new(b00, b01, b02), t) -- Vector right	local v2 = slerp(Vector3.new(a10, a11, a12), Vector3.new(b10, b11, b12), t) -- Vector up	local v3 = slerp(Vector3.new(a20, a21, a22), Vector3.new(b20, b21, b22), t) -- Vector back	local t = v1:Dot(v2)	if not (t < 0 or t == 0 or t > 0) then 	-- Failsafe		return CFrame.new()	end	return CFrame.new(	v0.x, v0.y, v0.z,	v1.x, v1.y, v1.z,	v2.x, v2.y, v2.z,	v3.x, v3.y, v3.z)end function animate(weldList, factor, endList)	startList = {}	for each, child in pairs(weldList) do		table.insert(startList, child.C1)	end	for loop = 1, factor do		for each, child in pairs(weldList) do			child.C1 = matrixInterpolate(startList[each], endList[each], loop/factor)		end		wait()	endend -- Rifle reload animation 		animate({shoulders[1], shoulders[2]}, skill, {CFrame.new(-0.340310365, 1.37734306, 0.0396762304, 0.533082128, -0.298985422, -0.791474342, 0.836598635, 0.0467345566, 0.545820475, -0.12620309, -0.953113198, 0.275044024), CFrame.new(0.00948375463, -0.189517677, -0.100000188, 0.137115732, -0.173902646, 0.975170434, -0.971230805, -0.217115387, 0.0978435054, 0.194709271, -0.960531414, -0.198669493)})		firearm.Handle.Magazine1Weld.Part0, firearm.Handle.Magazine1Weld.C1 = arms[1], parts[1].CFrame:toObjectSpace(arms[1].CFrame)		firearm.Handle.Magazine2Weld.Part0, firearm.Handle.Magazine2Weld.C1 = arms[1], parts[2].CFrame:toObjectSpace(arms[1].CFrame)		animate({shoulders[1], shoulders[2]}, skill/2, {CFrame.new(-0.400107473, 1.38669002, -0.150946438, 0.533082128, -0.298985422, -0.791474342, 0.836598635, 0.0467345603, 0.545820475, -0.12620309, -0.953113258, 0.275044024), shoulders[2].C1})		animate({shoulders[1], shoulders[2]}, skill, {CFrame.new(0.344564468, 0.365162492, 0.142650843, 0.121658817, 0.508820772, -0.85223335, 0.0332551412, 0.856040537, 0.51584059, 0.992016196, -0.0910976082, 0.0872237161), CFrame.new(0.00948375463, -0.189517677, -0.100000188, 0.0795999169, -0.206656024, 0.975170434, -0.992014229, 0.0796000808, 0.0978435054, -0.0978435948, -0.975171328, -0.198669493)})			parts[1].Transparency, parts[2].Transparency = 1, 1		wait(skill/20)		parts[1].Transparency, parts[2].Transparency = 0, 0		animate({shoulders[1], shoulders[2]}, skill, {CFrame.new(-0.400107473, 1.38669002, -0.150946438, 0.533082128, -0.298985422, -0.791474342, 0.836598635, 0.0467345603, 0.545820475, -0.12620309, -0.953113258, 0.275044024), CFrame.new(0.00948375463, -0.189517677, -0.100000188, 0.137115732, -0.173902646, 0.975170434, -0.971230805, -0.217115387, 0.0978435054, 0.194709271, -0.960531414, -0.198669493)})		animate({shoulders[1], shoulders[2]}, skill/2, {CFrame.new(-0.340310365, 1.37734306, 0.0396762304, 0.533082128, -0.298985422, -0.791474342, 0.836598635, 0.0467345566, 0.545820475, -0.12620309, -0.953113198, 0.275044024), shoulders[2].C1})		firearm.Handle.Magazine1Weld.Part0, firearm.Handle.Magazine1Weld.C1 = firearm.Handle, parts[1].CFrame:toObjectSpace(firearm.Handle.CFrame)		firearm.Handle.Magazine2Weld.Part0, firearm.Handle.Magazine2Weld.C1 = firearm.Handle, parts[2].CFrame:toObjectSpace(firearm.Handle.CFrame)		animate({shoulders[1], shoulders[2]}, skill, {CFrame.new(-0.0269896537, 1.7971313, -0.100000046, 0.479425579, -3.7252903e-009, -0.87758261, 0.877582967, -2.98023224e-008, 0.479425788, -2.85333801e-008, -1.00000048, -1.41384575e-008), CFrame.new(0.00948373973, -0.189517662, -0.100000188, 0.0998334214, 0, 0.995004177, -0.995005012, 1.12750769e-007, 0.0998335034, -1.12187486e-007, -1.00000083, 1.12562955e-008)})`