Advertisement
Treyzotics

Lerp

Dec 11th, 2018
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.02 KB | None | 0 0
  1. function Lerp(a, b, t)
  2. local qa = {
  3. QuaternionFromCFrame(a)
  4. }
  5. local qb = {
  6. QuaternionFromCFrame(b)
  7. }
  8. local ax, ay, az = a.x, a.y, a.z
  9. local bx, by, bz = b.x, b.y, b.z
  10. local _t = 1 - t
  11. return QuaternionToCFrame(_t * ax + t * bx, _t * ay + t * by, _t * az + t * bz, QuaternionSlerp(qa, qb, t))
  12. end
  13. local Lerp = CFrame.new().lerp
  14. function QuaternionFromCFrame(cf)
  15. local mx, my, mz, m00, m01, m02, m10, m11, m12, m20, m21, m22 = cf:components()
  16. local trace = m00 + m11 + m22
  17. if trace > 0 then
  18. local s = math.sqrt(1 + trace)
  19. local recip = 0.5 / s
  20. return (m21 - m12) * recip, (m02 - m20) * recip, (m10 - m01) * recip, s * 0.5
  21. else
  22. local i = 0
  23. if m00 < m11 then
  24. i = 1
  25. end
  26. if i == 0 and m00 or m11 < m22 then
  27. i = 2
  28. end
  29. if i == 0 then
  30. local s = math.sqrt(m00 - m11 - m22 + 1)
  31. local recip = 0.5 / s
  32. return 0.5 * s, (m10 + m01) * recip, (m20 + m02) * recip, (m21 - m12) * recip
  33. elseif i == 1 then
  34. local s = math.sqrt(m11 - m22 - m00 + 1)
  35. local recip = 0.5 / s
  36. return (m01 + m10) * recip, 0.5 * s, (m21 + m12) * recip, (m02 - m20) * recip
  37. elseif i == 2 then
  38. local s = math.sqrt(m22 - m00 - m11 + 1)
  39. local recip = 0.5 / s
  40. return (m02 + m20) * recip, (m12 + m21) * recip, 0.5 * s, (m10 - m01) * recip
  41. end
  42. end
  43. end
  44. function QuaternionToCFrame(px, py, pz, x, y, z, w)
  45. local xs, ys, zs = x + x, y + y, z + z
  46. local wx, wy, wz = w * xs, w * ys, w * zs
  47. local xx = x * xs
  48. local xy = x * ys
  49. local xz = x * zs
  50. local yy = y * ys
  51. local yz = y * zs
  52. local zz = z * zs
  53. return CFrame.new(px, py, pz, 1 - (yy + zz), xy - wz, xz + wy, xy + wz, 1 - (xx + zz), yz - wx, xz - wy, yz + wx, 1 - (xx + yy))
  54. end
  55. function QuaternionSlerp(a, b, t)
  56. local cosTheta = a[1] * b[1] + a[2] * b[2] + a[3] * b[3] + a[4] * b[4]
  57. local startInterp, finishInterp
  58. if cosTheta >= 1.0E-4 then
  59. if 1 - cosTheta > 1.0E-4 then
  60. local theta = math.acos(cosTheta)
  61. local invSinTheta = 1 / math.sin(theta)
  62. startInterp = math.sin((1 - t) * theta) * invSinTheta
  63. finishInterp = math.sin(t * theta) * invSinTheta
  64. else
  65. startInterp = 1 - t
  66. finishInterp = t
  67. if 1 + cosTheta > 1.0E-4 then
  68. local theta = math.acos(-cosTheta)
  69. local invSinTheta = 1 / math.sin(theta)
  70. startInterp = math.sin((t - 1) * theta) * invSinTheta
  71. finishInterp = math.sin(t * theta) * invSinTheta
  72. else
  73. startInterp = t - 1
  74. finishInterp = t
  75. return a[1] * startInterp + b[1] * finishInterp, a[2] * startInterp + b[2] * finishInterp, a[3] * startInterp + b[3] * finishInterp, a[4] * startInterp + b[4] * finishInterp
  76. end
  77. end
  78. end
  79. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement