Matrix Interpolation


/ Published in: Lua
Save to your folder(s)

For Notunknown99


Copy this code and paste it in your HTML
  1. function lerp(a, b, t) -- Linear interpolation
  2. return a + (b - a)*t
  3. end
  4.  
  5. function slerp(a, b, t) --Spherical interpolation
  6. dot = a:Dot(b)
  7. if dot > 0.99999 or dot < -0.99999 then
  8. return t <= 0.5 and a or b
  9. else
  10. r = math.acos(dot)
  11. return (a*math.sin((1 - t)*r) + b*math.sin(t*r)) / math.sin(r)
  12. end
  13. end
  14.  
  15. function matrixInterpolate(a, b, t)
  16. local ax, ay, az, a00, a01, a02, a10, a11, a12, a20, a21, a22 = a:components()
  17. local bx, by, bz, b00, b01, b02, b10, b11, b12, b20, b21, b22 = b:components()
  18. local v0 = lerp(Vector3.new(ax, ay, az), Vector3.new(bx , by , bz), t) -- Position
  19. local v1 = slerp(Vector3.new(a00, a01, a02), Vector3.new(b00, b01, b02), t) -- Vector right
  20. local v2 = slerp(Vector3.new(a10, a11, a12), Vector3.new(b10, b11, b12), t) -- Vector up
  21. local v3 = slerp(Vector3.new(a20, a21, a22), Vector3.new(b20, b21, b22), t) -- Vector back
  22. local t = v1:Dot(v2)
  23. if not (t < 0 or t == 0 or t > 0) then -- Failsafe
  24. return CFrame.new()
  25. end
  26. return CFrame.new(
  27. v0.x, v0.y, v0.z,
  28. v1.x, v1.y, v1.z,
  29. v2.x, v2.y, v2.z,
  30. v3.x, v3.y, v3.z)
  31. end
  32.  
  33. function animate(weldList, factor, endList)
  34. startList = {}
  35. for each, child in pairs(weldList) do
  36. table.insert(startList, child.C1)
  37. end
  38. for loop = 1, factor do
  39. for each, child in pairs(weldList) do
  40. child.C1 = matrixInterpolate(startList[each], endList[each], loop/factor)
  41. end
  42. wait()
  43. end
  44. end
  45.  
  46. -- Rifle reload animation
  47.  
  48. 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)})
  49. firearm.Handle.Magazine1Weld.Part0, firearm.Handle.Magazine1Weld.C1 = arms[1], parts[1].CFrame:toObjectSpace(arms[1].CFrame)
  50. firearm.Handle.Magazine2Weld.Part0, firearm.Handle.Magazine2Weld.C1 = arms[1], parts[2].CFrame:toObjectSpace(arms[1].CFrame)
  51. 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})
  52. 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)})
  53. parts[1].Transparency, parts[2].Transparency = 1, 1
  54. wait(skill/20)
  55. parts[1].Transparency, parts[2].Transparency = 0, 0
  56. 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)})
  57. 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})
  58. firearm.Handle.Magazine1Weld.Part0, firearm.Handle.Magazine1Weld.C1 = firearm.Handle, parts[1].CFrame:toObjectSpace(firearm.Handle.CFrame)
  59. firearm.Handle.Magazine2Weld.Part0, firearm.Handle.Magazine2Weld.C1 = firearm.Handle, parts[2].CFrame:toObjectSpace(firearm.Handle.CFrame)
  60. 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)})

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.